Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Check if file exists
by roboticus (Chancellor) on May 06, 2018 at 14:33 UTC | |
by Anonymous Monk on May 07, 2018 at 12:34 UTC | |
by haukex (Archbishop) on May 07, 2018 at 16:12 UTC | |
|
Re: Check if file exists
by Athanasius (Archbishop) on May 06, 2018 at 14:31 UTC | |
by afoken (Chancellor) on May 06, 2018 at 16:22 UTC |