in reply to Re^3: $_ not set in while <>
in thread $_ not set in while <>
Works fine for me:
Update: I guess this could be, using "and" instead of comma.use strict; use warnings; my $line; while ( (print "list of letters: "),$line=<>, $line !~ /\s*quit|exit|q +\s*$/i) { print "doing something with $line\n"} __END__ Example run: C:\Users\xxx\Documents\PerlProjects\Monks>perl commandloopX.pl list of letters: 234 doing something with 234 list of letters: asdf doing something with asdf list of letters: quit C:\Users\xxx\Documents\PerlProjects\Monks>
The comma operator is completely fine in Perl. This is more common in C code.while ( (print "list of letters: ") and defined ($line=<>) and $line ! +~ /\s*quit|exit|q +\s*$/i)
Update: Oh, the parens around the print are required in order to get the prompt onto the screen.
I never use a while(1) statement except perhaps in a server process.
Another Update:
I did run your code. I did require the addition of a "my declaration" for $line.
Your code does work albeit as wordy and hard to understand as it is.
I am sure that there are many Monks who will disagree with style issues!
use strict; use warnings; my $line; while (1) { print "list of letters: "; defined( $line = <> ) or last; $line !~ /\s*quit|exit|q\s*$/i or last; # Do something with $line. } __END__ C:\Users\xxxx\Documents\PerlProjects\Monks>perl commandloopX3.pl list of letters: adf list of letters: 123 list of letters: q
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: $_ not set in while <>
by kcott (Archbishop) on Jun 06, 2021 at 06:28 UTC | |
by Marshall (Canon) on Jun 06, 2021 at 07:34 UTC | |
|
Re^5: $_ not set in while <>
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 |