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

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

by perrin (Chancellor)
on Jan 19, 2002 at 03:34 UTC ( [id://140000]=note: print w/replies, xml ) Need Help??


in reply to (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).
  • Comment on Re: (jeffa) Re: Why are closures cool, continued?

Replies are listed 'Best First'.
Re: Re: (jeffa) Re: Why are closures cool, continued?
by dws (Chancellor) on Jan 19, 2002 at 04:37 UTC
    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...

      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.
Re: Re: (jeffa) Re: Why are closures cool, continued?
by mr.dunstan (Monk) on Jan 19, 2002 at 03:42 UTC
    Hey! I was thinking something along those lines, just because the variables themselves are not declared -within- the closure ... only near it?

    I am thinking this means:
    1) There is no spoon, I mean closure.
    2) That's a closure, but not an academic example of a closure.
    3) Mr. Dunstan, you are mincing words to gain karma.

    Zuh? -mr.dunstan
      Sort of. To make a closure, you have to refer to lexical variables that are declared outside of the scope of the current sub. That's the definition of a closure.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-19 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found