Text I/O



next up previous
Next: Pipes Up: File Handling Previous: File Handling

Text I/O

Perl reads and writes text files by way of filehandles. By convention, filehandles are usually in upper case.

Files are opened by way of the open command. This command is given two arguments: a filehandle and a file name (the file name may be prefixed with some modifiers). Lines of input are read by evaluating a filehandle inside angled brackets (<...>). Here is an example which reads through a file:

open(F,"data.txt");
while($line = <F>)
{
    # do something interesting with the input
}
close F;

The file name argument can have one of several prefixes. If the file name is prefixed with <, the file is opened for reading. (This is the default action.) If the file name is prefixed with > then it is opened for writing. If the file exists, it is truncated and opened. Finally, a prefix of >> opens the file for appending. Here are a few examples:

# peruse the passwd file
open(PASSWD,"</etc/passwd");
while ($p = <PASSWD>)
{
     chop $p;
     @fields= split(/:/,$p);
     print "$fields[0]'s home directory is $fields[5]\n";
}
close PASSWD;

# enter some information into a log file
open(LOG,">>user.log");
print LOG "user $user logged in as root\n";

# read a line of input from the user
$response = <STDIN>;

There are 3 predefined filehandles which have obvious meanings: STDIN, STDOUT, and STDERR. Trying to redefine these filehandles with open statements will cause strange things to happen. STDOUT is the default filehandle for print.

Perl's file input facility acts very differently if it is called in an array context. If input is being read in to an array, the entire file is read in as an array of lines. For instance:

$file = "some.file";
open(F,$file);
@lines = <F>;   # suck in the whole file.  yum, yum,...
close F;

This capability, though useful, should be used with great care. Ingesting whole files into memory can be a risky thing to do if you do not necessarily know what size files you are dealing with. Perl already does a certain amount of input buffering so reading in a file at once does not necessarily yield an increase in I/O performance.