Free Perl Tutorials

How to open a file in PERL

Opening a file in PERL is extremely easy! Creating a file in perl is easy! Appending a file in PERL is rediculously easy! You'll be like me when I first figured it out, and be like, "I had no idea how easy it is to open, create, and append a file using PERL!". First, if you don't know how to test a file, read how to test a perl file.

So lets hop to it!

OPENING A FILE IN PERL

1) First we need to create a file to open, so lets create a simple text file called "text.txt" and save it to our C:\test folder.
2) I just put in the following text into text.txt:
This is line 1
This is line 2

3) Now we need perl to open this file, and print what is in the file. Here is the perl code to open a file:

#First, the PERL code that opens the file
open (FILE, "text.pl");

#Next, our code which will print the file contents
print ;

#Last, we close the file!
close (FILE);


Upon testing the code in your command prompt, you should get the result:
This is line 1
This is line 2

What are we telling the computer to do? First off, FILE is a unqiue item called the "handle" in PERL. In a way, it acts like a normal variable. So in opening the file in PERL, we are calling the file handle "FILE", which PERL will reference throughout the script. Then we place a comma, and in quotation marks, put the name of the file to open "text.txt". NOTE! We only have to put "text.txt" instead of the full file path, because our test PERL file is located in the same folder on our computer. If it wasn't, you would have to include the full path to the file you want to open.

The second and third lines self explanatory, the 2nd prints the file, and the last line closes the file.

CREATING A FILE IN PERL
Creating a file in perl is crazy easy. You take almost the exact same code we just used:

open (FILE, ">hey.txt");
close (FILE);


Can you tell the difference between opening a file and creating a file in PERL? I'll give you a hint, to create a file in perl, you simply add the ">" character in front of the file name you wish to create. If you thought that was easy, wait until you see how to append a file using PERL! Before we do that though, let's take this one step further, and add some text to our new file that we just created!


open (FILE, ">hey.txt"
print FILE "This is line 1\n";
print FILE "This is line 2\n";
close (FILE);


Now if you open your "hey.txt" perl file, it will have that text in there! An important feature of the ">" or create file function, is:
1) If there IS NO "hey.txt" file, it creates it for you.
2) If there IS a "hey.txt" file, it erases the old one and creates a new file called "hey.txt".
So what if you want to just add stuff to a file rather than create a new file in PERL?

HOW TO APPEND A FILE IN PERL

Piece of cake, there is one very minor change, and I'll use the code from our last example to demonstrate:

open (FILE, ">>hey.txt"
print FILE "This is line 1\n";
print FILE "This is line 2\n";
close (FILE);


Can you tell the difference? Its simple, in the create file function in PERL where you use the ">", in the append file function in PERL you would instead use two, or ">>". Give it a try, and check out your code!

Easy Right?

There is another important function you need to be aware of that deals with multiple people being able to open a single file at once called the flock function. You can loose data if you aren't careful, and the file has multiple occurences open. So the "flock" function in PERL only allows one user to open the a file at a time. Here is how to use the flock function in PERL