[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to determine if pipe is given

greg

9/27/2006 4:52:00 PM

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?

Thanks,
Greg Weber


35 Answers

Vincent Fourmond

9/27/2006 4:57:00 PM

0

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 check if $stdin is a terminal with

$stdin.tty?

Vince


Ara.T.Howard

9/27/2006 5:03:00 PM

0

greg

9/27/2006 8:27:00 PM

0

thanks, a

This kind of sucks though.

apparently in perl, if there is a piped input, '-' will show up
automatically in ARGV. I think I will propose this change to Ruby.


Ara.T.Howard

9/27/2006 8:43:00 PM

0

Paul Lutus

9/27/2006 8:45:00 PM

0

greg wrote:

> thanks, a
>
> This kind of sucks though.

Why? One performs a test to determine if there is a pipe. That seems
reasonable.

> apparently in perl, if there is a piped input, '-' will show up
> automatically in ARGV.

Have you done an experiment to see if this is true? In fact, the user of the
script must provide this symbol as an argument if he wants the read a pipe,
then the script must interpret the presence of this symbol as a signal to
commence reading a pipe, by the way a very common *nix convention. It's all
very non-automatic.

> I think I will propose this change to Ruby.

No sense doing it in Ruby if it isn't done in Perl. It isn't done in Perl.

--
Paul Lutus
http://www.ara...

Gary Wright

9/27/2006 8:50:00 PM

0


On Sep 27, 2006, at 4:27 PM, greg wrote:

> thanks, a
>
> This kind of sucks though.
>
> apparently in perl, if there is a piped input, '-' will show up
> automatically in ARGV. I think I will propose this change to Ruby.

Normally I would just nod my head in agreement with Ara, but not this
time.

I just have to say yuck with regard to using '-' and the idea that
command lines
should have have an explicit syntax to indicate that the program
should read from
stdin.

If your program is prepared to deal with piped input, why are
you concerned about input coming from the keyboard? If I found a
program
that worked as:

cat file | program

and as

program < file

but did not work as just

program

I would be surprised, to say the least.

Usually the issue is the other way around where an interactive
program (vim, for
example) *needs* a tty device on stdin and may complain when
STDIN.tty? fails, but
I'm not sure I understand the need to complain about STDIN being tied
to a tty device
for a program that is just reading a stream of data.

The '-' hack just makes me shudder. Something like '/dev/stdin'
would be marginally better
if you really need something like that (although not as portable).




Paul Lutus

9/27/2006 8:55:00 PM

0

ara.t.howard@noaa.gov wrote:

/ ...

>> apparently in perl, if there is a piped input, '-' will show up
>> automatically in ARGV. I think I will propose this change to Ruby.
>
> eeeks! that's is pure __evil__!

It also isn't true. IMHO Larry Wall wouldn't countenance such a thing.

--
Paul Lutus
http://www.ara...

Gary Wright

9/27/2006 8:56:00 PM

0


On Sep 27, 2006, at 4:42 PM, ara.t.howard@noaa.gov wrote:
> consider i have many, many programs which do things like
>
> convert infile outfile
> convert - outfile # infile on stdin
> convert infile - # outfile on stdout
> convert - - # infile on stdin, outfile on stdout
> convert --infiles=- # list of infiles on stdin, auto-name
> outfiles

While I understand the historical reason for such constructs, I much
prefer:

convert infile outfile
convert /dev/stdin outfile # infile on stdin
convert infile /dev/stdout # outfile on stdout
convert # infile on stdin, outfile on stdout

> convert --infiles=- # list of infiles on stdin, auto-name
> outfiles

Not sure about this last one. It certainly isn't all that common.
You can get close
to this via:

cat filenames | xargs convert

with maybe an option to convert to cause it to send output to auto-
named outfiles:

cat filenames | xargs convert --inplace

I realize that some Unix systems don't support /dev/stdin, /dev/
stdout, though...


Gary Wright




greg

9/27/2006 9:25:00 PM

0

sorry, misread some perl stuff.

How about a global flag or constant
like $PIPE set to true if there is a pipe, and otherwise set to false?


Paul Lutus

9/27/2006 9:49:00 PM

0

greg wrote:

> sorry, misread some perl stuff.
>
> How about a global flag or constant
> like $PIPE set to true if there is a pipe, and otherwise set to false?

Are you asking whether this is true, or whether it would be a good idea? If
the latter, I personally think stuff like this should be explicitly coded
by the programmer. There are situations where detecting the
presence/absence of a pipe may have side effects best avoided.

Of course, reasonable people may (will) differ.

--
Paul Lutus
http://www.ara...