Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
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?
[Corion]: Hmm - no, nothing jumps out immediately
Corion jumps into bed
[choroba]: On the indicated line, it uses (caller 1)10
[choroba]: (caller 1)[10] that is

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2017-09-25 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (291 votes). Check out past polls.

    Notices?