Free Perl Tutorials

PERL File Operators And Their Function

There are a few perl file operators that are very useful in discovering information about a file or directory. Here are four, how they are used, and the information they provide you with:

-e Returns true (which equals 1) or false as to whether an object exists
-z Checks whether a file has zero size
-s Returns the size of a file or directory
-d Checks whether an object is a file or directory

You can use these to your advantage while programming PERL to determine whether a file exists, whether it has no size, is a file or folder, and to check the size of the object.

Usage is simplistic, here is an example in PERL code:

print -s "test2/hey.txt";


The above returns the file size in bytes. If you used "-e" instead, it would likely return a "1" indicating that, yes, the file exists. If it didn't, the file would not return anything.