Perl Operators   «Prev  Next»
Lesson 14I/O operators
Objective Use I/O operators to work with files in a Perl script.

Perl Input Output Operators

Perl has some special operators for dealing with files.
These operators make it particularly easy to work with files in Perl.
There are three common file actions with which you will want to use these operators:
  1. Opening a file
  2. Reading a file

Writing a file in Perl

You can write to a file with print by specifying the file handle without a comma before the arguments to print:
print FILEHANDLE "This and that to print\n";

This also works with Perl's C-like printf function:
printf FILEHANDLE "%d%c", 42, 0x0a;
For example, this program will copy the input to a file called output.txt:
#!/usr/bin/perl -w
$outfile = "output.txt";
open(OUTFILE, ">$outfile")
 or die "can't create $outfile: $!\n";
while(<>) { print OUTFILE }
close OUTFILE;

We have just introduced the concept of a Directory Handle for referring to a Directory on disk. We now introduce a similar concept of File Handle for referring to a File on disk from which we can read data and to which we can write data. Similar ideas of opening and closing the files exist. You use the open() operator to open a file (for reading):
open(FILEHANDLE,"file_on_device");
To open a file for writing you must use the ``>'' symbol in the open() operator:
open(FILEHANDLE,">outfile");

Write always starts writing to file at the start of the file. If the file already exists and contains data. The file will be opened and the data overwritten. To open a file for appending you must use the ``>>'' symbol in the open() operator:
open(FILEHANDLE,">>appendfile");
The close() operator closes a file handle:
close(FILEHANDLE);
To read from a file you simply use the <FILEHANDLE> command which reads one line at a time from a FILEHANDLE and stores it in a special Perl variable $_. For example, read.pl:
open(FILE,"myfile")
|| die "cannot open file";
while(<FILE>)
{ print $_; # echo line read
}
close(FILE);
To write to a file you use the Print command and simply refer to the FILEHANDLE before you format the output string via:
print FILEHANDLE "Output String\n";
Therefore to read from one file infile and copy line by line to another outfile we could do readwrite.pl:

Perl Read Write - Exercise

Click the Exercise link below to create a Perl script that uses I/O operators to read one file and write it to another.
Perl Read Write - Exercise

Reading an Array from the Standard Input File

In the programs you have seen so far, single lines of input are read from the standard input file and stored in scalar variables. For example:
$var = <STDIN>;

In this case, every appearance of <STDIN> means that another line of input is obtained from the standard input file. Perl also provides a quicker approach: If you assign <STDIN> to an array variable instead of a scalar variable, the Perl interpreter reads in all of the data from the standard input file at once and assigns it. For example, the statement
@array = <STDIN>;

reads everything typed in and assigns it all to the array variable @array. The variable @array now contains a list; each element of the list is a line of input.
Advanced Perl Programming