sub Counter ($) { local $/ = "\n"; my $fh; open $fh, '+<', $_[0] or open $fh, '>', $_[0] or return undef; flock $fh, 2; seek $fh, 0, 0; my $counter = <$fh>; seek $fh, 0, 0; truncate $fh, 0; print $fh ++$counter or return undef; close $fh or return undef; return $counter; }