On Wed, Aug 22, 2001 at 10:34:49AM +0100, Dave Mitchell wrote:
> Paul Johnson <[EMAIL PROTECTED]> wrote:
> > Actually, foo() is not a closure. A closure is an anonymous subroutine
> > and foo() clearly has a name.
>
> Damain's definition of a closure includes named subs:
>
> "In Perl, a closure is just a subroutine that refers to one or more lexical
> variables declared outside the subroutine itself" - OO perl, p 56.
So it does. And he's pretty explicit about it, even giving examples
using named subs. However, the Perl (5) documentation is pretty
explicit that, in a Perl sense anyway, a closure is an anonymous
subroutine. I don't think it says anywhere that a named sub is not a
closure, but it always says that a closure is an anonymous sub.
> > Now, if you were proposing that named subroutines should behave like
> > closures in this regard, which is what Piers and probably most others
> > were expecting, I suspect that you would probably get broad agreement.
Certainly from Damain, it would appear.
> I'm not sure I understand this bit - named subs are really no different from
> anon subs in respect to closures, except that named subs get instantianted
> once - at compile time, and anon subs may get instantiated multiple
> times, thus creating multiple private copies of the outer lexicals.
But "no different, except" means that they are different :-)
Try changing your original example from
sub foo {
to
*foo = sub {
and you'll see that everything works "as expected".
--
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net