File Manipulation
This document was written by CS 290W TA David Corcoran and was last
modified
In many cases it is useful to open files for storing data on a more
permanent basis. You may also want to read a file that was created
some time earlier. Perl allows you to open a file for reading or
writing using the open() operator.
# Opening a file
open(MYFILE, "myfile.txt"); # Reading
open(MYFILE, "myfile.txt"); # Writing
open(MYFILE, ">>myfile.txt"); # Appending
close(MYFILE); # Close it when finished.
What is happening here is that open() is attempting to create what is
called a stream to a file. A stream is just a channel in which you,
the programmer, can access a file using a handle. In this example the
handle to the stream is MYFILE, although it could be whatever you
choose it to be. Most Perl programmers use all capital letters for a
file handle.
The first example only allows you to read from the file myfile.txt.
The second allows you to write to it. This will overwrite any data
that is currently in the file, so BE CAREFUL! The third appends
information to the end of the file myfile.txt. We should close the
file stream when we are finished by using the close() operator.
We must be careful though. If myfile.txt does not exist in the first
example, you must be able to catch the error. Consider the following
example using the die() operator.
Catching Errors
# Checking the status of the open() operator
unless (open(MYFILE, "myfile.txt"))
{
die "Could not open file - Exiting\n";
}
unless (open(MYFILE, "myfile.txt"))
{
print "Could not open file - Continuing\n";
}
close (MYFILE);
In both examples the unless() operator will allow you to catch any
errors that might occur in the open() operation. These errors might
be anything such as: File Does Not Exist, File Privileges Not Met,
etc.
In the first example the die operator will print out the error message
and immediately exit the program. In the second you will just get an
error message and the program will continue operation.
OK, so we have successfully opened a file but now we want to do some
operations on it. Consider the following for reading from the file.
Reading the File
# Reading from the file
unless (open(MYFILE, "myfile.txt"))
{
die "Could not open file - Exiting\n";
}
read(MYFILE, $sFile, 10000);
# Reads every line in the file up to
# 10000 characters
print "$sFile";
# Prints every line in the file including
# their end-of-line characters
close(MYFILE);
We are storing each line into the character string $sFile. We can
then use the String operators such as substr(), split(), chop(),
etc. to parse out those lines if we are looking for certain data
within those lines. Enough with reading, let's talk about writing.
Writing to the File
# Writing to a file
unless (open(MYFILE, ">>myfile.txt"))
{
die "Could not open file - Exiting\n";
}
for ($iCount=0; $iCount < 10; $iCount++)
{
print MYFILE "Record $iCount \n";
}
close (MYFILE);
Here we are opening file myfile.txt for appending. We are then
printing to that file using the print() operator with handle MYFILE.
We are stepping through a for loop 10 times. The output in the file
should look like this:
Record 0
Record 1
Record 2
....
Record 9