On 11-11-22 04:27 PM, Mark Wagner wrote:
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?


use two files:

open OUTFILE, ">", "status.new" or die "could not open status.new: $!\n";
print OUTFILE "$last_date\n";
close OUTFILE;

use File::Copy;
move( "status.new", "status.txt" ) or die "could not move status.new to status.txt: $!\n";


--
Just my 0.00000002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

Never give up your dreams.  Give up your goals, plans,
strategy, tactics, and anything that's not working but never
give up your dreams.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to