Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by moritz (Cardinal)
on Jun 05, 2008 at 13:43 UTC ( [id://690427]=note: print w/replies, xml ) Need Help??


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

though they should be closed implicitly anyway, shouldn't they,

Only if you use lexical variables as file handles. If you use open HANDLE, '>', $file;, then HANDLE is a global that doesn't go out of scope.

When a file is closed, it should be written to disk immediately (well, the OS can still cache it, but it has to do it transparently).

What you describe here should never happen, so if you can reproduce this behaviour in a small, self-contained script, please file a bug report.

Replies are listed 'Best First'.
Re^2: making sure a file is not currently being written to
by Corion (Patriarch) on Jun 05, 2008 at 13:47 UTC

    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)

      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.

Re^2: making sure a file is not currently being written to
by why_bird (Pilgrim) on Jun 05, 2008 at 14:21 UTC

    Only if you use lexical variables as file handles. If you use open HANDLE, '>', $file;, then HANDLE is a global that doesn't go out of scope.

    Ah, I didn't realise this, thank you (I'm not yet quite on top of typeglobs(?)). In reality there are a lot of different subs writing to that file---I thought I had checked them all for terminating close()s, but perhaps I've missed one... What I'm doing should be fairly simple, so I doubt I've found a long hidden bug.. much more likely to be a mistake on my part. I'll check it out :)

    thanks

    ........
    Those are my principles. If you don't like them I have others.
    -- Groucho Marx
    .......

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-03-29 06:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found