Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
In Perl, file test operators provide a convenient way to obtain information about files. These unary operators return information about the file referenced by their argument, such as checking if a file is readable, writable, executable, etc.
Here's a tutorial on using file test operators in Perl:
The basic syntax involves placing the file test operator before the file argument:
if (-e 'myfile.txt') { print "myfile.txt exists.\n"; }
In the example above, -e
checks if myfile.txt
exists.
Here's a list of frequently used file test operators:
-e
: File exists.-f
: File is a plain file (as opposed to a directory, socket, etc.).-d
: File is a directory.-r
: File is readable by the effective uid/gid of the process.-w
: File is writable by the effective uid/gid.-x
: File is executable by the effective uid/gid.-z
: File has zero size (is empty).-s
: File has non-zero size (returns size in bytes).-o
: File is owned by the effective uid.Checking if a File is Readable and Writable:
if (-r 'myfile.txt' && -w 'myfile.txt') { print "myfile.txt is both readable and writable.\n"; }
Checking if a File is Empty or has Content:
if (-z 'myfile.txt') { print "myfile.txt is empty.\n"; } elsif (-s 'myfile.txt') { print "myfile.txt has content and is " . (-s 'myfile.txt') . " bytes in size.\n"; }
Checking if Something is a File or Directory:
if (-f 'myfile.txt') { print "myfile.txt is a file.\n"; } elsif (-d 'myfile.txt') { print "myfile.txt is a directory.\n"; }
When you use multiple file test operators on the same file, Perl caches the results to optimize performance. This feature is handy, but it's essential to be aware of it:
if (-r 'myfile.txt' && -w _) { print "myfile.txt is both readable and writable.\n"; }
Notice the use of _
in -w _
. This special filehandle tells Perl to use the cached stat results from the previous file test operator.
File test operators can also be used with filehandles:
open my $fh, '<', 'myfile.txt' or die "Cannot open: $!"; if (-r $fh) { print "myfile.txt is readable.\n"; } close $fh;
The -M
operator returns the age of the file (in days since modification) since the script started:
my $days_old = -M 'myfile.txt'; print "myfile.txt was last modified $days_old days ago.\n";
Similarly, -A
checks the days since last accessed, and -C
checks the days since inode change.
File test operators in Perl provide a flexible and intuitive mechanism to query properties of files and directories. By understanding and utilizing these operators, you can efficiently perform various file-related tasks and checks in your Perl scripts.
Using -e, -r, -w, -x in Perl:
-e
checks for file existence, -r
tests readability, -w
tests writability, and -x
checks executability.my $file = 'example.txt'; if (-e $file) { print "File exists\n"; } if (-r $file) { print "File is readable\n"; } if (-w $file) { print "File is writable\n"; } if (-x $file) { print "File is executable\n"; }
File existence check in Perl:
-e
operator is used to check whether a file exists.my $file = 'example.txt'; if (-e $file) { print "File exists\n"; } else { print "File does not exist\n"; }
Perl -d and -f operators examples:
-d
operator checks if a file is a directory, and -f
checks if it's a regular file.my $path = 'example_directory'; if (-d $path) { print "It's a directory\n"; } if (-f $path) { print "It's a regular file\n"; }
Testing file readability and writability in Perl:
-r
to test readability and -w
to test writability of a file.my $file = 'example.txt'; if (-r $file) { print "File is readable\n"; } if (-w $file) { print "File is writable\n"; }
Perl file permissions testing:
my $file = 'example.txt'; if (-r $file && -w $file) { print "File is readable and writable\n"; } if (-x $file) { print "File is executable\n"; }
Check if a file is a directory in Perl:
-d
operator to check if a given path corresponds to a directory.my $path = 'example_directory'; if (-d $path) { print "It's a directory\n"; } else { print "It's not a directory\n"; }
Perl file age and modification time testing:
-M
operator returns the age of the file in days since the last modification.my $file = 'example.txt'; my $age = -M $file; print "File age: $age days\n";
Conditional statements with file test operators in Perl:
my $file = 'example.txt'; if (-e $file && -r $file) { print "File exists and is readable\n"; } else { print "File either doesn't exist or is not readable\n"; }
Perl -s and -z file size operators:
-s
returns the size of the file, and -z
checks if the file is empty.my $file = 'example.txt'; if (-z $file) { print "File is empty\n"; } else { my $size = -s $file; print "File size: $size bytes\n"; }