in reply to Re: typeglob reference deletes global lexical
in thread typeglob reference deletes global lexical

During the first call to foo, $_BAR is false, even though it was initialized to 1.

Assuming the value didn't change, that usually means that foo was called before the assignment occurred.

use strict; use warnings; my $x = 1; sub foo { print("[$x]\n"); } BEGIN { foo(); } # Prints "[]" and gives an undefined warning.

Or since you're doing symtab manipulations, maybe it's getting clobbered by something. But that would mean the variable isn't a lexical (my) variable as you claim it is.

Seeing you didn't provide code that reproduces the problem, there's not much we can do.

Replies are listed 'Best First'.
Re^3: typeglob reference deletes global lexical
by robnagler (Novice) on Nov 08, 2008 at 02:46 UTC

    The code is here http://www.bivio.biz/hm/download-bOP I can't seem to distill it to the point of reproducing the problem in a short script.

    Needless to say a simple implementation does not produce the bug. I've stripped all the non-essential parts out, and started mod_perl, but the failure does show up. It is reliable for a particular package and code configuration. However, changing code in other packages can cause the failure to disappear.

      mod_perl? Don't use "global" lexicals in mod_perl Registry script. Had you turned on warnings, you would have gotten one. mod_perl wraps your script in a function, so you basically get
      sub mod_perl_wrapper { ##### Begining of script ##### use strict; use warnings; my $global = $_[0]; # Say some value from the request. sub foo { # Warms 'Variable "$global" will not stay shared' print("$global\n"); } foo(); ##### End of script ##### } mod_perl_wrapper(4); # First request prints "4" mod_perl_wrapper(5); # Subsequent requests also prints "4"

      Use a package variable instead.

      sub mod_perl_wrapper { ##### Begining of script ##### use strict; use warnings; local our $global = $_[0]; # Say some value from the request. sub foo { print("$global\n"); } foo(); ##### End of script ##### } mod_perl_wrapper(4); # 1st request prints "4" mod_perl_wrapper(5); # 2nd request prints "5"

      I almost mentioned this earlier. I can't believe you went through all this trouble rather than turning on warnings. Even if you don't normally use them for whatever reason, adding use strict; and use warnings; is the first thing you should do when you have a problem you can't explain.