Perl | File Test Operators Last Updated : 21 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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: Perl #!/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"); } 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 -rchecks if the file is readable -wchecks if the file is writable -xchecks if the file is executable -ochecks if the file is owned by effective uid -Rchecks if file is readable by real uid -Wchecks if file is writable by real uid -Xchecks if file is executable by real uid/gid -Ochecks if the file is owned by real uid -echecks if the file exists -zchecks if the file is empty -schecks if the file has nonzero size (returns size in bytes) -fchecks if the file is a plain text file -dchecks if the file is a directory -lchecks if the file is a symbolic link -pchecks if the file is a named pipe (FIFO): or Filehandle is a pipe -Schecks if the file is a socket -bchecks if the file is a block special file -cchecks if the file is a character special file -tchecks if the file handle is opened to a tty -uchecks if the file has setuid bit set -gchecks if the file has setgid bit set -kchecks if the file has sticky bit set -Tchecks if the file is an ASCII text file (heuristic guess) -Bchecks 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: Perl #!/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") } Output: Above example, checks for the existence of the file and if the file is plain or not and if it is readable. Create Quiz Comment A Akanksha_Rai Follow 0 Improve A Akanksha_Rai Follow 0 Improve Article Tags : Perl Perl-files Explore BasicsPerl Programming Language2 min readIntroduction to Perl7 min readPerl Installation and Environment Setup in Windows, Linux, and MacOS3 min readPerl | Basic Syntax of a Perl Program10 min readHello World Program in Perl3 min readFundamentalsPerl | Data Types3 min readPerl | Boolean Values3 min readPerl | Operators | Set - 112 min readPerl | Operators | Set - 27 min readPerl | Variables4 min readPerl | Modules3 min readPackages in Perl4 min readControl FlowPerl | Decision Making (if, if-else, Nestedâif, if-elsif ladder, unless, unless-else, unless-elsif)6 min readPerl | Loops (for, foreach, while, do...while, until, Nested loops)7 min readPerl | given-when Statement4 min readPerl | goto statement3 min readArrays & ListsPerl | Arrays6 min readPerl | Array Slices3 min readPerl | Arrays (push, pop, shift, unshift)3 min readPerl List and its Types4 min readHashPerl Hash4 min readPerl | Hash Operations8 min readPerl | Multidimensional Hashes6 min readScalarsPerl | Scalars2 min readPerl | Comparing Scalars6 min readPerl | scalar keyword2 min readStringsPerl | Quoted, Interpolated and Escaped Strings4 min readPerl | String Operators4 min readPerl | String functions (length, lc, uc, index, rindex)4 min readOOP ConceptsObject Oriented Programming (OOPs) in Perl7 min readPerl | Classes in OOP6 min readPerl | Objects in OOPs6 min readPerl | Methods in OOPs5 min readPerl | Constructors and Destructors4 min readPerl | Method Overriding in OOPs6 min readPerl | Inheritance in OOPs7 min readPerl | Polymorphism in OOPs4 min readPerl | Encapsulation in OOPs6 min readRegular ExpressionsPerl | Regular Expressions2 min readPerl | Operators in Regular Expression4 min readPerl | Regex Character Classes3 min readPerl | Quantifiers in Regular Expression4 min readFile HandlingPerl | File Handling Introduction7 min readPerl | Opening and Reading a File4 min readPerl | Writing to a File3 min readPerl | Useful File-handling functions2 min read Like