open(FH, ">$file") or die $!; &lock(FH,2); print FH $text; &lock(FH,8); close(FH); sub lock{ flock($_[0],$_[1]); }