Perl is often used by system administrators to install software packages and to perform other types of file manipulation. These notes provide a brief overview of Perl's file manipulation commands. For basic directions on how to open, close, and read files, see perl2.
You've already had a brief introduction in the perl2 notes to reading/writing and opening/closing files in Perl. Sometimes you will want to perform an operation on either all of the files in a directory or a certain class of files, such as those ending with the suffix .html. You can determine which files are in a directory in one of two ways: 1) use glob, or 2) use opendir/closedir/readdir.
The glob operator can also be used to obtain either all the names in a directory or the names of a specified class of files in a directory. Its formal syntax is:
glob 'pattern' or <pattern> # no no quotes are neededwhere pattern may be a C-shell type of file pattern. For example:
pattern | meaning | example |
---|---|---|
? | matches a single character-similar to . in regular expressions | brad? matches any filename that starts with brad and then has a fifth character. |
* | matches zero or more characters. Similar to '.*' in regular expressions. | *.perl matches all files with the suffix .perl. |
[chars] | matches any of the characters between the brackets. Ranges like [3-6] and [a-z] are allowed. Shorthand notation like \d does not work. | *[1-9]* matches any file that has a numeric digit. |
{a1,a2,...,an} | matches any of the strings between the braces | *.{perl,html} matches any file with either the suffix .perl or the suffix .html. Note that there is no space between the comma and the alternative strings. |
opendir returns a directory handle that can be used by readdir to access the filenames in a directory. The formal syntax for these commands is:
opendir(MY_DIR, 'perl') || die "Cannot open perl"; while ($filename = readdir(MY_DIR)) { print "$filename\n"; } closedir(MY_DIR); sample output . .. #data# #perl2.html# #perlsys.html# .#data .#perlsys.html add catinput.perl data data~ g.perl g.perl~ ...Notice that readdir does not return the full pathname of the file.
You can use the grep command to narrow the files returned by readdir to a particular category of files. grep takes a regular expression and a list of names and returns a sublist with all names that match the regular expression. For example, to find all .html files in the perl directory I could write:
opendir(MY_DIR, 'perl') || die "Cannot open perl"; @html_files = grep(/^.+\.html$/, readdir(MY_DIR));
chdir newdir: changes to a new directory. For example:
chdir '/home/bvz/cs594' # changes to my cs594 directory.
Things to note about chdir:
use Cwd;and then use the cwd command to return the name of your current directory:
$save_dir = cwd;The following code will change to my cs594 directory and then change back to the current directory:
use Cwd; $save_dir = cwd; chdir '/home/bvz/cs594'; chdir $save_dir;The use statement imports all the functions defined in the Cwd module. We will not be covering modules in this course so do not worry about how modules get constructed.
mkdir 'directory_name', permissions rmdir 'pathname'Notes:
unlink deletes files and returns the number of files it deleted:
unlink list_of_files unlink 'file_name'Notes:
The rename command renames files:
rename 'oldname', 'newname'Notes:
Perl provides a great many "flag-like" commands that allow you to query a file about certain information, such as whether or not it exists, its size in bytes, and its last modification date. For example, the following code tests whether or not a file exists before trying to open it:
if (-e $inputfile) { open(INPUT, $inputfile) || die "$inputfile: $!"; # we could still fail }Some of the most useful flag operators and their meanings are shown in the following table. Unless otherwise noted, the return value is true/false (1/0):
operator | meaning |
---|---|
-e | whether or not the file/directory exists |
-r | whether or not the script has read permission for this file/directory |
-w | whether or not the script has write permission for this file/directory |
-x | whether or not the script has execute permission for this file/directory |
-o | whether or not the script "owns" this directory and hence whether or not it can remove it. |
-s | the size of the file in bytes or undef if the file does not exist |
-M | time since last modification, measured in days. |
-A | time since last access, measured in days. |
Even more file information can be obtained via Perl's stat and lstat functions, but a description of them are beyond the scope of this course.