> -----Original Message-----
> From: Andrew G.McArthur [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, August 21, 2001 3:44 PM
> To: [EMAIL PROTECTED]
> Subject: Detecting if a pipe exists
> 
> 
> Dear all,
> 
> I'm developing a perl program in UNIX that I want to 
> sometimes work on 
> data from the pipe, sometimes from the command line, and sometimes 
> both.  For example, consider the hypothetical program 
> "squareroot".  I 
> would like to use it in the following three ways from the 
> command prompt:
> 
>  > squareroot 100 1000 25
> 
>  > cat numbers.list | squareroot
> 
>  > cat numbers.list | squareroot 25 144
> 
> I've had no problem with the second two examples, but the first waits 
> for input from the STDIN.  How can I get my perl program to know that 
> I'm not piping in data from the STDIN?

I don't think this can really be done.

You might use -p to see if STDIN is a pipe, but that wouldn't work for

   squareroot <numbers.list

You might use -t to see if STDIN is a tty, but that wouldn't work for

   $ cat >sq.sh
   squareroot 100
   $ sh sq.sh          # STDIN is a tty, but should I read input?

My recommendation would be to use a -f option to supply input from
a file (use Getopt::Std to parse the argument):

   squareroot -f numbers.list 25 144

You can still use this in a pipeline by using '-' as the filename:

   cat numbers.list | squareroot -f - 25 144
   squareroot -f - 25 144 <numbers.list

perl treats a filename of '-' as STDIN. (see perldoc -f open)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to