Perl Read File : Various Ways to Read from a File

perl read filePerl or Practical Extraction and Report language is designed to run on multiple platforms including Unix and windows. Developers who come from a C or Unix shell programming background would find it easy to learn Perl. This popular language offers support for both procedural and object oriented programming. Perl has strong text manipulation capabilities and is well suited for web development. One of its major features is that it can work with markup languages such as HTML, XML and more.

There are multiple ways to read content from a file in Perl. You can use read() or sysread(), along with chomp() depending on how you want to read. But before you read a file, you need to open it correctly. We’ll walk you through  this intermediate level tutorial on Perl read file,that shows you the combinations and how to use them. We assume that you already know how to program in Perl. In case, you want to brush up your basic Perl programming skills, we recommend you to check out this course on the Perl programming language.

First, open a file in Perl

Before we can read a file, we’ll need to open it, in the read mode. Lets see how to do that. A file handle is responsible for associating a physical file with a name. If you’re reading from a user created files, you’ll need to create a file handle based on the file name. Here is the syntax to open a file.

open FILEHANDLE, MODE, <filename>

The first parameter represents the file handle, that’ll link to the buffer where the file data is stored. The MODE specifies which mode to open the file in – read only, write only, read + write. We cover the details of the different modes in our Perl Open tutorial. Alternatively, you can take this course on Linux to better understand various file opening modes. If no MODE is specified, read mode is assumed. Let’s take an example:

open FILE1, "test.txt" 
  or die "Couldn't open file file.txt, $!";

OR

open FILE1, "<", "test.txt" 
  or die "Couldn't open file file.txt, $!";

As the result of the above command, the FILE1 filehandle will be associated with the file test.txt. Then the filehandle can be used to read from the file. However, if the file does not exist or cannot be opened due any reason, the die command will display an error message. It’s always a good practice to include the die command. You want to find possible errors sooner rather than later. Having the die command in place ensures you don’t end up trying to read or write a file that does not exist, or cannot be opened. If this sounds like a new concept, you may first want to take this introductory course to programming to learn the basics.

As we said earlier, there are many ways to read from a file in Perl. Let’s look at the options available.

Reading characters from a File

This can be done using the regular read command. This command reads a specified number of characters from the buffered data. It can be used in two forms – with three or four parameters – in the following ways.

read FILEHANDLE, SCALAR, LENGTH, OFFSET
read FILEHANDLE, SCALAR, LENGTH

Here

FILEHANDLE: is the handle to the file you want to read from

SCALAR: Is the variable you want to save or read those characters into

LENGTH: Is the number of characters you want to read

OFFSET: Is the offset you want to start reading from.

To summarize, this command lets you specify how many characters, you want to read into a certain variable, from a particular offset of a particular file. If no offset is given, it is assumed 0, and we read from the beginning of the file. Now lets try out a few programs.

Example 1 : To read file from start, to end, character by character

open FILE1, "test.txt" or die $!;
my ($buf, $data, $n);
while (($n = read, FILE1, $data, 4) != 0)
{
print "$n bytes read\n";
$buf .= $data;
}
close(FILE1);

The first command opens the file test.txt and gets the file handle. Then, in a loop, the read command reads four characters at a time into the $data variable, and prints the data, and number of bytes read, onto the screen. The while loop keeps on executing until we reach end of file. When the end of file is reached, the while loop terminates. Finally, the close command closes. In general it is a good practice to close a file once you’re done working with it. You don’t want to leave a file open – else it consumes system resources unnecessarily. Learn more about writing your own Perl programs with this course.

Example 2 : To read a file from a specified offset

When offset is specified, the current file read position can be changed within the buffer. If you want to start reading from a particular offset, here’s how you can modify the earlier program

my ($data, $n, $offset);
while (($n = read DATA, $data, 4, $offset) != 0)
{
print "$n bytes read\n";
$offset += $n;
}

Reading lines from a File

In some cases, you may want to read only a line, or a few lines. Let’s look at  two ways to do that

 open(my $file1, "<", "data.txt")
 or die "Could not open file $!";
 my $row = <$file1>;
 print "$row\n";
 print "done\n";

In this example, after opening the file, we just use $row to read in a single line. Alternatively, you can also use @lines to read a single line like this:

my @lines = <FILEHANDLE>;

Example 3: Reading multiple lines from a file

To read multiple lines from a file, we can modify the previous example to loop on the lines. Let’s see how this can be done:

 open(my $file1, "<", "data.txt")
 or die "Could not open file $!";
 while (my $row = <$file1>) {
 chomp $row;
 print "$row\n";
 }
 print "done\n";

Note how we’ve used the while loop here, as well as the use of the chomp() function to truncate extra trailing characters.

We hope this gives you a good understand of some of the commonly used ways to read files in Perl. You may want to also check out how it’s done in other scripting languages like Bash scripting and Python, with the help of this course.