Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Why are closures cool, continued?

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


in reply to Why are closures cool, continued?

That's a very confusing example because it looks like an object but isn't. That example would still work if you remove the OO looking stuff:
#!/usr/local/bin/perl use strict; my $dave = subref("dave", "oranges", "grapefruit"); $dave->("debug message"); sub subref { my (@stuff) = @_; return sub { doit(@stuff, @_) }; # <-- closure here? } sub doit { print shift, ": ", join(",", @_), "\n"; }
Yes, it is a closure, but it's also an anonymous sub ref, so it's showing multiple things. Here's a much simpler closure:
my $foo; sub bar { $foo++; }
After execution, bar() has a private copy of $foo. TheDamian uses this in his book to do private variables for classes.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://139990]
help
Chatterbox?
[stevieb]: I just don't feel like going hardcore on anything today, so I thought I'd *use* my software instead of just write it
[beech]: let the surprises begin :D

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2017-07-21 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (336 votes). Check out past polls.