On Jan 28, Naveen Parmar said:

>How do I have Perl script accept input from a file?

Two ways that come to mind:

  while (<>) {
    # this reads from the command-line arguments as filenames
    # or from STDIN if there are no arguments
  }

or

  open FILE, "< $ARGV[0]" # uses the first arg as a filename
    or die "can't read $ARGV[0]: $!";
  while (<FILE>) {
    # ... 
  }
  close FILE;

If either of these were in a program called foo.pl, I could say:

  japhy% perl foo.pl somefile.txt

and both would read from somefile.txt.  The benefit that the FIRST piece
of code has, though, is that I can say

  japhy% perl foo.pl this.txt that.txt those.txt

and read from all three files, in sequence.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to