debugger has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to append data to a file . However, my perl script hangs , if the file is kept open with some editor say swriter of open office. swriter locks the file. As soon as i close the swriter, i get an error with message "cannot open file".

Replies are listed 'Best First'.
Re: Appending data to locked file
by cdarke (Prior) on Jan 07, 2010 at 13:28 UTC
    Can we assume that your script is hanging on an open? It would clarify things if you could post your code. It would also help in trying to reproduce this if you state which operating system you are running on.

    You probably have a die statement after the open with "cannot open file", please also display $!.

    Update: Tried appending to a file opened by swriter on Windows, and it worked fine. Swriter did not appear to lock the file.
      Here is the code : $logFile = "C:\\WORK\\a.log"; open(LOGFILE, ">>$logFile") || die "$! $?"; print LOGFILE ("HELLO \n"); close(LOGFILE); output : Permission denied 0 at a.pl line 3. the file "a.log" is kept open with swriter. I can see a file named ".~lock.a.log#" at same location where a.log exists. The $? comes out to be 0 and $! is permission denied. I use Active perl 5.8.8. Platform windows.
        Thanks for the update.
        I could only generate a lock file if the file is created by swriter - if the text file already exists it does not generate a lock file (no idea why). However, that still did not generate your symptoms.
        I can exactly reproduce your problem by making the file read-only. Could it be that this is nothing to do with locking but simply that you do not have write/append permissions?

        By the way, $? has nothing to do with this issue. That is the exit status of the previous child process and, so far as I can tell, you are not running any.