# Let the Fcntl module define some constants use Fcntl qw(:DEFAULT :flock); # Open the counter file read/write and lock it. # If the file hasn't been created yet, it creates one. # Of course, the usual flock cautions apply. sysopen(FH, $file, O_RDWR|O_CREAT) or die "Can't open $file_banner: $!\n"; flock(FH, LOCK_EX) or die "Can't write-lock $file: $!\n"; # Read one line from the file. Set $num to the # value from that line, or to zero by default. # It's a little silly to read every line into an array # and reference the first element if you only want # this one value, isn't it? $num = || 0; # Start at the beginning of the file seek(FH, 0, 0) or die "Can't rewind $file: $!\n"; truncate(FH,0) or die "Can't truncate $file: $!\n"; # Print the new number to the file and close it print FH $num+1, "\n" or die "Can't write to $file: $!\n"; close(FH) or die "Can't close $file: $!\n";