Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Is the file there?

by higle (Chaplain)
on Oct 25, 2001 at 06:07 UTC ( [id://121348]=note: print w/replies, xml ) Need Help??


in reply to Is the file there?

... opendir(FOO, "/bar/baz"); my @files = readdir FOO; closedir FOO; foreach my $file (@files){ chomp $file; die $predefined_die_msg if $file =~ /^file_name$/i; } ...
Make sure that, before outputting any error messages to the browser, you print an HTML header. The die/warn message will be printed to the browser if this is going to be run as a CGI.

Update: Even though this is the most IO-intensive 'solution' to the problem, and definitely the longest to execute, it still works, in a caveman kinda way. :) Added line start/line end boundaries to the regex, and chomped that thang.
#------------- perl -e 's=$;$/=$\;W=i;$\=$/;s;;XYW\\U$"\;\);sig,$_^=$[x5,print;'

Replies are listed 'Best First'.
Re: Re: Is the file there?
by Albannach (Monsignor) on Oct 25, 2001 at 06:23 UTC
    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

      -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
Re: Re: Is the file there?
by Ahbeyra (Monk) on Oct 25, 2001 at 06:13 UTC
    Thanks! lots of thanks actually, as simple as that is, i couldnt get it to work ;)

    ----------------------------------
    I love the smell of pastures in the morning... /tell ahbeyra moo

Log In?
Username:
Password:

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

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

    No recent polls found