Moin André,

Am 26.04.2010 um 21:44 schrieb André Warnier:
> cr...@animalhead.com wrote:
>> The retention of values from previous executions applies
>> only to global variables.
> 
> Ah, yes.
> But that would have triggered another discussion (which it might now 
> still do of course), about what exactly /is/ a global variable, in the 
> context of a mod_perl handler or perl script run under modperl::Registry.

Let's first clarify it for Perl in general, and then for mod_perl.

A global variable in Perl is any variable not declared with "my". Which 
includes variables declared with "our" or "use vars" (I'll get to these), and 
also variables created by full qualification, as in "$Bla::Blub = 1".

A lexical variable in Perl is any variable declared with "my", regardless of 
the scope, which may be file-level. Unlike globals, lexical variables aren't 
directly accessible from outside the package.

A global variable declared (or introduced, or admitted) with "use vars" is in 
scope for the entire package where it is declared. A global variable declared 
with "our" is in scope only for the lexical scope where it is declared (see 
"perldoc -f our").

(There's also "local", a misnomer, to temporarily stash away the current value 
of a global variable and shadow it with another value. We can leave it out of 
the picture here.)

Now, how is this different for mod_perl? Well, it isn't, if you think about it, 
or rather it boils down to the difference between a mod_perl handler and your 
typical batch script. Your batch script is invoked, it runs, and ends. Running 
it probably includes some initialization code of yours placed at the file 
level. Next time around, the whole thing start anew. Nothing special here.

A mod_perl handler, as you know, is loaded once, and unless it is reloaded, is 
only acted upon by invocation of its functions, such as handler(). Which means 
that reinitialization doesn't happen automatically, as with your batch script 
running in a new process each time.

So what does this mean for file level lexical variables (my-variables) you have 
defined? Well, they don't get reinitialized (unless you provide code to do so), 
so they start behaving like global variables, retaining state between 
invocations. They are not, however, accessible from outside the current 
package, so they're still lexical variables.

There's one more thing to understand, especially in the context of 
Apache::Registry and Apache2::Registry, and that's lexical "my" variables 
referenced from nested names subroutines. You do not usually create nested 
named subroutines, but the Registry handler does it for you by wrapping your 
registry script in a handler subroutine in a package made up from the 
filesystem path of rour registry script. So if you define a registry script 
with a subroutine that references a lexical variable from the enclosing scope, 
you'll see the familiar warning message "Variable "$x" will not stay shared".

You can read up about this issue here:

http://perl.apache.org/docs/general/perl_reference/perl_reference.html

Hope this helps :-)

-- 
Michael.Ludwig (#) XING.com

Reply via email to