in reply to •closures are NOT anonymous subroutines necessarily
in thread subs as args
A closure is subroutine that has captured its lexical environment which has now gone out of scope.For completeness, I would add that not all closures are subroutines; regular expressions that use the (?{}) construct can also be closures. For example,
sub a { my $i = 0; # note that the regex is compiled only once () = shift =~ /a(?{print $i++})/g; } a("ababa"); # prints 0 1 2 a("ajaja"); # prints 3 4 5
I don't know if these regular expressions are implemented internally as subroutines, but they certainly don't look like subroutines, so people are often surprised to see this behavior.
|
|---|