next up previous contents
Next: File Test Operators Up: Some Files Are Standard Previous: Some Files Are Standard

Using the Diamond Operator (<>)

If no file handle is used with the diamond operator, Perl will examine the @ARGV special variable. If @ARGV has no elements, then the diamond operator will read from STDIN-either from the keyboard or from a redirected file. So, if you wanted to display the contents of more than one file, you could use the program shown, diamond.pl:

while (<>) {

    print();

}

The command line to run the program might look like this:

perl -w 09lst02.pl 09lst01.pl 09lst02.pl

And the output would be:

while (<STDIN>) {

    print();

}

while (<>) {

    print();

}

Perl will create the @ARGV array from the command line. Each file name on the command line-after the program name-will be added to the @ARGV array as an element. When the program runs the diamond operator starts reading from the file name in the first element of the array. When that entire file has been read, the next file is read from, and so on, until all of the elements have been used. When the last file has be finished, the while loop will end.

Using the diamond operator to iterate over a list of file names is very handy. You can use it in the middle of your program by explicitly assigning a list of file names to the @ARGV array. The listing below shows what this might look like in a program, file.pl.

@ARGV = ("diamond.pl", "stdin.pl");

while (<>) {

    print();

}

This program displays:

while (<STDIN>) {

    print();

}

while (<>) {

    print();

}

Next, we will take a look at the ways that Perl lets you test files, and following that, the functions that can be used with files.


next up previous contents
Next: File Test Operators Up: Some Files Are Standard Previous: Some Files Are Standard
dave@cs.cf.ac.uk