in reply to Why is it uninitialized?

In [Perl Best Practices] from Damian Conway, there is an entry 'Non-Lexical Loop Iterators' in chapter 6 warning about this construct.

Without my, Perl does not use the variable $S4 for the loop, but instead uses a new lexically variable also named $S4. Thats why the first variable $S4 is uninitialized in sub X.

So your code behaves like this:

#!env perl use strict; use warnings; my $S4; sub X { print "<$S4>\n"; return $S4; } for my $other_variable_also_named_S4 (1 .. 2) { print "A: <$other_variable_also_named_S4> <", X(), ">\n"; }

Conway warns to use this construct since its 'behaviour is contrary to all reasonable expectation'.

BTW: perlcritic warns about it too.

Replies are listed 'Best First'.
Re^2: Why is it uninitialized?
by dsheroh (Monsignor) on Dec 21, 2017 at 08:16 UTC
    BTW: perlcritic warns about it too.

    But, then, that's hardly surprising, given that "Perl::Critic is distributed with a number of Perl::Critic::Policy modules that attempt to enforce various coding guidelines. Most Policy modules are based on Damian Conway's book Perl Best Practices."