Полезная информация

Learning Perl

Learning PerlSearch this book
Previous: 12.6 ExercisesChapter 13Next: 13.2 Renaming a File
 

13. File and Directory Manipulation

Contents:
Removing a File
Renaming a File
Creating Alternate Names for a File: Linking
Making and Removing Directories
Modifying Permissions
Modifying Ownership
Modifying Timestamps
Exercises

This chapter shows you how to manipulate the files themselves, not merely the data contained in them. We'll use the UNIX (and POSIX and Linux) semantics for demonstrating access to files and directories. Not all filesystems access mechanisms, but these are the standard ones for reasonably support-rich filesystem models.

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 one name for a file (which could possibly have other names). When the last name for a file is deleted, and no processes have it open, the file itself is removed. This is exactly what the UNIX rm command does. Because a file typically has just one name (unless you've created hard links), for the most part, you can think of removing a name as removing the file. Given that, 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 ("cowbird","starling"); # kill two birds
unlink <*.o>;                  # just like "rm *.o" in the shell

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

The return value of unlink is the number of files successfully deleted. If there's one argument, and it is deleted, the result is one, otherwise it 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 object files (ending in .o) while reporting an error for any file that cannot be deleted:

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

If the unlink returns 1 (meaning the one file specified was indeed deleted), the true result skips the warn function. If the filename cannot be deleted, the 0 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 loop above as:

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


Previous: 12.6 ExercisesLearning PerlNext: 13.2 Renaming a File
12.6 ExercisesBook Index13.2 Renaming a File