How to Read and Write Files in Perl

Reading a File in Perl

Next: Writing to a file in Perl

Perl is an ideal language for working with files. It has the basic capability of any shell script, and some very advanced tools, like regular expressions, that make it infinitely more useful. In order to work with files, we first need to learn how to read and write to them. Reading a file is done in Perl by opening a filehandle to a specific resource - in this case a file on our system.

 #!/usr/local/bin/perl
 open (MYFILE, 'data.txt');
 while (<MYFILE>) {
 	chomp;
 	print "$_\n";
 }
 close (MYFILE); 
In order to work with this example, you'll need a file for our Perl script to read. Create a new text document called data.txt and place it in the same directory as the above Perl program. In the file itself, just type in a few names - one per line:
 Larry
 Curly
 Moe 
When you run the script, the output should be the same as the file itself. The script is simply opening the specified file, and looping through it line by line, printing each line as it goes. First we create a filehandle called MYFILE, open it, and point it at our data.txt file.
 open (MYFILE, 'data.txt'); 
Then we use a simple while loop to automatically read each line of the data file one at a time - this places the value of each line in the temporary variable $_ for one loop.
 while (<MYFILE>) { 
Inside the loop, we use the chomp function to clear off the newlines from the end of each line, then we print the value of $_ to show that it was read.
 	chomp;
 	print "$_\n"; 
Finally we close the filehandle to finish out the program.
 close (MYFILE); 
Next: Writing to a file in Perl

Let's take the same data file we worked with while learning to read a file in Perl, and we'll write to it this time. In order to write to a file in Perl, you must open a filehandle and point it at the file you're writing. If you're using Unix, Linux, or a Mac, you might also need to double check your file permissions to see if your Perl script is allowed to write to the data file.

 #!/usr/local/bin/perl
 open (MYFILE, '>>data.txt');
 print MYFILE "Bob\n";
 close (MYFILE); 
If you run this program, then run the program from the previous example on reading a file in Perl, you'll see that it's added one more name to the list.
 Larry
 Curly
 Moe 
In fact, every time you run the program it will add another Bob to the end of the file. This is happening because we opened the file in append mode. To open a file in append more, just prefix the filename with the >> symbol. This tells the open function that you want to write to the file by tacking more onto the end of it. If instead you want to overwrite the existing file with a new one, you can use the > single greater than symbol to tell the open function that you want a fresh file each time. Try replacing the >> with a > and you'll see that your data.txt file is cut down to a single name - Bob - each time you run the program.
 open (MYFILE, '>>data.txt'); 
Next we use the print function to print our new name to the file. You print to a filehandle simply by following the print statement with the filehandle.
 print MYFILE "Bob\n"; 
Finally we close the filehandle to finish out the program.
 close (MYFILE); 
Previous: Reading a file in Perl