Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by jeffa (Chancellor)
on Jan 19, 2002 at 03:20 UTC ( #139996=note: print w/ replies, xml ) Need Help??


in reply to Why are closures cool, continued?

If you wanted a 'real' constructor, just add bless:

sub new { my ($class, @stuff) = @_; return bless sub { doit(@stuff, @_) },$class; }

Now, if you want what i consider to be a good, everyday, pratical solution that uses a closure (not just a crazy way to make eyes go cross-eyed), then check out (the recently renamed) (jeffa) Practical Use For What jeffa Thought A Closure Was (3Re: Style Question on Closures). There are, of course, many more practical uses for closures ... this one has served me well, and i really think that it is easy to understand.

UPDATE: nope - i am wrong again. Sorry mr.dunstan. Thanks for the catch perrin.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)


Comment on (jeffa) Re: Why are closures cool, continued?
Download Code
Re: (jeffa) Re: Why are closures cool, continued?
by perrin (Chancellor) on Jan 19, 2002 at 03:34 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).
      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.
      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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2014-09-18 18:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (120 votes), past polls