Mike Gossland's Perl Tutorial Course for Windows

Introduction | String Functions | Array Functions | Test Functions | User Functions

Chapter 3. Functions

File Test  Functions

Test functions are functions used to test common file conditions. They are often used in if (...) statements. And there's no way other way to say it — they look weird.

Look at this:

if ( -e $file ) {

    #do something...


means: "if exists $file" then do something... In other words, if the variable $file refers to a file that exists on the hard drive, then do something...

There are many test functions used to test for different things. These next ones are the most useful to a beginner:

-e (file exists)
-f (is a plain file - not a directory)
-d (is a directory)
-z (has zero size)

One last point is that if you don't specify a $file variable, Perl will assume you mean, what else, the default variable!

if ( -e ) {

    #do something...


means if the file specified by the value of the default variable $_ exists, then do something.

One last section on functions remains and that is how to create your own...