How to check if a file exists in Perl?
Question
How to check if a file exists in Perl?
Checking if a file exists is a very common task in Perl, especially when working with file input/output (I/O). Perl provides a set of built-in file test operators which make it easy to check various attributes of files, including whether they exist or not.
Using the -e File Test Operator
The simplest way to check if a file exists in Perl is by using the -e file test operator. It returns true if the file (or directory) specified in its operand exists on the filesystem, otherwise it returns false.
This operator works by taking a filename or path as an argument. You can use it with a string filename or a variable holding the filename. For example:
if (-e "somefile.txt") {
print "File exists\n";
} else {
print "File does not exist\n";
}
Complete Example
Here is a short complete script that reads a filename from the command line argument and checks if that file exists:
#!/usr/bin/perl
use strict;
use warnings;
# Get the filename from the first command line argument
my $filename = shift // '';
if (!$filename) {
print "Usage: perl - \n";
exit 1;
}
if (-e $filename) {
print "File '$filename' exists.\n";
} else {
print "File '$filename' does not exist.\n";
}
Explanation and Perl Concepts
- File Test Operators: Perl includes a group of unary file test operators starting with a dash (-), such as
-e(exists),-r(readable),-w(writable),-x(executable),-f(is file), and more. - Sigils: The variable
$filenameuses the scalar sigil$, since a filename is a scalar string. - TMTOWTDI ("There's More Than One Way To Do It"): You could also check if a file exists by attempting to open it, but using
-eis more direct and idiomatic for existence checking. - Context: The
-eoperator is used in scalar context to return a simple boolean value (true or false).
Common Gotchas
-ereturns true for both files and directories. If you want to check specifically for a regular file (not a directory), use-finstead.- The file might be deleted between checking
-eand accessing it, so for atomic operations prefer opening the file and handling exceptions. - On case-insensitive filesystems (like Windows), checking for existence may not behave as strictly as on Unix systems.
- Remember to ensure the filename is not empty or undefined before applying
-e.
Summary
Use Perl’s -e file test operator to check if a file or directory exists. It's simple, built-in, and efficient for existence testing without opening the file.
The example above demonstrates a complete reusable script that you can run with:
perl - somefile.txt
to check whether somefile.txt exists.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 5ms
File does not exist
(empty)Was this helpful?
Related Questions
- How to use File::Find to recursively search directories in Perl?
- How to use Path::Tiny for file operations in Perl?
- How to use File::Copy to copy files in Perl?
- How to get file modification time in Perl?
- How to use File::Slurp to read files in Perl?
- How to open a file with specific encoding in Perl?