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__ }