Using perl’s ARGV file handle

The perl language has a number of useful tricks that are designed to make common programming tasks easy. One such is the "<ARGV>" file handle.

This handle is pre-defined for you when your script starts, and it can be used to read the contents of all the files specified on the script command line, in the order they are specified. It means you don't have to worry about parsing the command line or opening and closing the files in the script code.

The following very simple script does what unix's "cat" command does..

#! /usr/bin/perl
print $_ while (<ARGV>);

You can call this script like this..

./cat.pl file1 file2 file3

Here's an updated version that translates all characters in the input files into upper case.

#! /usr/bin/perl
print uc($_) while (<ARGV>);

Dont confuse  the "<ARGV>" handle with the @ARGV array, which contains a copy of the strings specified on the command line only.

Scroll to Top