[Edinburgh-pm] variable scope

Andrew Thomson andrewt at compunext.demon.co.uk
Fri Apr 25 12:26:37 PDT 2008

This section appears under the foreach subsection on the perlsyn page
supplied with the Activestate distro:

"The foreach loop iterates over a normal list value and sets the variable
VAR to be each element of the list in turn. If the variable is preceded with
the keyword my, then it is lexically scoped, and is therefore visible only
within the loop. Otherwise, the variable is implicitly local to the loop and
regains its former value upon exiting the loop. If the variable was
previously declared with my, it uses that variable instead of the global
one, but it's still localized to the loop. This implicit localisation occurs
only in a foreach loop."


-----Original Message-----
From: edinburgh-pm-bounces+andrewt=compunext.demon.co.uk at pm.org
[mailto:edinburgh-pm-bounces+andrewt=compunext.demon.co.uk at pm.org] On Behalf
Of asmith9983 at gmail.com
Sent: 25 April 2008 19:40
To: Edinburgh Perl-mongers
Subject: [Edinburgh-pm] variable scope

I've been trying to hone up today on variable scoping after using
chomp(my @lst=<>); return @lst;    I know slurping big style is not good 
but the file I'm slurping has less than 30 lines.  I found a www page 
that foreach my $i (@list?) { confined $i to the scope of the loop. However
wanted to verify this in the perldoc pages but couldn't locate the
words on how this worked. I'm not satisfied with just using code because it 
does what I want. I want to understand why, so it doesn't bite me when I use

it slightly differently. I guess this is why I have a love/hate relationship

with Perl. Most of the time it works just fine but sometimes because of my 
lack of knowledge its silently temperamental.

I like the perldoc docs but often find it  difficult to locate the
I really need.

Edinburgh-pm mailing list
Edinburgh-pm at pm.org http://mail.pm.org/mailman/listinfo/edinburgh-pm

More information about the Edinburgh-pm mailing list