I have a script that does something like that. It deals with text and can read that text from a file if $ARGV[ 0 ] is a readable file, from @ARGV if present but not a file, from a piped command if there's something to read on STDIN or typed in if not. It uses IO::Select to determine if input is waiting to be read on STDIN and thereby makes the assumption that it is being piped to.
use strict; use warnings; use IO::Select; ... my $text; if ( @ARGV ) { if ( -r $ARGV[ 0 ] ) { $text = do { local $/; <>; }; } else { $text = join q{ }, @ARGV; } } else { my $select = IO::Select->new(); $select->add( \ *STDIN ); print qq{Type text, finish with ctrl-D on its own line\n} unless $select->can_read( 0.05 ); $text = do { local $/; <>; }; } ... # Do something with $text
I hope this is of use.
Cheers,
JohnGG
In reply to Re: How do I Determine if my script is being piped to?
by johngg
in thread How do I Determine if my script is being piped to?
by netguy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |