use Fcntl qw(:flock); use Symbol; my @lines = ( qq{Complete documentation for Perl, including FAQ lists,\n}, qq{should be found on this system using `man perl' or\n}, qq{`perldoc perl'. If you have access to the Internet, point\n}, qq{your browser at http://www.perl.com/, the Perl Home Page.\n}, ); my $fh = gensym; open $fh, "+>>/tmp/foo" or die $!; # start critical section flock $fh, LOCK_EX; seek $fh, 0, 0; push @lines, <$fh>; seek $fh, 0, 0; truncate $fh, 0; print $fh @lines; close $fh; # close unlocks the file # end critical section