A closure is an anonymous subroutine that's been created by another subroutine.Not quite. That's a bad meme that perists, despite our efforts to the contrary.
A closure is subroutine that has captured its lexical environment which has now gone out of scope.
This is orthogonal to the concept of an anonymous subroutine, which may or may not need to be "closed". In other words, the properties of "having a name" and "being a closure" are uncorrelated.
Here's a named subroutine that's a closure:
This named subroutine gimme_next is a closure. And here's an example of an anonymous subroutine which is not a closure:BEGIN { my $current = 0; sub gimme_next { ++$current } }
So, please don't confuse anonymous subroutine with closure: they're really different things.my $foo = sub { 2 * shift };
-- Randal L. Schwartz, Perl hacker
In reply to •closures are NOT anonymous subroutines necessarily
by merlyn
in thread subs as args
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |