How to check if file exists (Perl)

To check if anything exists at given file path use the -e operator.

print "$anything exists!\n" if -e $in_path;

This is the broad test for also directory, a named pipe, a symlink.

If you need just a file (preferable with extension, like .txt) you need -f switch. The -f file-test operator asks whether a plain file exists.

print "plain $file exists!\n" if -f $in_path;

tags: & category: -