in reply to Chat Error
use warnings; use strict; use Fcntl ':flock'; our $talkfile="t44.talk"; open(TRANSCRIPT,"+>>$talkfile") or die "Client can't open transcript file: $!"; # Read the file with a shared lock flock(TRANSCRIPT,LOCK_SH) or die "Couldn't lock transcript: $!\n"; seek(TRANSCRIPT,0,0) or die "Couldn't rewind transcript file: $!\n"; print <TRANSCRIPT>; # Write the file with an exclusive lock flock(TRANSCRIPT,LOCK_EX); print TRANSCRIPT "ran at ",time,"\n"; # Unlock the file when you're done flock(TRANSCRIPT,LOCK_UN);
The lock will be released automatically when your program exits, so it's OK to die while holding the lock. Opening the file in append mode will tell perl or the OS to seek to the end of the file before writing anything.
|
|---|