in reply to Empty STDIN does not exit while loop

After chomping, check what remains; exit the loop if nothing is found.

last if $line=~/^$/; # or using length..

UPDATE: you can also use last unless $line; to quit the loop, but this breaks if 0 is passed. If 0 is valid you can last unless defined $line; see below Laurent_R.

Additionally if move the chomp inside the loop you can use CTRL-Z to end fidding STDIN

Notice that chomp returns the number of removed chars, not the chomped string (added in the same time of the below answer..). Considering this and avoiding extra variable $line you can have anything in the while condition:

while ( chomp ( $_ = <STDIN>) and length $_){ print " [$_]\n"; }

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Empty STDIN does not exit while loop
by Laurent_R (Canon) on May 20, 2017 at 08:48 UTC
    you can also use last unless $line; to quit the loop, but this breaks if 0 is passed. If 0 is valid you can last unless defined $line;
    The last unless defined $line; statement will not work either, because even if $line is empty, it will still be defined.

    Your original solution, i.e. last if $line=~/^$/; (or length $line ...), is definitely better.