On Nov 22, 1:39 pm, shawnhco...@gmail.com (Shawn H Corey) wrote: > 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; ^^^^^^^^^ or die "close error: $!";
Good solution but the close call should be checked for errors as well since a disk may have filled or an outage could have interrupted access. > > use File::Copy; > move( "status.new", "status.txt" ) or die "could not move status.new to > status.txt: $!\n"; > -- Charles DeRykus -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/