use Fcntl qw(:DEFAULT :flock :seek); sysopen FILE, $mailclicks, O_RDWR | O_CREAT or die "couldn't open $mailclicks: $!"; flock FILE, LOCK_EX or die "failed acquiring lock on $mailclicks: $!"; my @slurp = ; # do something with @slurp seek FILE, 0, SEEK_SET; # go back to top of file print FILE @slurp; truncate FILE, tell FILE; # because the file may be smaller than it was before close FILE;