in reply to Re: File opening/creating
in thread File opening/creating

Yeah, but if the file you want to open has permission 0200, it's going to bite you twice. First it'll wipe out the data in the file, then it gets stuck in an infinite loop.

Just use sysopen.

Abigail

Replies are listed 'Best First'.
Re: Re: File opening/creating
by davido (Cardinal) on Mar 03, 2004 at 22:55 UTC
    Oh, that's a great point. The basic problem is that if the file does exist but cannot be opened for reading, yet can be opened for output, my method will clobber what's there and then continue looping forever.

    I suppose that's why I should have just used the '-e' function:

    if ( -e $filename ) { open FH, "<$filename" or die $!; } else { open FH, ">$filename" or die $!; close FH; open FH, "<$filename" or die $!; }

    At least this eliminates the potential for the endless loop.


    Dave

      The potential problem here is that you might wipe out data created by another process. Testing for existance and the following open aren't atomic, and a lot of things can happen in between.

      Really, do use sysopen. That's atomic.

      Abigail