Perlaccess : Tutorials : File and Directory
Saving Files (4.2)
by Jacob A. Wheeler
http://www.bigresources.com

Everything is pretty straight forward when it comes to saving. Notice the file handle of the opened file "FILE" in the print statement of the text being saved. Here is a piece of sample code:

  1. open (FILE, ">file.txt");
  2. print FILE "Text To be saved\n";
  3. close(FILE);

The above saving code works nice for one time jobs, but what if you want to add a few items to a already started list? The below code will show you how to append to a file:

  1. open (FILE, ">>file.txt");
  2. print FILE "Text To be saved\n";
  3. close(FILE);

Take note of the ">>" located near the filename. This tells perl to open the file for input, and sets the output to append to the file. If you forget to put two <<'s you will write over the file.