in reply to Re^3: How to accept ARG from PIPE or STDIN?
in thread How to accept ARG from PIPE or STDIN?
Then you can do something like:
my $file = $ARGV[0]; if (defined $file) { open(IN, "< $file") or die("Unable to open supplied argument: $!\n"); } else { *IN = *STDIN; } while (<IN>) { print($_); # do stuff }
or with some magic:
my $file = @ARGV ? $ARGV[0] : '-'; open(IN, "< $file") or die("Unable to open input file: $!\n"); while (<IN>) { print($_); # do stuff }
or the OO way:
use FileHandle; my $file = $ARGV[0]; my $fh_in; if (defined($file)) { $fh_in = FileHandle->new($file, 'r') or die("Unable to open supplied argument: $!\n"); } else { $fh_in = *STDIN{IO}; } while (<$fh_in>) { print($_); # do stuff }
|
|---|