How to determine if input was pipe'd
Quote:
>I am writing a script which acts upon the output of another program, which may
>be supplied to my script either as a filename (containing the data), or may be
>piped through my script--ie, my script has two different calling syntax forms:
> 1) other_prog >output_file
> my_script.pl output_file
>-or-
> 2) other_prog | my_script.pl
>I need to be able to determine that if no filename argument is given, that
>the user has pipe'd the output, or print the correct usage if not. I'm not
>sure how to determine this in Perl & would appreciate suggestions.
From your questions, I assume that you are using "while (<>) {" to iterate
over your input.
When using "<>" to iterate, the variable $ARGV will hold the name of the
current file being read. If you are reading from stdin:
other_prog | my_script.pl
my_script.pl < output_file
$ARGV will be "-". If you are reading from a file it will be the name
of the file.
I don't think there's any easy way to tell where the data is coming from
when reading from stdin. However you can tell if it coming from the user:
if (-t STDIN). This test should only be used if you already know that
the data is coming from stdin ($ARGV eq "-").
You don't have to use <>. Instead you can process the arguments to your
-Dave