in reply to Empty STDIN does not exit while loop
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*
|
|---|
| 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 |