... what if my is within the condition of a postfix if? ... is it valid?
This is the way I look at it: The thing to remember is that the modifier clause (if that's the proper terminology) of a modified statement modifies the behavior of the statement. In order to do so, the modifier clause must always be evaluated. The ambiguity in a statement like
my $x if $some_conditional;
concerns whether (and when) the lexical definition is evaluated, but $some_conditional (or whatever expression may be there) must always be evaluated.
In a statement like
0 if my $x = 1;
there is no ambiguity: the lexical is always defined (and, in this example, initialized). Similarly, in the statement
do_something($_) for my @ra = @rb;
the for-loop initialization expression my @ra = @rb must always be evaluated (including the definition and initialization of its lexical) in order that for may be able to control (i.e., modify the behavior of) the statement.
So, is
0 if my $x;
and its ilk valid? Unquestionably (and unambiguously and without deprecation) yes, I would say. (But I'm too lazy right now to dig up a documentation citation.)
Consider the following code. Also consider running | compiling it with -MO=Deparse,-p added (often enlightening in these cases).
c:\@Work\Perl\monks>perl -wMstrict -le
"no warnings 'syntax';
;;
0 if my $x = 42;
print qq{$x};
;;
my @orig = qw(foo bar baz);
tr{a-z}{A-Z} && printf qq{'$_' } for my @copy = @orig;
print '';
print qq{(@orig) (@copy)};
;;
for (0 .. 2) {
die 'Oops...' if my $x;
print qq{\$x (still) undefined here} if not defined $x;
$x = 42;
print qq{\$x is $x here};
}
"
42
'FOO' 'BAR' 'BAZ'
(foo bar baz) (FOO BAR BAZ)
$x (still) undefined here
$x is 42 here
$x (still) undefined here
$x is 42 here
$x (still) undefined here
$x is 42 here
(Also consider running your code with warnings enabled. And strict.)
|