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>




Reply via email to