The Perl Tutorial: What's Perl?(6)

File Manipulation

As Perl is a programming language, it has I/O manipulation. But how is this applied to files? A filehandle is the name in a Perl program that connects I/O between your Perl program and external programs.

Perl has three default file handles which are automatically opened. STDIN (standard input), STDOUT (standard output), and STDERR (for standard error). Filehandles have their own name space, and do not conflict with Perl variables. The recommendation from Larry Wall in declaring filehandles is to use a name that is all uppercase.

In order to open a filehandle you would use the open function:

open(FILEHANDLE,"filename");

FILEHANDLE is the name of our handle and "filename" is the name of the file that we wish to open. The function returns true if it is successful and false if it is not.

To open a file for reading you may use either of the following two syntaxes:

open(FILEHANDLE,"filename");

or

open(FILEHANDLE,"<filename");

To create a file you would use the following syntax:

open(FILEHANDLE,">filename");

To append to an existing file you complete the following:

open(FILEHANDLE,">>filename");

Reading from a filehandle is pretty simple:


open(FILEHANDLE,"test.txt");
while(<FILEHANDLE>){
print "line: $_\\n";
}

or


@entire_file = <FILEHANDLE>; # this will read in the entire # file into the array

or


$one_line = <FILEHANDLE>; # this will read one line into # the scalar variable.

To write to a filehandle which has been opened for writing, you would do the following:

print FILEHANDLE "This line will go into our output file";

To open a file in binary mode, use the open function in the same manner as before, except this time, use one additional function:

open(FILEHANDLE,"filename");

binmode(FILEHANDLE);

The binmode() function forces binary mode treatment of the given file handle on systems that distinguish between text and binary files.

Once we are finished using a file handle we should close it:

close(FILEHANDLE);

If you reopen a filehandle before it has been closed it will close the previous file automatically. The same will happen when you exit the program.

There are also some file tests that you can run on a specific file handle or file name. Below are a few:


File Test Meaning -r File or directory is readable -w File or directory is writable -x File or directory is executable -o File or directory is owned by user -e File or directory exists -z File or directory exist and has zero size -f Entry is a plain file -d Entry is a directory -T File is "Text" -B File is "Binary"

An example of using one of these tests might be:


open(FILEHANDLE,"text.txt");
if(-e FILEHANDLE) {
print "File exists.";
}

or


$filename = "/users/abc123/text.txt";
if(-e $filename){
print "File exists.";
}

As you can see, we can perform these file tests on either the file handle, or the file name we want.

Directory Manipulation

Besides allowing you to manipulate files very easily, Perl also allows you manipulate directories in a very straightforward manner -- almost like files.

Let's start with some basic things. In order to change to a particular directory you would use the chdir() function. This function takes one argument: the name of the directory to which you wish to change.

chdir("/home/abc123");

If Perl is able to change to the specified directory, then this function returns true; otherwise, it returns false.

You can generate a list of files in a directory using the * operator in conjunction with the diamond operator (<>). This is called globbing. If I'm in /home/abc123/ and I want a list of all the files that exist, I'd do the following:

@files_found = </home/abc123/*>;

or, to list only Perl files,

@files_found = </home/abc123/*.pl>;

This will give you the names of all the files in the directory that match the specified pattern.

You can also open a directory using directory handles. To open a directory you would use the function opendir():

opendir(DIRHANDLE,"/home/abc123");

Now you can use the directory handle to manipulate the files using the readdir() function. This will also give you access to the filenames. If you invoke the readdir() function in a scalar context then it will return the next filename in the list, if you call it in an array context then it will return a list of all the files in the directory.

$filename = readdir(DIRHANDLE); #returns one filename

@filenames = readdir(DIRHANDLE); #returns a list of filenames

Once you are done with the directory, you can close it using the closedir() function:

closedir(DIRHANDLE);

Pattern Matching