Mike Gossland's Perl Tutorial Course for Windows


IO Introduction | STDOUT | Writing to Files | Reading from Files | Reading Directories | Editing File Contents | Recursive Editing


Chapter 4. Handling Files in Perl

Writing Files

Writing to STDOUT is very common, and using the redirection operator allows you to save output to a different file. But what if you want the output to go straight into a particular file from within your program?

The answer is you can specify the file you want to use by opening a filehandle to it. Then you use the new filehandle in your print statement, instead of the default STDOUT filehandle. When you're finished, you close the filehandle. It goes like this:

open OUTPUT, ">output.txt";
print OUTPUT "The quick brown fox\n";
close OUTPUT;

In the above code, OUTPUT is the new filehandle. You can name a filehandle anything you like. Instead of OUTPUT, we could have used FILE, DEST, OUT, SAVE, or any other non-reserved name. By convention, a filehandle is written in upper case, but it doesn't have to be.

We specify that we are opening a file for writing with the ">" sign in the quote:

">output.txt"

The ">" symbol is a mnemonic to indicate something is going into the file; in other words we are writing into it.

Lastly, we specify the destination file by naming the file in the open statement. In this example it is "output.txt", but any valid path would work. 

Note there is no comma after the filehandle in the print statement. It is a common error to include one.

When you name a file in an "open for writing" statement, the file may or may not exist before opening. If it does exist first, it is wiped clean as soon as it's opened. If it does not exist first, it is created with no contents. Either way, you start with a clean slate as you begin writing into the file. For this reason, opening files for writing is considered "dangerous" because you can wipe a file out inadvertently if you open it for writing by accident.

As another example, consider this:

open HTML, ">c:/web/root/index.html";
print HTML "Content-Type: text/html\n\n";
print HTML "<html><head></head><body>";
print HTML "<h2>Written by Perl!</h2>";
print HTML "</body></html>";
close HTML;

You've just created a new web page, saved in the file: c:/web/root/index.html.

Re-opening STDOUT

It is quite permissible to re-open existing filehandles. When you do, the filehandle is first closed, and the new one is opened. This works with STDOUT too. You can re-open STDOUT, and then use the print statement to write to your new file by default.

open STDOUT, ">output.txt";
print "The quick brown fox\n";
close STDOUT;

As you see, STDOUT is now going to output.txt, and there's no need to mention it in the print statement.

Appending to Files

If you want to append data onto the end of an existing file instead of writing new content from scratch, then you open the file with ">>" instead of ">".

open LOG, ">>log.txt";
print LOG "Here is a new log entry\n";
close LOG;

Any information you print now will be added on the the end of the file.

Continue on to the next section to learn about how to read information in from files.