Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

by princepawn (Parson)
on Oct 02, 2007 at 17:28 UTC ( [id://642178]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^3: How to write a program to read stdin or command line?
by perrin (Chancellor) on Oct 05, 2007 at 02:44 UTC
    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.
Re^3: How to write a program to read stdin or command line?
by runrig (Abbot) on Oct 02, 2007 at 17:41 UTC
    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://642178]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-24 04:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found