Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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

by dws (Chancellor)
on Jan 19, 2002 at 04:37 UTC ( #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...

Comment on Re: Re: (jeffa) Re: Why are closures cool, continued?
Download Code
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?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2015-11-28 12:21 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (741 votes), past polls