in reply to Re^2: Lexical closures
in thread Lexical closures
The real question is, what do your Scheme and Common Lisp code samples look like?
The Perl behaviour is precisely what I would expect. It generates three closures, each capturing a value of $i. So your first foreach block reduces to:
It's obvious, then, that 0 2 4 is the correct output ofmy @flist = ( sub {0 * $_[0]}, sub {1 * $_[0]}, sub {2 * $_[0]});
foreach my $f (@flist) { print $f->(2), "\n"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Lexical closures
by mpeever (Friar) on Oct 25, 2008 at 20:07 UTC |
In Section
Seekers of Perl Wisdom