use Fcntl ':flock'; open my $fh, '+<' , 'visit.txt' or die "Error opening ..... : $!"; flock $fh , LOCK_EX; my $counter = <$fh>; ++$counter; seek $fh,0,0; truncate $fh,0; print $fh $counter close $fh; # ... output now