Perl Open: Opening Files in Perl with the open () Function

perl open windowPerl is actually one of the most popular programming languages in the world at the moment, even though you won’t hear people talk about it as much as they do about Java or C++.  Even if it’s less well known than its competitors, Perl has been going strong for over 25 years and enjoys a strong community support. The language was primarily designed to help with report processing on UNIX systems. Today, you can do a lot more with Perl than just text processing. Perl is a very popular with some large organizations because of its open source nature- you can modify Perl in any way you like. Some large organizations have, in fact, customized Perl modules. Now, they maintain their own private libraries, which they use for projects or to create applications. Perl is also a good language when it comes to web development. It is often called the “duct tape” of the internet because of its flexible nature and its efficiency at CGI.

If you have studied languages like C++, Java or C before, you’ll find it very easy to learn Perl. Perl has, to some extent, been derived from C. Perl code is easy to pick up and remember, because it resembles standard English. Don’t worry if you’re a programming newbie- Perl is a forgiving language. It’s probably easier to learn Perl as a first language compared to many other modern programming languages (like C++). If you’re looking for learning resources for Perl, sign up for our Perl programming course. We teach you everything you need to know to become a competent Perl programmer.

Opening Files in Perl

We’re going to show you how you can open files in Perl in this basic tutorial with the open () function. The open () function is also used to create files- we’ll give you an overview of that as well. As long as you know how scripts in Perl work and how to execute them, you shouldn’t have a problem with it. If you’re new to programming, you may want to first take this course for absolute beginners.

The open () function, or subroutine, is used to open files in Perl. The general syntax for the function is:

open (filehandle, mode, file_expr)

Here, the filehandle parameter is a unique file handle you want to associate with the file you are trying to open. The mode is the parameter that contains the permissions you want to give yourself for this file. This is an optional parameter, which we will explain in more detail later. The final parameter is the file_expr parameter, which is the name of the file you want to open, along with its location. You also have to specify the type of file you want to open along with the name. If you have navigated to a directory containing the file you want to open, you don’t have to specify the location.

The open function returns a 1 value if it opens the file successfully. If it fails to open the fail, it will return a 0 value. Please note that if you don’t have a file in your system with the name you specified, Perl will create a file with that name.

Examples of the Open () Function

You will understand the open () function better with the help of a few examples. Let’s use the open () function to open an html file called “myhtml.html”:

$file = "filehandle";
$FileName = "myhtml.html";
open(file, $FileName) or die $!;

Looks simple enough, right? We associated the filehandle parameter with “$file”. Now, whenever we want to open the file, we use the “$file” variable. After that, we stored the name of the file in the variable $FileName. Then we used the open function to open the file. Notice that we also used the die function. If the open function fails to open the file, then the script will stop and you will get an appropriate error message because of it. This course can show you how to write your own Perl scripts with ease.

Let’s take a little more complex example. Let’s open a text file, print every line in it and then close it:

use strict;
use warnings;
my $filename = "mynote.txt";
unless(open FILEHAN, $filename) {
die "nCannot open $filenamen";
while(my $lines = <FILEHAN>) {
print $lines;
close FILE;

Output (If the file does NOT open successfully):

Cannot open mynote.txt

Here, we used a string variable to store the text file called “mynote.txt”. Why did we do that? So we can use it with the unless function. Notice that in unless function, we wrote the code to both open the file and to the die function. Finally, we used the while function to loop through all the lines in the file. Each line in the file will get printed once.

Permissions While Opening a File: The Mode Parameter

File permissions help you maintain the integrity of your files if they are specified. They limit what users can do with your file once it is open. The mode parameter we discussed earlier is actually where you set permissions. The modes here are pretty similar to the file operation modes in Linux. You can learn more about Linux file handling with this course.

Here is a list of the possible permissions you can set:

Operand SpecifiedResult
<Open a file in read only mode
>Open a file in write mode. Creates a file with the specified name if it doesn’t exist. Overwrites an existing file with the same name.
>>Append a file. Creates a file if it doesn’t exist.
+<Open a file in read/write mode. Does not create a file if the file name doesn’t exist. Does not delete an existing file.
+>Open a file in read/write mode. Creates a file if it doesn’t exist. Deletes existing file with the same name.
+>>Opens a file in read/write mode for editing. Does not delete an existing file with the same name (it opens that file). Creates a file with the specified name if it doesn’t exist.


To open the file “mynote.txt” in read only mode, for example, you can type:

open FILE, “<”, “mynote.txt” or die $!

Now that you know how to open a file, you can move on to learning about how to rename files, copying them and working with multiple files in Perl.