Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

(Ovid) Scoping in for loops

by Ovid (Cardinal)
on Aug 20, 2000 at 18:34 UTC ( [id://28727]=note: print w/replies, xml ) Need Help??


in reply to for loops, closures

Consider the for loop in the following form:
my $i; for $i (1..8) { print "$i\n"; }
In this case, $i is not being assigned the values 1 through 8. Instead, it becomes an alias to the values being iterated over. That's why so many Perl newbies use a construction like this to iterate over an array and then wonder why their array changed at the end. As for the scoping issue, page 100 of Programming Perl states that the variable immediately after the for|foreach in these loops (or $_ if not specified) is implicitly local to the loop and will regain its former value after exiting the loop. Now look at the following snippet:
for $i (1..8) { push( @ref_list, sub { print " \$i == $i\n" } ); }
Because $i is actually an alias to the variables being iterated over, the second $i in \$i == $i is pointing to the memory location where the appropriate value in the for loop is being stored. If the value at that memory location could be changed, your anonymous subroutine would print a different value. This is weird, but it works:
my ( @ref_list, $i, $j ); $j = 10; for $i ($j) { push( @ref_list, sub { print " \$i == $i\n" } ); } &{$ref_list[0]}; $j = 20; &{$ref_list[0]};
The above will print:

    $i == 10
    $i == 20

The C-style for ($i; $i<=8; $i++) {} does not have this benefit. Here, you've changed the value of $i, period. Unless you've exited early or done something funky with $i's value, it will be equal to 9 at the end of this loop. Consider the following snippet:

for ($i = 1; $i <= 8; $i++ ) { push( @ref_list, sub { print " \$i == $i\n" } ); }
There is no "implicit alias" here, so the second $i simply points to $i.

Hope this helps.

Cheers,
Ovid

Update: While I got the point across, I have to say that this is one of the most poorly written posts I have done. Ugh. No more posting before coffee!!!

Replies are listed 'Best First'.
RE: (Ovid) Scoping in for loops
by Aighearach (Initiate) on Aug 20, 2000 at 20:09 UTC
    Thank you very very much for the explanation, Ovid. I see now where I was going wrong. I'm not sure why I was expecting the anon sub ref to be using a copy of the lexical, but now I am understanding "deep binding" better. Maybe I should blame it on the word "magically" in the FAQ. ;)
    Paris Sinclair    |    4a75737420416e6f74686572
    pariss@efn.org    |    205065726c204861636b6572
    http://sinclairinternetwork.com
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://28727]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-24 00:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found