use Fcntl ':flock'; # ... code ... open FH, ">>$file" or die "Can't open $file: $!"; flock FH, LOCK_EX; # lock it # ... print() to file here flock FH, LOCK_UN; # not really necessary since close() # will unlock the file anyway close FH; # ... code ...