in reply to Re: First attempt at bringing in file for input/output
in thread First attempt at bringing in file for input/output
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 = <>).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: First attempt at bringing in file for input/output
by Marshall (Canon) on Oct 24, 2018 at 05:42 UTC | |
by hippo (Archbishop) on Oct 24, 2018 at 08:43 UTC | |
by Marshall (Canon) on Oct 25, 2018 at 18:53 UTC | |
by soonix (Chancellor) on Oct 26, 2018 at 08:15 UTC | |
by Marshall (Canon) on Nov 02, 2018 at 23:37 UTC |