Managing Files

In addition to simply reading from and writing to files, you can also manage them from inside Perl, just as you might from a command line or in a file manager: you can rename them, delete them, change their permissions, or create links to them. Each of these tasks makes use of several Perl built-in functions. In this section, you get an overview of each of these.

Renaming Files

To rename a file from one name to another, keeping its contents intact, use the rename function with two arguments: the old name of the file and the new name:

rename myfile, myfile.bak;

If a file with the same name as the new file already exists, Perl will overwrite it. If you're used to Unix, this won't be a problem. If you're a Windows or Mac user, beware! There are no checks to make sure you're not overwriting an existing file. You might want to test to make sure that the new file doesn't exist (with one of the file tests, such as -e) before you rename it.

The rename function returns 1 or 0 (true or false) depending on whether the rename was successful or not. You (or the user running the script) must have the right file permissions to rename the file.

Creating and Following Links

The Unix version of Perl also provides two functions for creating links between files: link and symlink, which create hard and symbolic links, respectively (similar to the Unix command ln(1)). Hard links are used to create a file with more than one name; if you remove any of those names (including the original file), the file continues to exist as long as there are multiple names for it. Symbolic links are pointers to other files; if you remove the link, the file continues to exist, but if you remove the original file, the symbolic link will no longer point to anything useful. Some Unix systems might not support symbolic links.

Use either the link or symlink functions with two arguments: the name of the original file to link to, and the name of the new link. Take this example:

link file1, file2;

Both functions return 1 (true) or 0 (false) depending on whether they were successful or not.

Both hard and symbolically linked files are transparent to Perl; if you use open with a filename that is actually a link, the original file will be opened instead. You can test to see if a given file is a symbolic link with the -l file test, and then follow that link to its original location with the readlink function:

if (-l $file) {  # file is a link
   $realfile = readlink $file;   # get the real file
}

Note that readlink returns the location of the real file as a relative path to the symbolic link, which means you might need to expand that path into a full path, or change directories to the place where the symbolic link is able to use the original file's path name. Changing directories is covered later in this chapter.

Using Perl for Windows? Neither hard nor symbolic links are available in Perl for Windows. You can use the Win32::Shortcut module to create Windows Explorer shortcuts on Windows.

Removing Files and Links

You're done with a file, you're sure you don't need it anymore. How do you get rid of it? Use the unlink function (despite its name, it's used to remove both links and files). It works much like the Unix command rm or the DOS command del; it does not move the file to a trashcan or recycle bin like Windows or the Mac. When the file is removed, it's really removed. Be careful or you can easily delete files you actually wanted to be there.

unlink takes a list argument, which can be a single file or a list of files. It returns the number of files it deleted:

unlink temp, config, foo;

Be aware that on some systems—Unix being the most obvious—Perl has no qualms about deleting read-only files, so watch what you're doing (the state of being read-only actually determines whether the contents of the file can be changed, not whether or not that filename can be moved or deleted).

The unlink function, when used on a hard link, removes that link. Other hard links to the file will continue to exist. On a symbolic link, you'll remove the link, but not the original file. You can use readlink to follow any symbolic links. Note that removing a file that has other symbolic links to it causes those symbolic links to point to nothing.

You cannot use unlink to remove directories; you'll learn how to remove directories in the section “Managing and Navigating Directories.”

Other Operations

I'm going to lump a number of other file-related functions together here, because these operations tend to vary from platform to platform (or not to exist at all on Windows or Mac). Table 17.1 shows some of the other file-management functions available on Unix systems, all of which correspond to actual Unix commands. If you're interested in running any of the functions listed in Table 17.1, check the documentation for your particular version of Perl to see if these functions are supported (or if the capabilities are supported through other means such as platform-specific modules.)

For more information on any of these functions, see the perlfunc man page.

Table 17.1. Other File Management Functions
Function What It Means
chmod Change the permissions for the file (read, write, execute, for world, group or owner). Windows and Mac have limited versions of this command that only accept permissions of 0666 (read and write) and 0444 (read only or locked).
chown Change the owner of the file (Unix only).
fileno Return the file descriptor number for the file.
utime Change the timestamp for a file.

For managing file permissions and attributes on Windows and Windows NT, consider the Win32::File and Win32::FileSecurity modules, which allow you to check and modify file attributes and NT user permissions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.103.16