|Perl: the Markov chain saw|
Why are closures cool, continued?by mr.dunstan (Monk)
|on Jan 19, 2002 at 02:17 UTC||Need Help??|
mr.dunstan has asked for the wisdom of the Perl Monks concerning the following question:
This has been a hot topic at my workplace and my boss came up with this example for showing how closures work.
Prepare to correct me if I'm wrong - I think what we've got here is a constructor (sort of) that returns a reference to an anonymous subroutine. When we call new() we're also setting $class and @stuff in the namespace of that anonymous subroutine to be "dave" and ("oranges", "grapefruit") respectively.
When I run this thing I get:
dave: oranges,grapefruit,debug message
Ok, so this is kind of neat, now I understand how you can use one constructor and some properties in order to respond with a reference to an anonymous subroutine of your choosing, but what I'm curious about is - is this -really- a closure?
I'm not completely versed in how long values like $class and @stuff would live inside a sub - only for the duration of the execution of the sub, right?
The closure (or rather using those values in the anonymous sub) makes these values live on past the execution of the sub, right?
Super Confused, -mr.dunstan