in reply to file testing is hard

If you only want to read or append to the file, open my $fh, "+>>", "filename" will work. (Update: Don't forget to seek before reading.)

Replies are listed 'Best First'.
Re: Re: file testing is hard
by jamgill (Acolyte) on Nov 30, 2003 at 22:12 UTC

    if i only wanted to open the file for reading or writing, yes. the open part isn't really my hurdle so much as the if, elsif, elsif, .. else structure surrounding it which must test the file (does it exist? can i write here? is it zero bytes? is it a directory? a plaintext file? am i opening the wrong thing?)

    I want to catch any possibility (at least on my platform*) and handle it responsibly.

    *nix

    thanks,
    jamgill

      Can you not just try to open it and see if you get an error? The only tricky part is making sure you don't allow a race condition (e.g. if the file is not there, then opening it with "+>"; but having another process create it inbetween the if and then parts).

      Update: s/is there/is not there/