Perl File Test Operators

Summary: in this tutorial, you will learn about Perl file test operators to check various characteristics of a file.

Before reading from a file or writing to a file, it is important to check if the file exists and readable. In order to perform those tasks, you use Perl file test operators.

The Perl file test operators are logical operators which return true or false value. For example, to check if a file exists you use -e operator as following:

#!/usr/bin/perl
use warnings;
use strict;

my $filename = 'c:\temp\test.txt';
if(-e $filename){
   print("File $filename exists\n");
}else{
   print("File $filename does not exists\n");
}Code language: Perl (perl)

The file test operator -e accepts a filename or filehandle as an argument.

The following list illustrates the most important Perl file test operators:

  • -r: check if the file is readable
  • -w: check if the file is writable
  • -x: check if the file is executable
  • -o: check if the file is owned by effective uid.
  • -R: check if file is readable
  • -W: check if file is writable
  • -X: check if file is executable
  • -O: check if the file is owned by real uid.
  • -e: check if the file exists.
  • -z: check if the file is empty.
  • -s: check if the file has nonzero size (returns size in bytes).
  • -f: check if the file is a plain file.
  • -d: check if the file is a directory.
  • -l: check if the file is a symbolic link.
  • -p: check if the file is a named pipe (FIFO): or Filehandle is a pipe.
  • -S: check if the file is a socket.
  • -b: check if the file is a block special file.
  • -c: check if the file is a character special file.
  • -t: check if the file handle is opened to a tty.
  • -u: check if the file has setuid bit set.
  • -g: check if the file has setgid bit set.
  • -k: check if the file has sticky bit set.
  • -T: check if the file is an ASCII text file (heuristic guess).
  • -B: check if the file is a “binary” file (opposite of -T).

Using multiple Perl file test operators

If you want to check:

  • If a file is a plain file, not directory
  • And the file exists
  • And the file is readable

You can use the AND logical operator in conjunction with file test operators as follows:

#!/usr/bin/perl
use warnings;
use strict;

my $filename = 'c:\temp\test.txt';
if(-e $filename && -f _ && -r _ ){
   print("File $filename exists and readable\n");	
}Code language: Perl (perl)

Whenever you use the file test operator, Perl will make a new call of stat(), which can be expensive. However, Perl stores the result from the last stat() call to a special filehandle named _, so the subsequent file test operators can use the result that stores in the _ filehandle.

Since Perl version 5.9.1 you can stack file test operators as follows:

#!/usr/bin/perl
use warnings;
use strict;

my $filename = 'c:\temp\test.txt';
if(-e -f -r $filename){
   print("File $filename exists and readable\n");	
}Code language: Perl (perl)

In this tutorial, you’ve learned how to use the Perl file test operators to test various attributes of a file.

Was this tutorial helpful ?