Learning Perl on Win32 Systems

Learning Perl on Win32 SystemsSearch this book
Previous: 12.6 ExercisesChapter 13Next: 13.2 Renaming a File

13. File and Directory Manipulation

Removing a File
Renaming a File
Making and Removing Directories
Modifying Permissions
Modifying Timestamps

This chapter shows you how to manipulate the files themselves, not merely the data contained within. Perl uses UNIX semantics for providing access to files and directories. Some of these names will be familiar to Win32 programmers who have used the C run-time library, while others may not. Perl provides a rich set of file and directory manipulation routines, and not all of these are implemented on Win32 platforms, but we'll cover the most useful ones here.[1]

[1] In particular, we've omitted discussion of the link() and symlink() functions, used to create hard and symbolic links under Unix, because these functions are unimplemented in the Windows NT filesystems and thus in Perl for Win32, as well.

13.1 Removing a File

Earlier, you learned how to create a file from within Perl by opening it for output with a filehandle. Now, we'll get dangerous and learn how to remove a file (very appropriate for Chapter 13, File and Directory Manipulation, don't you think?).

The Perl unlink function (named for the POSIX system call) deletes a file. This is exactly what the command prompt del command does. Here's how to remove a file called fred and then remove a file specified during program execution:

unlink ("fred"); # say goodbye to fred
print "what file do you want to delete? ";
chomp($name = <STDIN>);
unlink ($name);

The unlink function can take a list of names to be unlinked as well:

unlink ("spottedowl","meadowlark"); # kill two birds
unlink <*.bak>; # just like "del *.bak" in the command prompt

The glob is evaluated in a list context, creating a list of filenames that match the pattern. This list is exactly what we need to feed unlink.

The return value of unlink is the number of files successfully deleted. If only one argument exists, and it is deleted, the result is one; otherwise, the result is zero. If there are three filenames but only two could be deleted, the result is two. You can't tell which two, so if you need to figure out which deletion failed, you must do them one at a time. Here's how to delete all of the backup files (ending in .bak) while reporting an error for any file that cannot be deleted:

foreach $file (<*.bak>) { # step through a list of .bak files
        unlink($file) || warn "having trouble deleting $file: $!";

If unlink returns one (meaning the one file specified was indeed deleted), the true result skips the warn function. If the filename cannot be deleted, the zero result is false, so the warn is executed. Once again, this can be read abstractly as "unlink this file or tell me about it."

If the unlink function is given no arguments, the $_ variable is once again used as a default. Thus, we could have written the preceding loop as:

foreach (<*.bak>) { # step through a list of .bak files
        unlink || warn "having trouble deleting $_\: $!";

Previous: 12.6 ExercisesLearning Perl on Win32 SystemsNext: 13.2 Renaming a File
12.6 ExercisesBook Index13.2 Renaming a File