Some File Test Operators
The file test operators allow you to test whether certain file attributes are set before you go and blindly muck about with the files. The most basic file attribute is, of course, whether the file exists. For example, it would be very nice to know whether your mail aliases file already exists before you go and open it as a new file, wiping out everything that was in there before. Table 1-6 gives a few of the file test operators.
Table 1-6. File test operators
| Example | Name | Result |
|---|---|---|
–e
$a | Exists | True if file named in $a exists |
–r
$a | Readable | True if file named in $a is readable |
–w
$a | Writable | True if file named in $a is writable |
–d
$a | Directory | True if file named in $a is a directory |
–f
$a | File | True if file named in $a is a regular file |
–T
$a | Text File | True if file named in $a is a text file |
You might use them like this:
–e "/usr/bin/perl" or warn "Perl is improperly installed.\n"; –f "/vmlinuz" and say "I see you are a friend of Linus.";
Note that a regular file is not the same thing as a text file. Binary files like /vmlinuz are regular files, but they aren’t text files. Text files are the opposite of binary files, while regular files are the opposite of “irregular” files like directories and devices.
There are a lot of file test operators, many of which we didn’t list. Most of the file tests are unary Boolean operators, which is to say they take only one operand (a scalar that evaluates to a filename or a filehandle), and they return either a true or false value. A few of them return ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access