Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re^4: Lexical closuresby mpeever (Friar) |
on Oct 25, 2008 at 20:07 UTC ( [id://719544]=note: print w/replies, xml ) | Need Help?? |
OK, I took some time to mock out some Scheme: here are both behaviours you've seen:
cclosures uses the equivalent ofdeclaring my $i inside the foreach loop: it defines a new variable called val for every iteration. cclosures2 uses the equivalent of declaring my $i outside the foreach loop: val gets reassigned with every iteration. Notice the closure closes over the variable, not the value. So when the variable is reassigned, the value inside the closure changes too. HTH
In Section
Seekers of Perl Wisdom
|
|