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"; }
In Section
Seekers of Perl Wisdom