modperl-closures_vs_anonymous_subs

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



To: "raptor" <raptor@unacs.bg>
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: my OR our that is the question ?!
Date: 10 Aug 2001 05:04:43 -0700

>>>>> "raptor" == raptor  <raptor@unacs.bg> writes:

raptor> - closure makes a new instance of of itself and get the latest value of
raptor> outer-lexical variable in its private space.
raptor> - there is no reason why normal sub() can get/alias! this value every time,
raptor> this doens't broke the closure behaviour (i.e. normal sub() doesn't need to
raptor> make its own private copy but use the outer-scope-lexical  ) or I'm wrong
raptor> again.

You're mixing two concepts:

a subroutine may or may not be named - those that don't are called
"anonymous subroutines"

a subroutine may or may not see lexical variables that can go out of
scope before the lifetime of the subroutine's scope - that that do are
called "closures".

An anonymous subroutine is not necessarily a closure.  A closure is
not necessarily an anonymous subroutine.  A named subroutine may have
visibility of limited-lifetime variables, and that's what's happening
here... a closure-behavior on the first activation of the subroutine.
Now, since the subroutine has a slot in the global symbol table (it's
named!), it has a lifetime of the entire program.  So the first time
the subroutine is activated (called) in the program, it wakes up, sees
some limited lifetime variables, and says "those are MINE MINE all
MINE", and never lets go of them.  But it stays alive forever, so even
on subsequent activations, it has to hold the first scope it saw to be
a proper closure.

Probably a bit fuzzy, but it makes sense.

===

the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu