Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re^3: Lexical closures

by Corion (Pope)
on Oct 25, 2008 at 08:27 UTC ( #719479=note: print w/replies, xml ) Need Help??

in reply to Re^2: Lexical closures
in thread Lexical closures

Well, it depends on what the closures really close over, respectively when a new instance of the loop variable/value gets created. It seems that in Perl, at least when you use a lexical loop variable, you get a new copy each iteration, while in the other languages, you don't. To test this theory, create a new lexical variable within the loop body in each language and see if that's different, that is, use the local equivalent of the following Perl code to create the closures:

for (0..2) { my $i = $_; push @funcs, sub { $i * $_[0] }; };

You want to force allocation of a new instance of the lexical variable so your subroutine references get a new instance on each way around.

Of course, most of the languages have map, so using it would be more apt.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://719479]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2018-03-23 21:19 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (296 votes). Check out past polls.