Perl files FAQ: How do I open and read files in Perl?
This article demonstrates how to open and read files in Perl. As you’ll see, the creators of Perl thought this was a fundamental activity, and they made it as easy as possible. (That’s one of the things I really like about Perl: many of the routine, daily programming tasks have been made very easy with this language.)
If you’ve used other “structured” languages, such as C or FORTRAN, you’ll appreciate how easy it is to open a file and create a loop to read data from the file.
The Perl open function
You “open” files in Perl using the open
function. When you open a data file, all you have to do is specify (a) a file handle and (b) the name of the file you want to read from.
For example, suppose you need to read some data from a file named checkbook.txt. Here’s a simple open
statement that opens the checkbook file for read access:
open (CHECKBOOK, "checkbook.txt");
In this example, the name CHECKBOOK
is the file handle that you’ll use later when reading from the checkbook.txt data file. Any time you want to read data from the checkbook file, just use the file handle named CHECKBOOK
.
Perl file reading example: Opening a data file and reading from it
Now that we’ve opened the checkbook file, we’d like to be able to read what’s in it. Here’s how to read one line of data from the checkbook.txt file:
$record = <CHECKBOOK>;
After this statement is executed, the variable $record
contains the contents of the first line of the checkbook file. The <>
symbol is called the Perl line reading operator, and in this example we’ve put the checkbook file handle in the line reading operator, indicating that we’d like to read a line from the checkbook file.
Of course, instead of reading just one line of data, you may want to operate on many lines of data in the checkbook file. Suppose, for example, you want to print every record of information from the checkbook file. Here’s the code to (a) open the checkbook file, (b) print each record from the file, and (c) close the file when you’re finished working with it:
open (CHECKBOOK, "checkbook.txt"); while ($record = <CHECKBOOK>) { print $record; } close(CHECKBOOK);
Notice the use of the close()
statement in this example. You always want to close a file when you’re finished reading from it, and since the while
loop reads through the entire file, there’s not much else to do when you’re finished except close it.
Another Perl syntax to use when opening a file
Here’s another form of Perl’s open
command I could have used when opening the checkbook file:
open (CHECKBOOK, "<checkbook.txt");
The only difference between this form and the first form shown above is the use of the <
symbol. Either form is perfectly correct, and both symbolize that we want to read from the file. In my experience, it seems that the first format is more popular with most developers.
What if the file doesn’t exist, or I don’t have read permission?
Suppose the file you’re trying to open doesn’t exist for some reason — how do you handle this situation using Perl? If you try to open a file that doesn’t exist, the open()
statement will fail.
How you handle this situation depends entirely on the Perl program you’re creating, but for our simple example, there’s not much else to do but stop the program. Here’s the code that handles the situation where a file may not exist:
open (CHECKBOOK, "checkbook.txt") || die "couldn't open the file!"; while ($record = <CHECKBOOK>) { print $record; } close(CHECKBOOK);
The Perl language includes the eloquently-named die
function for cases just as this. If you can’t open the needed data file, what should you do? The die
function gracefully terminates the program, and prints your error message to standard output.