|P is for Practical|
Re: Closure - Perl Perspectiveby tobyink (Canon)
|on Oct 17, 2014 at 09:13 UTC||Need Help??|
A closure is a subroutine that makes use of a lexical variable defined outside the subroutine. For example:
The foo() sub is said to be a "closure", and the $foo variable has be "closed over".
When most people talk about closures though, they are usually referring to anonymous functions rather than named ones like above. Something like:
In fact, people often use the word "closures" to refer to anonymous functions even if the function doesn't close over any variables!
This is probably due to the fact that in most programming languages (including Perl) there's no syntactic difference between defining an anonymous sub that closes over a variable verses one that does not. They are implemented subtly differently internally though as can be seen by this example:
You'll notice that the three non-closure coderefs share a reference address. This is an optimization that Perl does. (I have at least twice had to work around this optimization by defining a dummy variable and closing over it. But then again, I do a lot of weird stuff in Perl. Most people probably never need to care about this difference.)
Anyway, coming back to Data::Form::Validator - it does accept coderefs as arguments in some places. Whether those coderefs are technically closures (i.e. they close over external variables), it probably doesn't care.