The closest thing I can find in the documentation is in perlsub:
A "my" has both a compile-time and a run-time effect. At compile time, the compiler takes notice of it. The principal usefulness of this is to quiet "use strict 'vars'", but it is also essential for generation of closures as detailed in perlref. Actual initialization is delayed until run time, though, so it gets executed at the appropriate time, such as each time through a loop, for example.
The section doesn't make it entirely obvious what's going on, and there are certainly no examples, but it is the explanation for why you're seeing what you are.
The canonical example is use as a static variable: sub foo { my $foo if 0; $foo++ }. perl sees the declaration at compile-time, so space is allocated for it, and it passes strict. But at run-time the variable is never initialized (my $foo; being equivalent to my $foo = undef;), so the value is left as it was.
This misfeature has been discussed several times on the perl5-porters mailing list. The last time I saw it there was resistance to documenting it in any form lest it be read as an official sanction for the syntax. It is, after all, a method of declaring a static variable, which can be useful.
In case anyone is curious, the proper way to declare a static is to use a closure:
{ my $foo; sub foo { $foo++ } } ... foo(); foo(); ...
In reply to Re: Conditional initialisation
by Somni
in thread Conditional initialisation
by Akhasha
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |