Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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...

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
Node Status?
node history
Node Type: note [id://140015]
help
Chatterbox?
[Eily]: Maybe the puns in his dialogues were somehow kept through translation, but I can tell he lost his accent
[LanX]: qui?
[Eily]: Astérix
1nickt gives up on finding a Perl YAML parser that supports hash merging. In 2017.
[LanX]: Oh yes true, he lost his accent! xD
[Eily]: and since his name is a modified version of astérisque (asterisk), which is a typographical symbol, I think it's even funnier that he would lose a diacritic
[Eily]: the pun doesn't work so well with "he lost a diacritic" though :P
[LanX]: Obelix might offer to find it again ...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (13)
As of 2017-05-24 14:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?