Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Mysterious Disapperance of file contents

by sgifford (Prior)
on Aug 28, 2003 at 22:45 UTC ( #287575=note: print w/replies, xml ) Need Help??


in reply to Mysterious Disapperance of file contents

What you've posted so far looks fine. What do writedate and release_lock look like?

One good way to do this is to create the new count in a different file, then rename it in to the actual name. You probably have a moment when the file is empty, and if the program crashes just then it will stay empty. By using rename instead, the file is never empty; it is either the old value, or the new value.

Another way is to open the file read-write, read the value, then rewind and write the new value. As long as the number is reasonably short (less than 1000 digits or so) and buffered writes are turned off (or you use syswrite), this should be atomic as well.

Why do you do these two lines?

open(MFILE, ">>$completeadd") || die "file open1 failed: $!\n"; close(MFILE);

Replies are listed 'Best First'.
Re: Re: Mysterious Disapperance of file contents
by belg4mit (Prior) on Aug 29, 2003 at 01:00 UTC
    In which case, there's no sense reinventing wheels.

    --
    I'm not belgian but I play one on TV.

Re: Re: Mysterious Disapperance of file contents
by dtr (Scribe) on Aug 29, 2003 at 11:40 UTC

    The effect of these two lines will be so that when you run the script for the first time, the open(filehandle, filename) || die doesn't die because the file does not exist.

    Otherwise, the script would die continuously until you manually created the file. In theory, the ">>" (append) should prevent data from being overwritten in the file if it does exist and you crash with an open filehandle - although that's perhaps not the safest way of doing things.

    Try removing these lines, and putting a &CreateFile() unless -e $filename; earlier on in the code instead.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2020-05-31 04:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (173 votes). Check out past polls.

    Notices?