#!/usr/bin/perl -wT
use CGI qw(:standard);
use strict;
use Fcntl qw(:flock :seek);

print header;           # print the content-type header

open(IN,"+<counts") or &dienice("Can't open counts for reading: $!");
flock(IN,LOCK_EX);	# lock the file (exclusive lock)
seek(IN,0,SEEK_SET);	# rewind it to the beginning
my $count = <IN>;       # read only the first line.

$count = $count + 1;    # increment the counter
truncate(IN,0);         # this erases (truncates) the file to length=0
seek(IN,0,SEEK_SET);	# rewind it to the beginning
print IN "$count\n";   # write out the new count
close(IN);             # close the file.

print "You are visitor number $count.<p>\n";

sub dienice {
    my ($errmsg) = @_;
    print "<p>$errmsg</p>\n";
    exit;
}