P is for Practical | |
PerlMonks |
Re: Re: Is the file there?by Albannach (Monsignor) |
on Oct 25, 2001 at 06:23 UTC ( [id://121351]=note: print w/replies, xml ) | Need Help?? |
I think you'd better anchor that regex or else your /file_name/
will match a file called "this_is_a_file_named_Joe" (that
is to say use /^file_name$/). Even better would
be to use eq in this case (with suitable use of lc if you are
using a case-insensitive file system).
A larger problem is that the directory contents could well have changed between the readdir and the testing loop, so your result won't be certain. Since you are going to die when you find a match anyway, you could just as well put the readdir in the loop condition and not bother with the array at all, which makes your test somewhat more reliable. Even so, you're still testing whether a variable (the return value of the readdir) matches a string, which does not really tell you anything certain about a file on disk. The best solution would be to use the file test operator: die $message if -f '/ber/baz/file_name'. Update: A fair point Tyke. Of course -e also returns true for a directory, for which one might want to be more specific using -d if one were actually looking for a directory. We don't know the greater purpose of the test in this case, but perldoc -f -X should certainly be consulted to select the best test available under the circumstances. --
In Section
Seekers of Perl Wisdom
|
|