in reply to Take the next step into closures (Re: subs as args)
in thread subs as args
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: •closures are NOT anonymous subroutines necessarily
by itub (Priest) on Jan 20, 2005 at 22:08 UTC | |
|
Anon subs are a good way to introduce closures
by dragonchild (Archbishop) on Jul 08, 2002 at 17:20 UTC |