On 11/22/11 Tue Nov 22, 2011 1:27 PM, "Mark Wagner" <[email protected]> scribbled:
> I want to update a status file, similar to this: > > open OUTFILE, ">", "status.txt"; > print OUTFILE "$last_date\n"; > close OUTFILE; > > However, if something goes wrong (e.g. the disk is full), this code > will replace "status.txt" with an empty file. How can I update the > file while preserving the previous contents if something goes wrong? Write a new file with a different name (you can use a temporary name, see 'perldoc -q temp'). Then close the file handle and check for error. If there are no errors, rename the new file to the old file name. The old file should be released and recycled and the new file left in its place. When writing the new file, check the return status from the open, the print, and the close. If all are OK, the new file should be OK, unless you are writing to a network server. -- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] http://learn.perl.org/
