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.
Count the number of lines and bytes in a series of files. If no file is specified, count from standard input (like wc does).
Code:
#!/usr/local/bin/perl
# file: wc.pl
($bytes,$lines) = (0,0);
while (<>) {
$bytes += length($_);
$lines++;
}
print "LINES: $lines\n";
print "BYTES: $bytes\n"; |
Output:
(~/grant) 79% wc.pl progress.txt LINES: 102 BYTES: 5688 (~/grant) 80% wc.pl progress.txt resources.txt specific_aims LINES: 481 BYTES: 24733
Previous
Contents ![]()
Next ![]()
Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory Last modified: Thu Oct 12 22:23:38 EDT 2000