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"
}