Beefy Boxes and Bandwidth Generously Provided by pair Networks
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??


in reply to Re: Is the file there?
in thread Is the file there?

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.

--
I'd like to be able to assign to an luser

Replies are listed 'Best First'.
Re: Re: Re: Is the file there?
by Tyke (Pilgrim) on Oct 25, 2001 at 16:57 UTC
    -f tests if the file is a 'plain' file or not. If you want to test for existence you might be better off with the -e test

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://121351]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found