Home: Perl Programming Help: Beginner:
work backup file



Jimtaylor5
Deleted

Jun 4, 2000, 11:35 AM


Views: 1185
work backup file

I am trying to write a backup to work on a file so it doesn't get corrupted when someone quits in the middle of the program, but am having problems. Can anyone tell me what is wrong with this, or give me an "example" of a more effective way of doing this?

open(ORIG,"$old");
flock ORIG, 2;
@orig = <ORIG>;
flock NEWMSG, 8;
close(ORIG);

open(BAK,">$new");
flock BAK, 2;
for each $line (@orig) {
# do some work
print BAK "$line";
}
use File::Copy;
open(ORIG,"$old");
flock ORIG, 2;
copy ("$new", "$old");
flock ORIG, 8;
close(ORIG);

flock BAK, 8;
close(BAK);

HELP! How would I do this so that my original file stops occasionally getting corrupted. Flock is working perfectly.