Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: a sub within a sub -- revisiting

by Anonymous Monk
on Apr 10, 2010 at 04:14 UTC ( #833943=note: print w/ replies, xml ) Need Help??


in reply to a sub within a sub -- revisiting

bar is still visible outside of foo, so make it lexical

sub foo { my $bar = sub { my ($i) = @_; push @arrb, $arra[$i]; }; $bar->($#arra); ... }


Comment on Re: a sub within a sub -- revisiting
Download Code
Replies are listed 'Best First'.
Re^2: a sub within a sub -- revisiting
by ikegami (Pope) on Apr 11, 2010 at 06:21 UTC

    Note that the above leaks if the anon sub accesses $bar. That would be the case if the anon sub is recursive, for example. I find the following simpler, and it doesn't leak.

    sub foo { ... local *bar = sub { ... }; bar(...); }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2015-07-31 02:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls