Updated July 1, 2023
Introduction to Perl file exists
In Perl, file existence is checked using file operators, which are used for checking if the specified file is present or not in the particular directory or folder is known as checking of file existence using file existence operators such as –X operators where particularly we use –e operator for checking file presence in the directory or folder in Perl programming language. In general, we define or check the file existence using file –X operators, such as the –e operator, for checking the presence of a file, which the Perl file operators provide.
Working of checking file existence in Perl with examples
In this, it has file test operator –X, which itself contains various unary file operators for file testing such as file existence, file size, file permissions, etc, where these unary operators take the only single argument as the name itself suggests, and it will take the filename as an argument with file operator. There are many different file operators for testing different concepts related to files in Perl. This –e a file test operator is useful as whenever the programmer wants to read a file or write to a file, there is a need to check file existence; then we use this operator, which is very important in such case.
Now let us see the simple syntax of how to use the file operator for file existence checking in the below section.
Syntax:
-e $file_name;
In the above syntax, we can see we are using the “-e” operator along with the filename of which we want to check or search if the file is present or not. This operator mostly returns the Boolean value, such as true if the file is present and false if the file is not present. This operator is used inside if statements, and they can also be used with AND logical operator (&&) to check or use any other file operators along with this file operator. But this method of using multiple file test operators using AND (&&) logical operator is mostly used in below Perl 5 versions, and in the above versions, the method of using the multiple file test operators along with –e operator can be just done by stacking up the required file test operators which we will see in the below examples. Anyways both method works in Perl 5 version and above.
Examples
Let us discuss examples of Perl file exists.
Example #1
Code:
#!/usr/bin/perl
use warnings;
use strict;
print "Demonstration of file test operator (-e) for checking file existence";
print "\n";
print "\n";
my $f1 = 'C:\Users\User\Documents\ex1.txt';
print "Now the file checking process starts:";
print "\n";
if(-e $f1)
{
print "The file specified is present in the path given as: ";
print "\n";
print $f1;
print "\n";
}
else
{
print "The file specified is not present in the path given: ";
print "\n";
print $f1;
}
Output:
In the above program, we can see we have first declared a file path that specifies the location of the file specified. This path specifies the path of the file, which we are going to check if this file exists in the given path or directory. Then we are using the “if” statement by passing the condition using file test operator –e followed by the variable that holds the file name along with the path, and this variable is “$f1”. When the program starts the execution, the “if” statement checks the file existence using the –e operator and returns true, which prints the statement saying the file is present in the specified file path, and it returns false, which prints the statement saying the file is not present in the specified file path for the specified file name. In the output, we can see in the first execution that we are specifying the file name as “ex1.txt,” created in the path specified along with the file name, which is a text document. Then in the next part, we get the output where the file is not present as we are specifying the file “abc1.txt” not present in the specified path. So remember, whenever specifying the file name then, we should carefully specify the path also; else, it will give a file not found.
Example #2
Code:
#!/usr/bin/perl
use warnings;
use strict;
print "Demonstration of multiple file test operators:";
print "\n";
print "\n";
my $f1 = 'C:\Users\User\Documents\ex1.txt';
print "Use of multiple file test operator by just stacking up the operators:";
print "\n";
if(-e -f -r $f1)
{
print("The specified file is present in the given path this file is readable also");
print "\n";
print $f1;
print "\n";
}
else
{
print "The file specified is not present in the path given: ";
print "\n";
print $f1;
}
print "\n";
my $f2 = 'C:\Users\User\Documents\ex2.txt';
print "Use of multiple file test operator by using logical AND (&&) operator:";
print "\n";
if(-e $f2 && -f _ && -r _ )
{
print("The specified file is present in the given path this file is readable also");
print "\n";
print $f2;
print "\n";
}
else
{
print "The file specified is not present in the path given: ";
print "\n";
print $f2;
}
Output:
In the above program, we can see we have declared two file names and the specified path in which the file is present. In the above, we have seen two different methods of using multiple file test operators along with the –e file test operator, where the first one is just by stacking up the file test operators followed by the file name, and the second is by using the logical AND (&&) operator.
Conclusion
In this article, we saw how to use the –e file test operator with examples and syntax. We also saw how we can use multiple file test operators along with this –e file test operator with example and the two ways to do this.
Recommended Articles
This is a guide to Perl file exists. Here we discuss the Working of checking file existence in Perl, examples with code implementation. You may also have a look at the following articles to learn more –