To create a filehandle, call the open
command, with the filehandle as the
first argument, and a character string or variable containing the name of the
file you wish to open.
While you can use any name for a filehandle, many perl programmers use names
composed entirely of capital letters for filehandles, to help distinquish them
from other barewords contained in a program, and as a visual reminder that
filehandles are different from other variables in perl.
By default, perl will open files for reading.
Special symbols, as illustrated in Table ,
can be used to differentiate between opening files for reading or writing, or between
writing to or from a file and writing to or from a pipe. (A pipe reads or
writes to or from another program instead of to or from a file.)
When using the open
function, it's important to remember that not every
request for opening a filehandle is guaranteed to meet with success. If a file
does not exist, you can't open it for reading. Similarly, if a program doesn't
exist, you can't pipe data to or from it. Under operating systems which support
the notion of permissions, a file may exist, but you may not be permitted to
read from or write to it. For these reasons, the open
function returns a value
of 1 if it's successful, and a value of undef
if it fails. This leads
to one of the
most common perl programming constructs, illustrated by the following example:
open(FI,"<$myfile") || die("Couldn't open $myfile");Checking the return value of an open is essential, because the diamond operator will return
undef
when it is operating on a file handle whose
value is undef
, without any other warning. If you examine the code of
most experienced perl programmers, you'll find that every call to open
has a corresponding call to die
like the one in the example above.
The die
function simply prints the message that you provide, along with
the line number on which it occured, and then terminates the program.
A similar function called
warn
prints a message without terminating the program.
On systems like Windows which make a distinction between text and binary files, you must explicitly inform perl after opening a file which you intend to read or write in binary form by passing the filehandle in question to the binfile function.
The close
function can be called with a single argument representing the
filehandle that you want to close. Like most programs, perl will automatically
close files after a program is finished, so it's usually not necessary to explicitly
close files in your perl programs. Additionally, if you open several files using the
same filehandle, perl will close the previously opened file before opening another.
However, if you open many files with different file handles, you may exceed your
operating system's limit for open files, so you may need the close
function
in some cases. In addition, explicitly closing files often makes the logic of
your program a little clearer.
Once you've created a filehandle for reading, simply include its name inside the diamond operator to read from the file in question. The following simple program prompts for a file name, opens the file, and then prints the longest line encountered in the file.
print "Name of file to open? "; $filename = <STDIN>; chomp $filename; open(FI,"<$filename") || die "Couldn't open $filename"; while(<FI>){ if(length > $maxlength){ $maxlength = length; $maxline = $_; } } print "Longest line ($maxlength characters):\n$maxline";