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

spurperl has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, Can you please explain the mechanism that makes the following print "0 2 4" in Perl:
my @flist = (); foreach my $i (0 .. 2) { push(@flist, sub {$i * $_[0]}); } foreach my $f (@flist) { print $f->(2), "\n"; }
The Python equivalent prints "4 4 4":
flist = [] for i in xrange(3): def func(x): return x * i flist.append(func) for f in flist: print f(2)
Javascript and Common Lisp behave like Python, while Scheme behaves like Perl.