Perl | File Test Operators

File Test Operators in Perl are the logical operators which return True or False values. There are many operators in Perl that you can use to test various different aspects of a file. For example, to check for the existence of a file -e operator is used. Or, it can be checked if a file can be written to before performing the append operation. This will help to reduce the number of errors that a program might encounter.

Following example uses the ‘-e’, existence operator to check if a file exists or not:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Using predefined modules
use warnings;
use strict;
   
# Providing path of file to a variable
my $filename = 'C:\Users\GeeksForGeeks\GFG.txt';
  
# Checking for the file existence
if(-e $filename)
{
      
    # If File exists
    print("File $filename exists\n");
}
  
else
{
      
    # If File doesn't exists
    print("File $filename does not exists\n");
}

chevron_right


Output:

Filename or filehandle is passed as an argument to this file test operator -e.
 
Following is a list of most important File Test Operators:

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Using predefined modules
use warnings;
use strict;
   
# Providing path of file to a variable
my $filename = 'C:\Users\GeeksForGeeks\GFG.txt';
  
# Applying multiple Test Operators 
# on the File
if(-e $filename && -f _ && -r _ )
{
   print("File $filename exists and readable\n"); 
}
  
else
{
    print("File $filename doesn't exists")
}

chevron_right


Output:

Above example, checks for the existence of the file and if the file is plain or not and if it is readable.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.