Which Counter is Perfect?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Counter 1: from Perldoc

    use Fcntl qw(:DEFAULT :flock);
    sysopen(FH, "numfile", O_RDWR|O_CREAT)       or die "can't open numfile:
    flock(FH, LOCK_EX)                           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: $!";
    close FH

Counter 2:

        01: #!/usr/bin/perl -w        02: use strict;
        03: use Fcntl qw(:flock);        04: my $COUNTER = 'count.dat';
        05: my $SEMAPHORE = $COUNTER . ".lock";        06: print
qq{Content-Type: text/html\n\n};        07: open(LOCK, ">$SEMAPHORE") or die
"Can't open $SEMAPHORE ($!)";
        08: flock(LOCK, LOCK_EX);        09: open(DATA, $COUNTER) or die
"Can't open $COUNTER ($!)";
        10: my $count = <DATA>;
        11: close DATA;        12: $count++;        13: open(DATA,
"+<$COUNTER") or die "Can't open $COUNTER ($!)";
        14: print DATA $count;
        15: close DATA;
        16: close LOCK;        17: print qq{You are number
$count!};Logically, Counter 2 looks perfect, but why would perldoc uses a
flawed counter? I also don't understand the difference of    seek(FH, 0, 0)
or die "can't rewind numfile: $!";    truncate(FH, 0)
or die "can't truncate numfile: $!";and open ">$filename";

Re: Which Counter is Perfect?

Quoted text here. Click to load it

The second one is simply madness. Why use a seperate lock file when
the counter file will do? Why open a file in read-write mode when you
are only writing to it?

It also fails to protect the count if the flock fails and hence
can't be called "perfect".

The second one has far more flaws than the first.

Please post code in a readable format and not the gunk that is quoted above.

Sam Holden

Re: Which Counter is Perfect?

Quoted text here. Click to load it

You need to read http://www.perlguy.com/articles/locking.html
to understand why the second one is perfect, as far as it stated. I am not
an expert, but his statement makes sense.

Re: Which Counter is Perfect?

Quoted text here. Click to load it

The only reason a seperate lock file is used in those examples is
because the count file is opened once for reading and then once for
writing. Sane people open it once for read-write and hence don't need
a second file for locking as the lock isn't lost during the
read->write reopen.

And it isn't "perfect", I already gave the case in which data corruption

If you are not expert how do you know his statement makes sense?

Sam Holden

Re: Which Counter is Perfect?

nntp> Counter 1: from Perldoc

nntp> Counter 2:

Counter 3:

use File::CounterFile;
(See docs for examples.)

Don't reinvent what is so readily available.

print "Just another Perl hacker,"; # the original
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

Re: Which Counter is Perfect?

The spontaneous answer to the question in the subject line that comes
up in my mind is: None.

As regards counters that are used for e.g. displaying visits at web
pages, it's important to understand that caching etc. makes the result
not exactly reflect the real visits. But something I personally find
more important is that the value of a counter, that just accumulates
visits, is limited. To me, it's more relevant to focus on the
*current* traffic. This is an example of a Perl counter written with
that view in mind:


For obvious reasons, that code is a little longer than the two
examples you posted, but OTOH it results in more meaningful (IMO)
info. For an example of what it outputs, see the bottom left of

Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Site Timeline