Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
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 ( #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


Comment on Re^2: How to write a program to read stdin or command line?
Select or Download Code
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
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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2014-12-19 04:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (70 votes), past polls