![]() |
|
Think about Loose Coupling | |
PerlMonks |
Re: Closures clarificationby memnoch (Scribe) |
on Dec 04, 2007 at 14:03 UTC ( #654793=note: print w/replies, xml ) | Need Help?? |
If I'm not mistaken, neither of your examples are closures. According to Intermediate Perl, a closure is "just a subroutine that references a lexical variable that has gone out of scope". In your first example, while there is an anonymous subroutine reference passed to the find() function, it doesn't reference any lexical variables. In your second example, 1) there is no subroutine, and 2) the $a enclosed in braces is a different variable than the one outside of them. Note also that your first example was incorrect. It should have been: You were missing the "use" statement, missing a "(" in the find function, and the "{" in the sub definition was misplaced. An example of a closure from Intermediate Perl) is as follows: So, here the subroutine is a closure because it refers to the lexical variable $count. Hope this helps. memnoch Gloria in Excelsis Deo!
In Section
Seekers of Perl Wisdom
|
|