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 @records = ; seek FILE, 0, SEEK_SET; for(@records) { # now the line will be in $_ rather than $rec next if $_ eq "\n"; # skip empty lines completely my ($id) = split /::/, $_, 2; ++$received if $id eq $formdata{id}; print FILE $_; } truncate FILE, tell FILE; close FILE;