perlfaq nodetype
faq_monk
<P>
Didn't anyone ever tell you web-page hit counters were useless? They don't
count number of hits, they're a waste of time, and they serve only to
stroke the writer's vanity. Better to pick a random number. It's more
realistic.
<P>
Anyway, this is what you can do if you can't help yourself.
<P>
<PRE> use Fcntl;
sysopen(FH, "numfile", O_RDWR|O_CREAT) or die "can't open numfile: $!";
flock(FH, 2) or die "can't flock numfile: $!";
$num = <FH> || 0;
seek(FH, 0, 0) or die "can't rewind numfile: $!";
truncate(FH, 0) or die "can't truncate numfile: $!";
(print FH $num+1, "\n") or die "can't write numfile: $!";
# DO NOT UNLOCK THIS UNTIL YOU CLOSE
close FH or die "can't close numfile: $!";
</PRE>
<P>
Here's a much better web-page hit counter:
<P>
<PRE> $hits = int( (time() - 850_000_000) / rand(1_000) );
</PRE>
<P>
If the count doesn't impress your friends, then the code might. :-)
<P>