The Magic of <>

The bare <> function when used without any explicit filehandle is magical. It reads from each of the files on the command line as if they were one single large file. If no file is given on the command line, then <> reads from standard input.

This sounds weird, but it is extremely useful.

A Practical Example of <>

Count the number of lines and bytes in a series of files. If no file is specified, count from standard input (like wc does).


  #!/usr/local/bin/perl
  # file: wc.pl
  ($bytes,$lines) = (0,0);

  while (<>) {
    $bytes += length($_);
    $lines++;
  }

  print "LINES: $lines\n";
  print "BYTES: $bytes\n";

Because <> uses open internally, you can use "-" to indicate standard input, or the pipe symbol to indicate a command pipe to create.

Manipulating How <> Works

<> takes its file names from the @ARGV array. You can modify @ARGV before calling <>. This will add the file defaults.txt in front of the @ARGV array, making <> read it before any other files:

unshift 'defaults.txt',@ARGV;
while (<>) {
  ... etc.

This example will detect all gzipped files on the command line and turn them into a gunzip -c pipe.

foreach (@ARGV) {
  $_ = "gunzip -c $_ |" if /\.gz$/;  # uses a pattern match
}
while (<>) {
  ... etc.

Another example: The GET program fetches any Web URL across the network, returning the document's text. This fragment changes filenames that look like URLs into GET pipes:

foreach (@ARGV) {
  $_ = "GET $_ |" if /^(http|ftp|gopher):/;  # another pattern match
}
while (<>) {
  $lines++
}
This lets you run the wc.pl program on a mixture of files and URLs:
(~/grant) 90% wc.pl progress.txt http://www.nsf.gov/guidelines/rfa990192.html

ARGV and $ARGV

While performing <> processing, Perl sets the scalar $ARGV to the name of the current file (or "-" if processing standard input), and ARGV to the current filehandle. You can refer to these explicitly if you want.


<< Previous
Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Tue Oct 12 08:21:00 EDT 1999