in reply to Re: Re: Re: static-like persistence of my variable due to trailing conditional
in thread static-like persistence of my variable due to trailing conditional

my $foo = 'blah' is not executed at all. Instead, simply $foo = 'FOO' gets executed, setting $main::foo (you can verify this by printing out $main::foo after you call mycode() with no arguments).

Wrong. Actually, the fact that $foo is scoped lexically must be known at compile time - think about it, how else would strict work? After the compiler sees a my, it generates ops referring to lexicals rather than globals for the variables. However, allocation of these lexicals happens at runtime. By adding an if, you prevent the allocation at runtime, but you do not prevent the lexical scoping that happens at compile time.

The effect is that subsequent invocations of the routine refer to a lexically scoped scalar that was allocated in a previous invocation - the same way as closures do.

Makeshifts last the longest.

  • Comment on Re^4: static-like persistence of my variable due to trailing conditional