Learn how to open and read data files with Perl

Learn Perl at
Developer's Daily Perl Department


Summary

This article teaches the process of opening a text file for read access using Perl, and then demonstrates a simple way to read the contents of the file.


Introduction

In the process of learning Perl, you'll see that in many applications you need to be able to read data from files. The ability to read data from data files is a major first step forward if you're trying to create interactive Perl/CGI applications for the Internet.

In this article we'll show you how to easily open a file and read data from the file with Perl. As you'll see, the creators of Perl thought this was a fundamental activity, and they've 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 open() function

Data files are opened 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.

As an 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".


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 file:

$record = <CHECKBOOK>;

Pretty simple, eh? After this statement is executed, the variable $record contains the contents of the first line of the checkbook file. The "<>" symbol is called the 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 wanted 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.

Here are a few other thoughts to help make this Perl article complete:


Another syntax to use when opening a file

Here's another form of the 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 symbolizes that we want to read from the file. In our experience, it just 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.


Return to
Developer's Daily Perl Department


Read the BLOG

Copyright © 1998 DevDaily Interactive, Inc.
All Rights Reserved.