Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How to write a program to read stdin or command line?

by runrig (Abbot)
on Oct 02, 2007 at 16:51 UTC ( [id://642166]=note: print w/replies, xml ) Need Help??


in reply to How to write a program to read stdin or command line?

Need better specs. From you description I can't tell if there's no difference between the two (say, just use the '-p' flag on the command line or the <> operator in the code), or if maybe there is some difference where I might need to test @ARGV to see if there are any arguments before processing STDIN.

Replies are listed 'Best First'.
Re^2: How to write a program to read stdin or command line?
by princepawn (Parson) on Oct 02, 2007 at 17:28 UTC
    Need better specs
    Shoot. Right you are. What I meant was "how would you emulate the way that grep receives its file argument. It could be via cat file | grep search_string or it could be via grep search_string file"


    Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality
      This is built-in behavior in Perl. From perlrun:
      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 <> is evaluated, the @ARGV array is checked, and if it is empty, $ARGV[0] is set to "-", which when opened gives you standard input. The @ARGV array is then processed as a list of filenames.
      In that case I would just shift off any required arguments from @ARGV, then process the file list with <> (or manually with open, etc., if I were paranoid about using <>).
        ...just shift off any required arguments from @ARGV...

        Exactly. Or, if it makes sense, use something a little more sophisticated, such as Getopt::Long, to extract them from the @ARGV array.

        A word spoken in Mind will reach its own level, in the objective world, by its own weight

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://642166]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-19 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found