To get input from either STDIN or a file, use a variable to store the file handle, rather than using FH or STDIN directly. That way you can use the exact same code to handle both cases - reading from STDIN and reading from a file.
Additionally, I would stay away from slurping, i.e. reading everything into an array at once: @hosts = <STDIN>. It doesn't scale well and even for small data sets, it doesn't give you much opportunity to clean up the data before you put it in the @host array. For example, you might want to get rid of blank lines or trailing new lines.
use strict;
use warnings;
# set input file handle
my $fh;
if (scalar(@ARGV)) {
# Note: 3 parameter open - this is much safer.
my $file = $ARGV[0];
open($fh, '<', $file) or die "Failed to open file $file: $!\n";
} else {
$fh = \*STDIN;
}
# read in the data
my @hosts;
while (my $line=<$fh>) {
# get rid of trailing new line on each hosts name
chomp $line;
# skip blank lines
next unless $line;
# add host to list of hosts
push @hosts, $line;
}
# print it results
if (scalar(@hosts)) {
# we have input so print it out
local $"='|';
print "hosts found: |@hosts|\n";
} else {
# if @hosts is empty it isn't exactly a syntax error. Users will
# be confused if you *only* print out the usage statement.
if (scalar(@ARGV)) {
# we read from a file but it was empty
printf STDERR "file $ARGV[0] was empty!\n";
} else {
# oops no input! and we read from STDIN
printf STDERR "I expected input from STDIN and got nothing!\n";
}
die <<__USAGE__;
usage: do_something [file]
...
__USAGE__
}
To play with this script and see how it handles STDIN you can either run it by piping data ( echo -e "a\nb\nc\n" | perl MyScript.pm ) or by just running perl MyScript.pm). If you do it the second way, you must stop entering input using Ctrl-D on Linux. If you use Ctrl-C (like I sometimes do), the script will abort before it ever prints out the list of hosts at the end.
Best, beth
Update: Forgot to include usage statement for case when there is no data - added.
Update 2: Changed error message to clarify nature of the problem. |