in reply to Re: $_ not set in while <>
in thread $_ not set in while <>
For a while loop, I like to write the main exit condition into the while statement. There could be many ways for the loop to exit past the main, expected condition. However, I code the "most likely" case into the conditional statement.
Example:
In the above, a "my" variable cannot be defined and be used within the same comma statement or logical "and" statement, hence the "my $line;" before the loop conditional.my $line; while ( (print "list of letters: "),$line=<>, $line !~ /\s*quit|exit|q +\s*$/i) {..do something with this line...}
I do recommend using named variables for all while loops that go into production code.
In the above, using "and" instead of the "," would be just fine. The truthfulness of a comma statement only has to do with the value of the last part.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: $_ not set in while <>
by LanX (Saint) on Jun 06, 2021 at 12:46 UTC | |
|
Re^3: $_ not set in while <>
by ikegami (Patriarch) on Jun 06, 2021 at 04:15 UTC | |
by Marshall (Canon) on Jun 06, 2021 at 05:02 UTC | |
by kcott (Archbishop) on Jun 06, 2021 at 06:28 UTC | |
by Marshall (Canon) on Jun 06, 2021 at 07:34 UTC | |
by ikegami (Patriarch) on Jun 09, 2021 at 05:57 UTC | |
by LanX (Saint) on Jun 09, 2021 at 14:15 UTC | |
by ikegami (Patriarch) on Jun 09, 2021 at 19:06 UTC | |
|
Re^3: $_ not set in while <>
by LanX (Saint) on Jun 09, 2021 at 12:35 UTC | |
|
Re^3: $_ not set in while <>
by Anonymous Monk on Jun 08, 2021 at 17:06 UTC |