> On Sun, Jan 02, 2000 at 01:13:26PM -0800, Aaron Turner wrote:
> >
> > This is driving me nuts. This code works the first few exectutions, but
> > then doesn't after a while. It's pretty simple, it should print out:
> >
> > Name = value1
> > Name = value2
> >
> > then after a few reloads it prints:
> >
> > Name = value1
> > Name = value1
> >
> > I'm sure it's something stupid, but my brain is apparently fried.
>
> This is a FAQ. You're declaring a closure by using %variable in that
> inside subroutine. Yes, it's 'my' to the handler, but using it inside the
> other subroutine makes it kind of like a persistent copy of the very first
> usage of the variable. So, even if the handler gets a new %variable each
> time, the closure never sees it. That's the behavior you're seeing, per
> apache process.
That's right. But actually this is not a closure but a nested subroutine
effect. I've erroneously called this effect a closure in the guide, I've
fixed it now (see perl.apache.org/guide/perl.html). But the implications
are correct.
% perldoc -q closure
=head1 Found in /usr/lib/perl5/5.00503/pod/perlfaq7.pod
=head2 What's a closure?
Closures are documented in L<perlref>.
I<Closure> is a computer science term with a precise but
hard-to-explain meaning. Closures are implemented in Perl as anonymous
subroutines with lasting references to lexical variables outside their
own scopes. These lexicals magically refer to the variables that were
around when the subroutine was defined (deep binding).
Closures make sense in any programming language where you can have the
return value of a function be itself a function, as you can in Perl.
Note that some languages provide anonymous functions but are not
capable of providing proper closures; the Python language, for
example. For more information on closures, check out any textbook on
functional programming. Scheme is a language that not only supports
but encourages closures.
... the FAQ section is continued
_______________________________________________________________________
Stas Bekman mailto:[EMAIL PROTECTED] http://www.stason.org/stas
Perl,CGI,Apache,Linux,Web,Java,PC http://www.stason.org/stas/TULARC
perl.apache.org modperl.sourcegarden.org perlmonth.com perl.org
single o-> + single o-+ = singlesheaven http://www.singlesheaven.com