in reply to Delete lines if matched expression
Further to roboticus's reply: DespacitoPerl: Also note that the conditional in a statement like
next if (my $start == 0);
is always true. A new lexical is created in the scope of the if-expression (update: see Update 2 below) and initialized as undefined. It does not exist outside of the if-expression. undef coerces to 0.
Note also that there seem to be some warnings that you are ignoring.c:\@Work\Perl\monks>perl -wMstrict -le "my $x = 999; print 'is it safe?' if (my $x == 0); " "my" variable $x masks earlier declaration in same scope at -e line 1. Use of uninitialized value in numeric eq (==) at -e line 1. is it safe?
Update 1: Just to be clear, this use of a lexical variable and other, similar uses of lexicals in what amount to dead scopes are all semantic errors.
Update 2: "A new lexical is created in the scope of the if-expression ..." This is not true. Because the if-expression in
next if (my $start == 0);
is a statement modifier (see perlsyn), no new scope is created. That (I finally realized) is the point of the "my" variable $x masks earlier declaration in same scope ... warning.
The assignment causes a change in the enclosing scope, so apparently it isn't safe, Dr. Szell!c:\@Work\Perl\monks>perl -wMstrict -le "my $x = 999; print 'is it safe?' if (my $x = 1); print $x; " "my" variable $x masks earlier declaration in same scope at -e line 1. Found = in conditional, should be == at -e line 1. is it safe? 1
Note that a statement of the non-statement-modifier form
if (my $start == 0) { next; }
would create a new scope and would eliminate one warning message.
Either way, however, the statement from the OPed code is still semantically problematic.c:\@Work\Perl\monks>perl -wMstrict -le "my $x = 999; if (my $x = 1) { print 'is it safe?'; } print $x; " Found = in conditional, should be == at -e line 1. is it safe? 999
Update 3: Added link to the Statement Modifiers section in perlsyn.
Give a man a fish: <%-{-{-{-<
|
|---|