← Previous Next →

Perl Files

When we're dealing with files, we need something that tells Perl which file we're talking about, which allows us to refer to and access a certain file on the disk. We need a label, something that will give us a 'handle' on the file we want to work on. For this reason, the 'something' we want is known as a filehandle. All filehandles are capable of read/write access, so we can read from and update any file or device associated with a filehandle. However, when we associate a filehandle, we can specify the mode in which the filehandle is opened.

Three basic file handles are - STDIN, STDOUT, and STDERR, which represent standard input, standard output and standard error devices respectively. Every program has these three filehandles available, at least at the beginning of the program. To read and write from other files, though, we'll want to open a filehandle of our own. Filehandles are usually one-way: We can't write to the user's keyboard, for instance, or read from his or her screen. Instead, filehandles are open either for reading or for writing, for input or for output.

Opening and Closing Files

There are two functions that can be used to open any existing or a new file in Perl. The two functions are open and sysopen. Here FILEHANDLE is the file handle returned by the open function and EXPR is the expression having file name and mode of opening the file.



1. Open Function

The syntax to read a file in read-only mode is:

open(DATA, "<file.txt");

Here file,txt opens in read-only mode. The less than '<' sign indicates that file has to be opend in read-only mode. DATA is the file handle, which will be used to read the file. If the file does not exist then this creates the file and opens it in read-only mode.

The syntax to open a file in writing mode is:

open(DATA, ">file.txt")

The greater than '>' sign indicates that the file has to be opened in writing mode. This actually clears the contents of the file before opening it for writing mode. In order to open a file in reading as well as writing mode we write the following:

open(DATA, "+<file.txt"); 

To open a file for updating without truncating it we use '+<' whereas to truncate the file first we use '+>'.

open DATA, "+>file.txt"

Sometimes we may want to append content to a file. For this the writing point must be set to the end of the file. For this we use:


A double >> opens the file for appending, placing the file pointer at the end, so that we can immediately start appending information. However, we can't read from it unless we also place a plus sign in front of it as follows:


The following table gives the possible values of different modes:

 Mode Description 
 < or r  Read Only Access
 > or w  Creates, Writes, and Truncates
 >> or a  Writes, Appends, and Creates
 +< or r+  Reads and Writes
 +> or w+  Reads, Writes, Creates, and Truncates
 +>> or a+  Reads, Writes, Appends, and Creates

2. Sysopen Function

The sysopen function is similar to the main open function, except that it uses the system open() function, using the parameters supplied to it as the parameters for the system function. 

The syntax to open a file is:

sysopen(DATA, "file.txt", O_RDWR);

We can use O_CREAT to create a new file and O_WRONLY- to open file in write only mode and O_RDONLY - to open file in read only mode. The PERMS argument specifies the file permissions for the file specified, if it has to be created. By default it takes 0x666.

The possible mode values are listed in the table below:

 Mode Description 
 O_RDWR   Read and Write
 O_RDONLY  Read only
 O_WRONLY Write only 
 O_CREAT Create the file 
 O_APPEND Append the file 
 O_TRUNC Truncate the file 
 O_EXCL Stops if the file already exists 
 O_NONBLOCK Non-Blocking usability

Reading and Writing a File

 There are numerous ways to read and write data to a file. Some of them are:

<FILEHANDL> Operator

The main method of reading the information from an open filehandle is the <FILEHANDLE> operator. In a scalar context, it returns a single line from the filehandle. When we use the <FILEHANDLE> operator in a list context, it returns a list of lines from the specified filehandle. The following example shows the use of this operator:

print "What is your name?\n";
$name = <STDIN>;
print "Hello $name\n";

getc Function

The getc function returns a single character from the specified FILEHANDLE, or STDIN if none is specified. If there was an error, or the filehandle is at end of file, then undef is returned instead.


read Function

The read function reads a block of information from the buffered filehandle: This function is used to read binary data from the file. The length of the data read is defined by LENGTH, and the data is placed at the start of SCALAR if no OFFSET is specified. Otherwise data is placed after OFFSET bytes in SCALAR. The function returns the number of bytes read on success, zero at end of file, or undef if there was an error. Its usage is as follows:


print Function

For all the different methods used for reading information from filehandles, the main function for writing information back is the print function. The print function prints the evaluated value of LIST to FILEHANDLE, or to the current output filehandle (STDOUT by default).

← Previous Next →