It is important to check the existence of the file before accessing the file. If the file does not exist and the user opens the file for reading, an error will occur. If the file exists and the user opens the file for writing, the existing content of the file will be overwritten. So, it is necessary to check whether the file exists or not before reading or writing. Many file test operators exist in Perl to check the existence of the file. The purposes of using different test operators and different ways to check the existence of the file are shown in this tutorial.
File Test Operators in Perl
Three test operators of the testing file in Perl are given in the following:
Operator | Purpose |
-e | It returns true if the file exists. |
-z | It returns true if the file exists and is empty. |
-s | It returns true if the file exists and is not empty. |
Examples of File Test Operators
The uses of the file test operators that are mentioned previously are shown in this part of the tutorial using multiple Perl examples.
Example 1: Check Whether the File Exists or Not
Create a Perl file with the following script that takes the filename from the user until a non-existing filename is taken. After taking the valid filename, a line of text will be written in the file. An infinite “while” loop is used in the script to take the valid filename and the “e” operator is used to check whether the file exists or not. The “last” statement is used to terminate the loop after creating the new file with the content.
use strict;
use warnings;
use 5.34.0;
#Take the filename
print "Enter a filename:";
my $filename =<>;
chomp($filename);
#Declare an infinite loop
while(1)
{
#Check whether the file exists or not
if ( -e $filename )
{
say "$filename already exists. Try another filename.";
print "Enter a filename:";
my $filename =<>;
chomp($filename);
}
else
{
#Open the file for writing
open(my $file_handler, "> $filename");
#Write content into the file
print $file_handler "It is a testing file.\n";
#Close the file handler
close $file_handler;
say "File is created successfully.";
last;
}
}
Output:
The following output appears after executing the script for the first time and executing the “cat” command to check the file content. According to the output, one line of text is written in the “test.txt” file:
Example 2: Check Whether the File Is Empty or Do Not Exist
Create a Perl file with the following script that takes the filename from the user, and check whether the file is empty or not using the “-z” file test operator. If the operator returns true, a line of text will be written into the empty file. Otherwise, a message will be printed.
use strict;
use 5.34.0;
#Take the filename from the user
print "Enter a filename:";
my $filename =<>;
chomp($filename);
#Check the file exists but is empty
if (-z $filename)
{
say "$filename already exists but empty.";
say "Writing content to the file...";
#Open the file for writing
open(file_handler, "> $filename");
#Write a line into the file
print file_handler "Add content to the file.\n";
#Close the file handler
close file_handler;
#Print success message
say "File is updated successfully.";
}
else
{
#Print failure message
say "File does not exist or file is not empty.";
}
Output:
The script is executed three times. According to the following output, the “book.txt” file exists in the current location but is not empty or does not exist:
According to the following output, the “testfile.txt” file exists in the current location and is empty. So, a line of text is added to the file:
According to the following output, the “testfile.txt” file exists in the current location and is not empty. So, nothing is added to the file:
Example 3: Check Whether the File Exists and Is Not Empty
Create a Perl file with the following script that takes the filename from the user, and check whether the file is not empty or does not exist using the “-s” file test operator. If the operator returns true, a line of text will be added at the end of the file.
use strict;
use 5.34.0;
#Take the filename from the user
print "Enter a filename:";
my $filename =<>;
chomp($filename);
#Check the file exists but is non-empty
if (-s $filename)
{
say "$filename already exists and not empty.";
say "Appending content to the file...";
#Open the file for appending
open(file_handler, ">> $filename");
#Write a new line into the file
print file_handler "Append new content to the file.\n";
#Close the file handler
close file_handler;
#Print success message
say "File is updated successfully.";
}
else
{
#Print failure message
say "File does not exist or file is empty.";
}
Output:
According to the output, the “test.txt” file is taken as the filename that is not empty. So, a line of text is appended to the file:
The output of the following “cat” command shows that a line is added at the end of the file:
According to the output, the “testing.txt” is taken as the filename that does not exist:
Conclusion
Different uses of the file test operators are shown in this tutorial to check whether the file exists or not using multiple examples.