It is not allowed to optionally create a variable in an if statement.
You're probably referring to this piece of documentation (discussed in e.g. Variable Scope):
NOTE: The behaviour of a my, state, or our modified with a statement modifier conditional or loop construct (for example, my $x if ...) is undefined. The value of the my variable may be undef, any previously assigned value, or possibly anything else. Don't rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.
However, if you are referring to syntax like if (open my $fh, '<', 'file.txt'), this is perfectly legal; from Private Variables via my():
the scope of $answer extends from its declaration through the rest of that conditional, including any elsif and else clauses, but not beyond it.if ((my $answer = <STDIN>) =~ /^yes$/i) { user_agrees(); } elsif ($answer =~ /^no$/i) { user_disagrees(); } else { chomp $answer; die "'$answer' is neither 'yes' nor 'no'"; }
One very common case of this kind of scoping being while (my $line = <>).
In reply to Re^2: First attempt at bringing in file for input/output
by haukex
in thread First attempt at bringing in file for input/output
by catfish1116
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |