in reply to Should "use strict" complain if I use a variable that is not declared?

From 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."

use strict 'vars' is checked at compiletime. Yet at compiletime, the compiler cannot know what the condition will be. It has to assume that under some conditions the variable will be declared, and that being the case, it must squelch the stricture complaint.


Dave

Replies are listed 'Best First'.
Re^2: Should "use strict" complain if I use a variable that is not declared?
by earthman (Initiate) on Jun 28, 2010 at 11:21 UTC

    I understand that it does squelch the stricture complaint, but must it? I would suggest that it could, as an alternative, assume that under some conditions the variable might not be declared, and complain. It is supposed to be strict, after all.

      The variable is declared. It might just not be initialized. The two are distinct, and strict is only about declaration.

      Corion explained that it's proper for strict not to give an error, but Perl could certainly emit a warning or throw an error.

      The only complication would be backwards compatibility. People rely on the behaviour of a conditionally executed my even though the docs have long warned against it.

      sub foo { my $x if 0; print ++$x, "\n"; } foo() for 1..3;
      1 2 3

      Note that using the hardcoded zero there warns Deprecated use of my() in false conditional.