I prefer not to use while(1) type loops for anything other than say in a server loop. For a command loop I prefer to put the prompt and the exit condition test right there in the loop conditional. You can modify the regex for "quit" or whatever. Here the loop exits on a blank line - is that what you wanted?
#!/usr/bin/perl -w
use strict;
my $cmd;
while ((print "enter cmd: "), $cmd=<STDIN> and $cmd !~/^\s*$/)
{
chomp $cmd;
print "cmd was: $cmd\n";
}
print "loop exited\n";
Update: Perhaps you wanted to loop until the first non-blank line was entered?
my $cmd="";
until ($cmd !~ /^\s*$/){print "enter cmd: "; $cmd=<STDIN>;}
chomp $cmd;
print "cmd was: $cmd\n";
I guess I'm not so clear about exactly you want to do.
Basically, I try to construct these loops such that the "last;" statement if any, is not the main, "normal" way that the loop exits. I use that to mean, "oh, we are quitting abnormally early" for some reason - maybe finding the first match or something like that.
If you are wondering why I used a regex, normally leading/trailing blanks on user input are ignored and I would have some statement to trim these instead of just a simple chomp; Anyway 3 spaces and one tab should be counted as just a "whitespace" line.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.