Thanks for the help drieux.

I could have been more explicit in my question to have stated that
I want perl.script to exit quietly if there are no files on the 
command line or if not invoked as the recipient of piped output.

I tested your code (I named the file test.input.source) and ...

Case 1. a file is specified - works as desired.
> test.input.source test.input.source 
file test.input.source had 59 number of lines
we saw 59 number of lines

Case 2. a file is cat'd to test.input.source - works as desired.
> cat test.input.source |test.input.source
no command line args - switching to STDIN
STDIN had 59 number of lines
we saw 59 number of lines
>

Case 3. (this is the difficult case for me) the script is invoked
with no file and no pipe to it.  I would like the script to 
end quietly, such as
>test.input.source
>

Instead, it waits for input.

> test.input.source                       
no command line args - switching to STDIN


and now it waits forever.


So I do not want to explicitly open STDIN if there is not a pipe 
already waiting to send me data.

It just dawned on me that I may not be using the correct terminology
since "pipe" and "STDIN" probably imply much more than I mean for them 
to convey.

I hope this is more clear.  And again, thanks for your help.

 - Paul

-----Original Message-----
From: drieux [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, January 06, 2004 11:20 AM
To: Perl Beginners Mailing List
Subject: Re: What is the source of my input, file or STDIN?



On Jan 6, 2004, at 9:32 AM, [EMAIL PROTECTED] wrote:
[..]
> So, 1. from within perl.script, how can one tell if the input stream
is
> coming from
> STDIN or a file that was opened by <>?
>
> 2. If input stream is not coming from STDIN, but a file, how can one
> tell which file is the current file (assuming multiple files were
> specified on the command line)?

This is gonna sound a bit silly, so laugh along
with me while I play this out.

If you want to know which is the whom For What,
why not simply code it that way?

As an illustration:
<http://www.wetware.com/drieux/pbl/perlTrick/CommandLine/ 
file_or_stdin.plx>

In this case I will walk the @ARGV if there is
anything to walk, test that the file exists,
then pass it off to a count_lines() function,
which will open it and count the number of lines.

If there are no arguments at the command line,
then we will call count_lines() without a file name.

On the inside of the count_lines() function is the trick

        $file ||= '-'; # we open STDIN unless file

either we were passed a file name to open, or we
will set $file to '-' so that it will read from STDIN.

cf: perldoc -f open


ciao
drieux

---


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to