Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: Why callbacks?

by Joost (Canon)
on Mar 30, 2007 at 17:32 UTC ( [id://607539]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Why callbacks?
in thread Why callbacks?

Named subroutines never go out of scope; they're global - bound to a package just like global variables.

Anonymous subrefs can go out of scope, just like any other variable, but, just like any other variable, you can pass them around:

sub call { my ($arg1,$arg2) = @_; my $callback = sub { my ($arg3) = @_; return $arg1 + $arg2 + $arg3; }; call_func_needing_callback($callback); } # or even sub call { my ($arg1,$arg2) = @_; call_func_needing_callback( sub { my ($arg3) = @_; return $arg1 + $arg2 + $arg3; }); }

Also note that there are scoping issues with defining named closures within other subroutines (this probably has to do with named subroutines being global), while unnamed subroutines/closures will work as real lexical closures.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-04-25 08:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found