If $image is '' or undefined, then $file would be "F:/images/". If the directory F:/images exists, then -e would be true. If you used "use warnings;" you'd at least get a warning in the undefined case.
I'd suggest also using -f instead of -e, as -f would be true only if it exists and is a file rather than a directory:
$ perl u.pl Use of uninitialized value $file in concatenation (.) or string at u.p +l line 16. Use of uninitialized value $file in concatenation (.) or string at u.p +l line 21. OK, missing OK, missing OK, OK $ cat u.pl use strict; use warnings; my $f1; my $f2=''; my $f3='README'; my $dir='./xyz'; print chk($dir, $f1), ", ", chk2($dir,$f1),"\n"; print chk($dir, $f2), ", ", chk2($dir,$f2),"\n"; print chk($dir, $f3), ", ", chk2($dir,$f3),"\n"; sub chk { my ($dir, $file) = @_; return -e "$dir/$file" ? "OK" : "missing"; } sub chk2 { my ($dir, $file) = @_; return -f "$dir/$file" ? "OK" : "missing"; }
...roboticus
When your only tool is a hammer, all problems look like your thumb.
In reply to Re: Check if file exists
by roboticus
in thread Check if file exists
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |