BEGIN { my $countFile= "/path/to/count/file"; my $fh= do { local(COUNT); \*COUNT }; open $fh, ">> $countFile" or die "Can't append to $countFile: $!\n"; my $byte= "\x80" | pack "C", 0x7f & $$; sub getCounter { my $start= tell($fh); print $fh $byte; my $end= tell($fh); return $start if $start+1 == $end; seek($fh,$start,0); my $buf; read( $fh, $buf, $end-$start ); my $offset= index($buf,$byte); return $start + $offset if -1 == index($buf,$byte,$offset); seek( $fh, $start+$offset, 0 ); my $lock; my $new; while( 1 ) { $lock= fileLock->new( $fh, $start+$offset, 1 ); read( $fh, $new, 1 ); if( $new eq $byte ) { $new &= "\x7f"; seek( $fh, $start+$offset, 0 ); write( $fh, $new ); return $start+$offset; } $offset= index($buf,$byte,$offset); die "Impossible!" if $offset < 0; } } }