As others have said, while (chomp( my $line = <STDIN>)){} doesn't work because chomp returns the number of characters removed. A "blank" input line will have at least the line ending characters - chomp() will remove them and return how many. So you will never get 0 (zero), even for a "blank" line.

For terminal user input, I recommend following normal unix conventions: any white space in front does not matter and any white space after the input does not matter. You do not need chomp() when using s/\s*$//; because all line endings are considered white space.

I strongly prefer to put the condition that "ends the loop" in the loop conditional instead of burying it somewhere within the loop's body. With the "comma operator", the only part of the statement that matters for "truthfulness" is the last clause. If there are say 9 things that can end the loop, I would try to put the very most common of those in the conditional and leave the other 8 to be within the loop (e.g. last if XYZ;).

For terminal input like this, consider the following 2 examples:

First, simple Blank Line to exit:

#!/usr/bin/perl use strict; use warnings; my $line; # don't put a my declaration in a loop conditional print "Enter data at prompt, blank line to finish data entry\n"; while ( (print "> "), $line = <STDIN>, $line !~ /^\s*$/) { $line =~ s/^\s*//; # delete leading spaces $line =~ s/\s*$//; # delete trailing spaces # no chomp is needed because both # carriage return and line feed are # white space characters (\s) in Perl # regex lingo print "Data processed:$line\n"; } print "LOOP EXITED DUE TO BLANK LINE\n";
Second, using QUIT, quit or any variation like QuIt to quit:
#!/usr/bin/perl use strict; use warnings; my $line; # don't put a my declaration in a loop conditional print "Enter data at prompt or type: quit to stop data entry\n"; while ( (print "> "), $line = <STDIN>, $line !~ /^\s*Q(uit)$/i) { $line =~ s/^\s*//; # delete leading spaces $line =~ s/\s*$//; # delete trailing spaces # no chomp is needed because both # carriage return and line feed are # white space characters (\s) in Perl # regex lingo print "Data processed:$line\n"; } print "LOOP EXITED DUE TO QUIT \n";
update: changed typo goof in the # comments of \w to \s.

In reply to Re: Empty STDIN does not exit while loop by Marshall
in thread Empty STDIN does not exit while loop by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.