in reply to Re: making sure a file is not currently being written to
in thread making sure a file is not currently being written to

This can easily happen when Perl buffers some data instead of flushing it to disk. A second file handle opened to a file can't see that buffered data. Your solution of using lexical filehandles won't solve every case, for example the following:

sub write_stuff { open my $fh, '>>', $logfilename or die "Can't append to '$logfilen +ame': $!"; ... check_stuff(); ... }; sub check_stuff { open my $fh, '<', $logfilename or die "Can't read '$logfilename': +$!"; while (<$fh>) { ... }; };

(also see Suffering From Buffering)

Replies are listed 'Best First'.
Re^3: making sure a file is not currently being written to
by moritz (Cardinal) on Jun 05, 2008 at 13:57 UTC
    If one of your example functions calls the other, or threads are being used, that might happen, yes. If not, it's not allowed to happen.

    From perldoc -f close:

    Closes the file or pipe associated with the file handle, returning true only if IO buffers are successfully flushed and closes the system file descriptor.

    So if the closing was successfully (and it usually is, unless the disk is full or something really evil happens), the buffer has to be flushed. If not, it's a bug.