Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: Lexical closures

by Corion (Patriarch)
on Oct 25, 2008 at 08:27 UTC ( [id://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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://719479]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-19 18:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found