How to Read and Write Files in Perl

Boy student programming at computer in dark classroom

Caiaimage/Robert Daly/Getty Images

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.

In the file itself, just type in a few names — one per line:

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.

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.

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.

Finally, close the filehandle to finish out the program.

Writing to a File in Perl

Take the same data file you worked with while learning to read a file in Perl. 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.

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.

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.

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.

Finally, close the filehandle to finish out the program.

mla apa chicago
Your Citation
Brown, Kirk. "How to Read and Write Files in Perl." ThoughtCo, Aug. 25, 2020, Brown, Kirk. (2020, August 25). How to Read and Write Files in Perl. Retrieved from Brown, Kirk. "How to Read and Write Files in Perl." ThoughtCo. (accessed May 31, 2023).