In a message dated 10/30/03 12:04:07 PM Eastern Standard Time,
[EMAIL PROTECTED] writes:
> 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
>
A Thousand Thanks! As a real "beginner" I appreciate your taking the time to
help me find a solution to this problem, and explaining the possible reasons.
I think I now have a better grasp of what's going on and why. Again, many
Thanks for your patience!