I'm experiencing a strange variable scope issue.

Normally, I expect that lexical 'my' vars declared at the package
scope (i.e. at the top of a file), should be visible to subroutines
declared in the same package, and should maintain their values between
calls to those subroutines.

Under mod_perl, I find inconsistent behaviour.  It works fine when a
module is loaded via the PerlModule directive in httpd.conf.  However
when a module is loaded via startup.pl, the package lexicals "forget"
their values between calls.

    # Foo.pm
    package Foo;
    use strict;

    my $PACKAGE_LEXICAL;

    warn "I'm being loaded";

    sub load_var {
        my $param = shift;

        $PACKAGE_LEXICAL = $param if $param;

        warn "PACKAGE_LEXICAL: $PACKAGE_LEXICAL\n";
    }
    1;

    # startup.pl

    use Foo;
    Foo::load_var('wubba');


With the above I expect to be able to call the following in some
handler:

    Foo::load_var()

...and $PACKAGE_LEXICAL should still be 'wubba'.

The strange thing is that this works when Foo is required in
httpd.conf via a "PerlModule Foo" directive.  But when Foo is pulled
in via a "use Foo" in startup.pl, $PACKAGE_LEXICAL does not keep it's
value.

In both cases, Foo.pm gets loaded twice at startup, (there are two
"I'm being loaded messages" in the logs which I'm assuming this is
because PerlFreshRestart is On by default).  Also in both cases,
Foo.pm is being loaded by uid 0, so I'm assuming this is not a
parent/child sharing problem.

Can anybody see what I'm missing?

The Eagle book suggests that it's best to load modules from startup.pl
rather than httpd.conf.  Is this not such a good idea after all?

Michael




Reply via email to