On Thu, 28 Sep 2006, greg wrote:

> To retrieve piped input to my program I can use something like
> ARGF.readlines
>
> However, if there is no piped data, this will wait for input from the
> terminal- how can I avoid this?

you can do something like

   stdin =
     if STDIN.tty?
       ''
     else
       STDIN.read
     end

but that is an extremely bad idea because running the program under cron, or
even using something like

   program &

will cause it to hang

the best/safest approach is to __always__ construct the command-line to give
clear notice that stdin will be following.  the most common way to do this is
to pass '-' on the command line.  for example


   program -
   program --verbosity=4 -

etc.  then, the code is simple and always correct:

   # parse options first

   stdin = ARGV.delete '-'

   if stdin
     buf = STDIN.read
     # ...
   else
     # ...
   end


regards.


-a
-- 
in order to be effective truth must penetrate like an arrow - and that is
likely to hurt. -- wei wu wei