http://www.perlmonks.org?node_id=719527


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:

my @flist = ( sub {0 * $_[0]}, sub {1 * $_[0]}, sub {2 * $_[0]});
It's obvious, then, that 0 2 4 is the correct output of
foreach my $f (@flist) { print $f->(2), "\n"; }