Pardus has asked for the wisdom of the Perl Monks concerning the following question:

I have a program that I want to be able run in:
  1) interactive mode
  2) non-interactive with input from a commandline option
  3) non-interactive as part of a pipeline

First I parse the commandline args because options can be specified in any of the three cases, and check for case 2. Then I simply test for case 1 by the "-t" operator.
if (-t STDIN && -t STDOUT) {...} # case 1
My problem is this: in order to realize case 3 I should know whether there is a pipeline printing to STDIN or not. If so I obvious need a while (<STDIN>) {...} loop. _But_ using this loop without knowing there is a pipeline will cause the program to hang till it gets an EOF on STDIN (which it obviously won't get).

To summarize: How do I know/test/detect there is a pipeline printing to the STDIN of my program so I can use this input?
--
Jaap Karssenberg || Pardus (Larus)? <pardus@cpan.org>
>>>> Zoidberg: So many memories, so many strange fluids gushing out of patients' bodies.... <<<<

Replies are listed 'Best First'.
Re: detecting a pipeline on STDIN
by pfaut (Priest) on Mar 15, 2003 at 21:18 UTC

    -p STDIN should tell you if STDIN is a pipe. The filetest operators are described in perlfunc.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
      Thx -- I looked it up in the reference guide which only talks about testing for _named_ pipes with "-p". Sorry for asking the obvious :S
      --
      Jaap Karssenberg || Pardus (Larus)? <pardus@cpan.org>
      >>>> Zoidberg: So many memories, so many strange fluids gushing out of patients' bodies.... <<<<
Re: detecting a pipeline on STDIN
by dakkar (Hermit) on Mar 16, 2003 at 18:14 UTC

    You say:

    using this loop without knowing there is a pipeline will cause the program to hang till it gets an EOF on STDIN (which it obviously won't get).

    Duh? This:

    ls|perl -e 'while (<STDIN>) {/1/ and do{print}}'

    works as expected (prints only the lines of the output of ls which contain a 1). OK, you need to do different processing in the case of a pipeline, but you will still get EOF.

    Just nitpicking...

    -- 
            dakkar - Mobilis in mobile