[Melbourne-pm] Perl switch statements
Ryan, Martin G
Martin.G.Ryan at team.telstra.com
Tue Oct 23 15:25:55 PDT 2012
Jacinta,
> On Tuesday, 23 October 2012 4:10 PM, Jacinta Richardson [jarich at perltraining.com.au] wrote;
> {
> local $x = 1;
> while( $x < 10 ) {
> say $x;
> }
> }
>
> Notice that extra set of parentheses and the local?
>
> As per <perldoc perlsyn> this is intentional:
>
> 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 localization occurs only in a "foreach" loop.
>
>
> No point arguing, it's existed this way for a long, long time. ;)
Thank you for expanding on that - I found it very illuminating.
Fascinating how if it was previously declared with "my", it localizes a lexical variable. (which you can't do normally, yes?)
I've always used a fresh variable for the cause - "$i" if I'm running low on imagination - and hence the question doesn't arise (probably best that way for the sanity of future maintainers).
Martin
More information about the Melbourne-pm
mailing list