Perl Operators   «Prev  Next»
Lesson 12File test (-X) operators
Objective Create a program that reports file information.

File Test (-X) Operators

Perl has a number of operators for testing the conditions of files. These operators are useful if you want to know if a file is readable, or executable, or if it's a directory, and so on. Because Perl was designed and written on Unix, you will find that many of these operators do not work as expected on Windows NT or other non-Unix operating systems. If you are using a non-Unix system and are unsure whether a particular operator does what you expect, you can try it from the command line with something like this:
perl -e "print -X 'filename' ? qq(true\n)
: qq(false\n)" 

The example above would normally appear on a single line. It is broken into multiple lines for readability.

Here are all the file test operators defined in Perl 5, and some

Perl 5 File Test Operators

Operator Used for
-r File is readable by effective uid/gid.
-w File is writable by effective uid/gid.
-x File is executable by effective uid/gid.
-o File is owned by effective uid.
-R File is readable by real uid/gid.
-W File is writable by real uid/gid.
-X File is executable by real uid/gid.
-O File is owned by real uid.
-e File exists.
-z File has zero size.
-s File has nonzero size (returns size).
-f File is a plain file.
-d File is a directory.
-l File is a symbolic link.
-p File is a named pipe (FIFO).
-S File is a socket.
-b File is a block special file.
-c File is a character special file.
-t Filehandle is opened to a tty.
-u File has setuid bit set.
-g File has setgid bit set.
-k File has sticky bit set.
-T File is a text file.
-B File is a binary file (opposite of -T).
-M Age of file in days when script started.
-A Same for access time.
-C Same for inode change time.


Operator Testing

Use the operator you are testing and a suitable filename, in place of -X and filename above. Then you can see if you get the result you expect.
  • Common uses for file test Operators in Perl
    To see if a file exists, you can check it with -e, like this:
    if(-e $filename) {
     print "$filename is there!\n"
    }
    else {
     print "I didn't find $filename!\n"
    }
    

    Is the file a directory? Check it like this:
    if(-d $filename) {
     print "$filename is a directory\n"
    }
    else {
     print "$filename is not a directory\n"
    }
    

    You can use the special filehandle _ to check the same file more than once. This code checks to see if a file is a plain file, then reports its size:
    if(-f $filename) { 
      $size = -s _; 
      print "$filename is $size bytes." 
    }
    else {
      print "didn't find $filename\n"
    }
    


Perl 6

Using String Operators

As mentioned, the difference between Perl’s functions and operators is a bit vague at times, but for convenience, the punctuation bits are referred to as operators.
  • Repetition Operator: x
    STRING x INTEGER
    (STRING) x INTEGER
    

    The x operator is for repetition. It’s often used to repeat a string several times:
    my $santa_says = 'ho' x 3.7;
    print $santa_says;
    

    The previous code assigns hohoho to $santa_says.
    Sometimes you will want to assign a single value multiple times to a list. Just put the string in parentheses to force list context:
    my $ho = 'ho';
    my @santa_says = ($ho) x 3;
    

    @santa_says now contains the three strings ho, ho, and ho.

XOP Exercise

Click the Exercise link below to practice using file test operators.
XOP Exercise

SEMrush Software