in reply to Re: Re: Text file got corrupted?
in thread Text file got corrupted?

Hello kiat,

I'm sure the other monks will let me know if I am on the wrong track, but I think you may have a problem in the line flock(FILE, 2) || die $! if $flock;

By putting in the phrase "if $flock" what you are saying is that the script should die if the lock is "shared for reading" but not if it is not locked at all! What I think would be preferable is simply:

flock(FILE, 2) || die "can't print to $tmp_file: $!";
as this would force the script to die on any circumstances other than a "locked for writing" situation.

Addendum: It also seems a little strange to me that you go to the trouble of defining a variable $flock but then use flock(FILE, 2). Though I can see that you wouldn't want to mistakenly do anything else but "2" in a writing situation I would think that consistent use of one or other methods (and making sure that usage is correct in the chosen method) is better than being inconsistent and inviting anomolies to occur.