in reply to quantum behavior in perl?

Sounds like a closure problem.

{ my $var = 'this string'; sub test1 { print("var = $var\n"); } sub test2 { eval(' print("var = $var\n"); '); } sub test3 { $var; eval(' print("var = $var\n"); '); } } test1(); # var = this string test2(); # var = (undef) test3(); # var = this string

Perl doesn't know that test2 will need $var, so it doesn't capture $var.

Is your my variable inside curlies? Remember that mod_perl Registry scripts are placed inside a function and therefore inside curlies.

Update: Exiting any scope will cause closures and thus the problem. Curlies create scopes, but so do do, require and use as tye points out in his reply. The following will also exhibit the problem:

# Module.pm pacakge Module; my $var = 'this string'; sub test1 { print("var = $var\n"); } sub test2 { eval(' print("var = $var\n"); '); } sub test3 { $var; eval(' print("var = $var\n"); '); }
# script.pl use Module; Module::test1(); # var = this string Module::test2(); # var = (undef) Module::test3(); # var = this string

Replies are listed 'Best First'.
Re^2: quantum behavior in perl? ("curlies")
by tye (Sage) on Aug 18, 2007 at 06:29 UTC

    "Curlies" aren't required for this problem. This is not the "will not stay shared problem" (and more than "curlies" is required for that; the my statement has to have the potential to be run more than once -- at least, that was my understanding).

    All that is required for this problem is for the scope in which the lexical was declared to be left before the function is called (I didn't previously realize that even this was required). This can happen if the lexical and sub are in a module, for example. The original node says "A package I have", so a separate file such as a module seems likely; therefore curlies likely don't matter.

    - tye