in reply to Why is it uninitialized?
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 |