Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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

by jeffa (Bishop)
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.


(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
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).
      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.
      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://139996]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2018-06-20 12:29 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.