in reply to Re: static variable hack
in thread static variable hack
Overall right. Just nitpicking two details.
First, optimization is not a factor. It's simply whether the my gets executed or not.
Second, you're also off on the timing. Clearing occurs at end of scope. It's triggered by the my, but it doesn't occurs when the my is encountered. (By the way, that's also when a new my variable is created if the refcount is greater than 1.)
Both points can be observed in
sub foo { my ($reset) = @_; my $x if $reset; # Will reset at end of scope if $reset is true. return ++$x; } print foo(0), "\n"; # 1 print foo(0), "\n"; # 2 print foo(1), "\n"; # 3 print foo(0), "\n"; # 1 print foo(0), "\n"; # 2
|
|---|