Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: evaluation strategy of perl

by kabel (Chaplain)
on Sep 20, 2002 at 14:14 UTC ( [id://199476]=note: print w/replies, xml ) Need Help??


in reply to evaluation strategy of perl

the problem here is that $i is declared only once and at the end of the for loop it has the value 3. i think what you want is something like this:
foreach my $i (0 .. 2) { push @d, sub { print "$i\n" }; }
in each iteration a new variable $i is created. the old $i (from the former loop iteration) is no longer accessible via $i, but because there is a subroutine referencing it, it is not garbage collected.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-23 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found