From: [EMAIL PROTECTED] > $old = "oldfile.txt"; > $new = "newfile.txt"; > > open(OLD,"$old") or die "Can't Open File: $!"; > flock OLD, 2; > open(NEW,>$new) or die "Can't Open File: $!"; > flock NEW, 2; > while (<OLD>) { > if ($_ =~ /NO_EMAIL/) { > $count++; > } > else { > print NEW $_; > } > } > close(NEW) or die "Can't Close File: $!"; > close(OLD) or die "Can't Close Old: $!"; > rename($old,$new); > > Is there something inherently wrong with doing it this way?
Yes there is. (Apart from the typo on the open(NEW,...) line.) The problem is that things could happened between you close the old file and rename the new one. And even more likely there can be a process that already has the old file open. So even if you rename the files, the process still has the old one. And as soon as you unlock it, the other process starts reading ... the old data. You HAVE to use a separate log file! Eg. like this (I'm sure someone will be happy to correct me if I screw up) sysopen(FH, "file.lock", O_WRONLY|O_EXCL|O_CREAT) or die "can't open file.lock: $!"; open(OLD,$old) or die "Can't Open File: $!"; open(NEW,">$new") or die "Can't Open File: $!"; while (<OLD>) { if ($_ =~ /NO_EMAIL/) { $count++; } else { print NEW $_; } } close(NEW) or die "Can't Close File: $!"; close(OLD) or die "Can't Close Old: $!"; unlink $old or die "cannot unlink the old file\n"; rename($new => $old) or die "cannot rename\n"; close FH; unlink "file.lock"; or open(LCK, ">file.lock") or die "can't open file.lock: $!"; # so someone else has it opened as well, who cares flock(LCK, LOCK_EX) or die "can't lock the lock: $!"; # I am the only one who can have it locked though open(OLD,$old) or die "Can't Open File: $!"; open(NEW,">$new") or die "Can't Open File: $!"; while (<OLD>) { if ($_ =~ /NO_EMAIL/) { $count++; } else { print NEW $_; } } close(NEW) or die "Can't Close File: $!"; close(OLD) or die "Can't Close Old: $!"; unlink $old or die "cannot unlink the old file\n"; rename($new => $old) or die "cannot rename\n"; close LCK; The second seems a little safer to me (the system will remove the LOCK (not the lock file, just the lock) if the script dies). Jenda ===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz ===== When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]