Problems? Is your data what you think it is? | |
PerlMonks |
Re^3: closures: anonymous subs vs function templates?by Athanasius (Archbishop) |
on Dec 21, 2014 at 16:45 UTC ( [id://1110967]=note: print w/replies, xml ) | Need Help?? |
Hello Laurent_R, I think you are correct: your example code does seem to fulfil the requirements for a closure according to the Wikipedia article Closure_(computer_programming). And at the start of the tutorial Closure on Closures, broquaint takes a similar view: However, I believe this isn't entirely accurate as a closure in perl can be any subroutine referring to lexical variables in the surrounding lexical scopes. But, as you point out, this type of closure isn’t very useful, since there is no way to change the value of $add from outside the block. And if we try to return a reference to a named function from within another function, it doesn’t work as required:
Output:
Hope that helps,
In Section
Seekers of Perl Wisdom
|
|