Check if file exists with perl.

Take a command line filename and check if the file exists. I think this is a bashy way of doing this, but its useful nonetheless:

#!/usr/bin/perl
use strict;
die "usage: exist.pl file\n" unless defined($ARGV[0]);
 
if (-e $ARGV[0]) {
  print "File exists!\n";
} else {
  print "File does not exist!\n";
}

The opposite can also be done. If you want to see if a file does not exist you can change the condition to:

if (! -e $ARGV[0]) {
  print "File does not exist!\n";
}
This entry was posted in Programming, Webhosting and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.