Questions And Answers

More Tutorials

Perl Files Handling


A filehandle is the name for the connection between your Perl program and the operating system. Filehandles follow the same naming conventions as labels, and occupy their own namespace.

Every Perl program has three filehandles that are automatically opened for it: STDIN, STDOUT, and STDERR:
STDIN Standard input (keyboard or file)
STDOUT Standard output (print and write send output here)
STDERR Standard error (channel for diagnostic output)

Filehandles are created using the open() function:

You can open files for reading, writing, or appending:

open(FILE,”> newout.dat”)
Writing, creating a new file
open(FILE,”>> oldout.dat”)
Appending to existing file
open(FILE,”< input.dat”)
Reading from existing file

As an aside, under Windows, there are a number of ways to refer to the full path to a file:
Escape the backslash in double quotes
Use proper path in single quotes
UNIX-style forward slashes

It is important to realize that calls to the open() function are not always successful. Perl will not (necessarily) complain about using a filehandle created from a failed open().
This is why we test the condition of the open statement:
open(F,”< badfile.dat”) or dieopen: $!”

You may wish to test for the existence of a file or for certain properties before opening it. Fortunately, there are a number of file test operators available:
File test Meaning
-e file File or directory exists
-T file File is a text file
-w file File is writable
-r file File is readable
-s file File exists and has nonzero length

These operators are usually used in a conditional expression:
if (-e myfile.dat) {
open(FILE,”< myfile.dat”) or die “open: $!\n”;

Even more information can be obtained about a given file using the stat() function.

Using filehandles

After a file has been opened for reading you can read from it using the diamond operator just as you have already done for STDIN:
$_ = <FILE>; or
while (<FILE>) {

To print to your open output file, use the filehandle as the first argument to the print statement (N.B. no commas between the filehandle and the string to print).
print FILE “Look Ma! No hands!\n”;

To change the default output filehandle from STDOUT to another one, use select:
select FILE;

From this point on, all calls to print or write without a filehandle argument will result in output being directed to the selected filehandle. Any special variables related to output will also then apply to this new default. To change the default back to STDOUT, select it:
select STDOUT;

When you are finished using a filehandle, close it using close():


In this page (written and validated by ) you learned about Perl Files handling . What's Next? If you are interested in completing Perl tutorial, your next topic will be learning about: Perl Files Format.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.