Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Re: (jeffa) Re: Why are closures cool, continued?

by dws (Chancellor)
on Jan 19, 2002 at 04:37 UTC ( [id://140015]=note: print w/replies, xml ) Need Help??


in reply to Re: (jeffa) Re: Why are closures cool, continued?
in thread Why are closures cool, continued?

That's a good example of using code refs, but it's not a closure. There are no private variables (unless I missed something there).

The anonymous routine (blessed or not) refers to a lexically-scoped variable in its enclosing scope. That lexically-scoped variable is magically kept alive (in the anonymous subroutine) after that enclosing scope has been exited. That is what makes an anonymous subroutine a closure.

sub makeAnonymousSub { sub { print "I am not a closure\n" } } sub makeClosure { my $x = shift; sub { print "I keep $x alive, thus I'm a closure\n" } }

Update: This is in agreement with perrin, btw. I'm just filling in some details...

Replies are listed 'Best First'.
Re: Re: Re: (jeffa) Re: Why are closures cool, continued?
by perrin (Chancellor) on Jan 19, 2002 at 05:57 UTC
    Are we looking at the same thing? The example jeffa posted has this anonymous routine in it:
    print $foo->print_me(sub {ucfirst shift}), "\n";
    It's not a closure.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-19 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found