How do I know whether a user specify STDIN or command invocation argument filename 
Author Message
 How do I know whether a user specify STDIN or command invocation argument filename

I am writing a perl program similar like wc in UNIX.  How do I know
whether the user provide input via <STDIN> (redirect a file from the
command line) or via specifying one or more files as invocation



Thu, 07 Apr 2005 06:14:58 GMT  
 How do I know whether a user specify STDIN or command invocation argument filename

Quote:

>I am writing a perl program similar like wc in UNIX.  How do I know
>whether the user provide input via <STDIN> (redirect a file from the
>command line) or via specifying one or more files as invocation


Count the number of files on the command line. If its zero, use stdin.

gtoomey



Thu, 07 Apr 2005 06:36:57 GMT  
 How do I know whether a user specify STDIN or command invocation argument filename

Quote:

> I am writing a perl program similar like wc in UNIX.  How do I know
> whether the user provide input via <STDIN> (redirect a file from the
> command line) or via specifying one or more files as invocation


You don't need to know if you use the <> special null filehandle.

perldoc perlop
[snip]
       The null filehandle <> is special: it can be used to
       emulate the behavior of sed and awk.  Input from <> comes
       either from standard input, or from each file listed on
       the command line.  Here's how it works: the first time <>

       empty, `$ARGV[0]' is set to "-", which when opened gives

       a list of filenames.

John
--
use Perl;
program
fulfillment



Thu, 07 Apr 2005 06:48:48 GMT  
 How do I know whether a user specify STDIN or command invocation argument filename


Quote:

> > I am writing a perl program similar like wc in UNIX.  How do I know
> > whether the user provide input via <STDIN> (redirect a file from the
> > command line) or via specifying one or more files as invocation

> You don't need to know if you use the <> special null filehandle.

> perldoc perlop
> [snip]
>        The null filehandle <> is special: it can be used to
>        emulate the behavior of sed and awk.  Input from <> comes
>        either from standard input, or from each file listed on
>        the command line.  Here's how it works: the first time <>

>        empty, `$ARGV[0]' is set to "-", which when opened gives

>        a list of filenames.

> John

What happens if there are no valid filenames on the command line?
  I am thinking both of the case where a filename was listed but didn't
  actually exist and the case where other command line options are
  listed (eg. -p myprinter, action=buy, etc...)

I have always shied away from <> for these reasons.

mike



Thu, 07 Apr 2005 14:16:29 GMT  
 How do I know whether a user specify STDIN or command invocation argument filename

Quote:



>> You don't need to know if you use the <> special null filehandle.
> What happens if there are no valid filenames on the command line?

What happened when you tried it?

Quote:
>   and the case where other command line options are
>   listed (eg. -p myprinter, action=buy, etc...)


--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Thu, 07 Apr 2005 14:31:37 GMT  
 How do I know whether a user specify STDIN or command invocation argument filename

Quote:

> I have always shied away from <> for these reasons.

So just dare to use it and get acquainted with it.  It's one of the most
useful idioms Perl has to offer.

Ciao,
        Harald
--

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
++$sheep while !$asleep;



Thu, 07 Apr 2005 15:04:23 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Saving the state of a form from invocation to invocation

2. Multiline pattern matching with command line invocation

3. perl/tk command line invocation

4. Business days with user specified weekends (eg, Thu Fri instead of Sat Sun)

5. Problem checking if a command line option has been specified using Getopt::Long

6. Specifying *.txt as the input file on the command line

7. Specifying TAB to -F on the command-line

8. don't know if this can be done.to do with splitting up file names

9. howto specify command line options

10. passing arguments to scripts using STDIN

11. Transferring STDIN/STDOUT to user & back

12. debug of user sub with arguments

 

 
Powered by phpBB® Forum Software