5. Input/Output

Perl provides basic I/O for both the standard input (keyboard) and output (display) devices and for files in the UNIX file system. More sophisticated I/O is provided through the UNIX DBM library. These various I/O capabilites are discussed.

5.1 File system I/O

standard files

Perl provides access to the standard files: STDIN, STDOUT, and STDERR.

STDIN is accessed through the angle brackets (<>) operator. When placed in a scalar context, the operator returns the next line; when place in an array context, it returns the entire file, one line per item in the array.


$a = <STDIN>;# returns next line in file @a = <STDIN>; # returns entire file

STDOUT is the default file accessed through a print statement.

STDERR is the file used by the system to which it writes error messages; it is usually mapped to the terminal display.

open file

Files are accessed within a Perl program through filehandles which are bound to filenames within the UNIX file system through an open statement. By convention, Perl filehandle names are written in all uppercase, to differentiate them from keywords and function names.


open (FILEHANDLE, "filename");


open (INPUT, "index.html");

In the above, the file is opened for read access. It may also be opened for write access and for update. The difference between the two is that write replaces the file contents, whereas update appends new data to the end of the current contents. These two options are indicated by appending either a single or a double greater than (>) symbol to the file name as a prefix:


open (FILEHANDLE, ">filename"); # write access open (FILEHANDLE, ">>filename"); # update


open (INPUT, ">index.html"); open (INPUT, ">>index.html");

Since Perl will continue operating regardless of whether the open was successful or not, you need to test the open statement. Like other Perl constructs, the open statement returns a true or false value, indicating success or failure. One convenient construct in which this value can be tested and appropriate response taken is with the logical or and die operators. die can be used to deliver a message to STDERR and terminate the Perl program. The following construct can be paraphrased: "open or die."


open (FILEHANDLE, "filename") || die "Message written to STDERR";


open (INPUT, "index.html") || die "Error opening file index.html ";

close file

Files are closed implicitly when another open is encountered. they may also be closed explicitly.




close (INPUT);

read file

The file, once opened and associated with a filehandle, can be read with the angle brackets operator (<>), which can be used in a variety of constructs.




while (<INPUT>) { # read one line at a time until EOF chop; # remove newline print line = $_ \n"; # print line read using default scalar variable }

write file

Once a file has been opened for either write or update access, data can be sent to that file through the print operator.


print FILEHANDLE (content);


print OUTPUT "$next \n"; # outputs contents of $next followed by newline char.

file tests

There are a number of circumstances where the actions taken by the Perl program should take into account attributes of the file, such as whether or not the file currently exists, whether or not it has content, etc. A number of tests can be performed on files through the dash (-) operator.


-SYMBOL # where SYMBOL is a single character designator

See a Perl text for the complete list; some of the more useful ones include the following:


-r # readable -w # writeable -x # executable -o # owned by user -e # exists -z # zero content -s # nonzero content (size) -f # plain file -d # directory -l # symbolic link -T # text file -B # binary file -M # modification age -A # access age

5.2 UNIX DBM library I/O

Many UNIX systems include as a standard library a database management utility called DBM. Perl provides an interface to this library.

The DBM provides a transparent interface between associative arras internal to a Perl program and a pair of files that are managed by DBM in which the keys and corresponding values of the array are stored. Thus, when one inserts, changes, or deletes a key and/or value, the system makes the appropriate update to the persistent file version of the array. When accessing, the each ( ) operator, which returns both key and value, is more efficient than the foreach ( ) operator.

There are only two operators associated with DBM associate arrays: dbmopen and dmbclose. Once such as array has been opened, all interaction with it is conventional.


Opens the persistent array, given a file name and a access mode.


dbmopen(%ASSOC_ARRAY, "dbmfile", $mode);


dbmopen(%AN_ASSOC_ARRAY, "name_address", $mode);

In this example, the array, %AN_ASSOC_ARRAY can be created and manipulated within the Perl program. It's actual values will be maintained, however, in two files managed by DBM: name_address.dir and name_address.pag. The $mode includes a standard UNIX access mode, such as "0755". For details, see the discussion under chmod in the section on System Operators, below.


Closes the files.