How is -e $file true when $image is the empty string '' or even undef? How do I check if a file really exists i.e. true only if the file is in the directory? I could do the following but I'm not sure it's the right way:my $dir = "F:/images"; my $image = 'swim.jpg'; #my $image = ''; #my $image = undef; my $file = "$dir/$image"; if (-e $file) { # printed even if $image is '' or undef print "Yes image exists\n"; } else { print "No image doesn't exist\n"; }
if ($image and -e $file) { # printed even if $image is '' or undef print "Yes image exists\n"; } else { print "No image doesn't exist\n"; }
In reply to Check if file exists by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |