http://qs321.pair.com?node_id=642178


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