The Most Useful File Tests in Perl - dummies

The Most Useful File Tests in Perl

By Paul Hoffman

Part of Perl For Dummies Cheat Sheet

Programming with Perl is fairly straightforward, which runs to the letters you use for file tests. For example, r tests whether a file can be read, and T looks for a text file. Here are most useful file tests in Perl:

Test Description
-e File exists.
-r File can be read.
-w File can be written to.
-z File is exactly zero bytes long.
-d Named item is a directory, not a file.
-T File is a text file. (The first chunk of a file is examined,
and it’s a text file if fewer than 30 percent or so of the
characters are nonprintable.)
-B File is a binary file. (This is the exact opposite of the -T
test — it’s a binary file if more than 30 percent or so
of the characters are nonprintable.)
-s Size of the file in bytes.
-C Creation age of file.
-A Access age of file.
-M Modification age of file.