in reply to Nicest way of getting STDIN a beauty contest

update: bah, needlessly complicated. Just use <> and check the result.

I'd do it like this:

#!/usr/bin/perl use strict; use warnings; my @hosts; while(<>){ chomp; die "usage: do_something [file]\n" if $ARGV eq '-' and ! $_; next unless $_; push @hosts, $_; } print "Read hosts:\n"; print $_,$/ for @hosts;

That will bail at an empty input line from STDIN. Works with

do_something hosts do_something < hosts echo foo | do_something do_something type type type

Replies are listed 'Best First'.
Re^2: Nicest way of getting STDIN a beauty contest
by mickep76 (Beadle) on Oct 09, 2009 at 09:34 UTC
    It's a nice approach but it still doesn't bail when you simply type...
    do_something

    It will then wait for input until you do CTRL+D. In the first example it will exit immediately if nothing is available in STDIN.

    But doing that either involves using "select" or "IO::Select". Using either if those the solution quickly becomes quite ugly for a quite simple thing.