Don't reinvent the wheel. Use File::CounterFile.
my $path_to_counterfile = "/tmp/merlyn.counterfile"; use File::CounterFile; my $next_number = File::CounterFile->new($path_to_counterfile)->inc;