How to Read and Write Files in Perl

Learn How to Read and Write a File in Perl

Perl is an ideal language for working with files. It has the basic capability of any shell script and advanced tools, such as regular expressions, that make it useful. In order to work with Perl files, you 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.

Reading a File in Perl

In order to work with the example in this article, you'll need a file for the Perl script to read.

Create a new text document called  data.txt and place it in the same directory as the Perl program below.

 #!/usr/local/bin/perl
 open (MYFILE, 'data.txt');
 while () {
 	chomp;
 	print "$_\n";
 }
 close (MYFILE); 

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.

Next, create a filehandle called MYFILE, open it, and point it at the data.txt file.

 open (MYFILE, 'data.txt'); 

Then 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 () { 

Inside the loop, use the chomp function to clear off the newlines from the end of each line and then print the value of $_ to show that it was read.

 chomp;
 print "$_\n"; 

Finally, close the filehandle to finish out the program.

 close (MYFILE); 

Writing to a File in Perl

Take the same data file you worked with while learning to read a file in Perll. This time, you will write to it. 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 and then run the program from the previous section on reading a file in Perl, you'll see that it added one more name to the list.

 Larry
 Curly
 Moe 
 Bob

In fact, every time you run the program, it adds another "Bob" to the end of the file. This is happening because the file was opened in append mode. To open a file in append mode, 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 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 see that the data.txt file is cut down to a single name—Bob—each time you run the program.

 open (MYFILE, '>>data.txt'); 

Next, use the print function to print the new name to the file. You print to a filehandle by following the print statement with the filehandle.

 print MYFILE "Bob\n"; 

Finally, close the filehandle to finish out the program.

 close (MYFILE); 
Format
mla apa chicago
Your Citation
Brown, Kirk. "How to Read and Write Files in Perl." ThoughtCo, Aug. 20, 2017, thoughtco.com/read-and-write-files-in-perl-2641155. Brown, Kirk. (2017, August 20). How to Read and Write Files in Perl. Retrieved from https://www.thoughtco.com/read-and-write-files-in-perl-2641155 Brown, Kirk. "How to Read and Write Files in Perl." ThoughtCo. https://www.thoughtco.com/read-and-write-files-in-perl-2641155 (accessed November 21, 2017).