This is (I think) the World's First One-Liner Perl web counter. Touch counter.dat before running.

The WFOLPWC was written on Linux. Other platforms may or may not be able to use this, your guess is probably better than mine.

Don't forget to touch counter.dat before running this:

perl -e 'print "Content-Type = text/html\n\n";open(F, "counter.dat");w +hile (<F>) {$c = $_};chomp $c;$c++;print "$c\n";close F;unlink "count +er.dat";open(N, ">counter.dat");print N "$c";'

And since code tags make copying and pasting one-liners less than ideal:

perl -e 'print "Content-Type = text/html\n\n";open(F, "counter.dat");while (<F>) {$c = $_};chomp $c;$c++;print "$c\n";close F;unlink "counter.dat";open(N, ">counter.dat");print N "$c";'

And for the anally retentive file locking crowd: (Code changes by wog)

perl -MCGI -MFcntl=:flock,:seek -e'print CGI->new->header;open F,"+<counter.dat";flock F,LOCK_EX;print $b=<F>;seek ,0,SEEK_SET;print F ++$b;'

Replies are listed 'Best First'.
Re: One-Line Web Counter
by Zaxo (Archbishop) on Sep 09, 2001 at 06:55 UTC

    What happens when your site gets busy and two of these are running? Thats a race on your file, needs a lock.

    After Compline,
    Zaxo

(zdog) Re: One-Line Web Counter
by zdog (Priest) on Sep 09, 2001 at 07:02 UTC
    Golfing!! I'm pretty sure this would also work:

    perl -MCGI -e 'print CGI->new->header;open(F,"c");($c)=<F>;print ++$c; +close F;open(N,">c");print N $c;'

    Zenon Zabinski | zdog | zdog7@hotmail.com

      More golf...

      Do echo "0" >c first.

      perl -MCGI -i -e 'while(<>){s!(\d+)!$t=$1+1!e;print}print CGI->new->h +eader,$t' c

      JJ

      Update: Removed useless semicolon.

      Update 2: Even smaller now!

        Well leaving aside locking issues, and the point that the number 2 by itself is not exactly valid html, here is the above made even shorter.
        perl -MCGI -pi -e 'END{print&CGI::header,$t}s/\d+/$t=$&+1/e' c
        UPDATE
        And, of course, there is always a shorter way:
        perl -MCGI -pi -e'END{print&CGI::header,$t}$t=++$_' c
Re: One-Line Web Counter
by merlyn (Sage) on Sep 10, 2001 at 05:38 UTC
      Let me preface this by saying the following.
      Your books and articles have been an incredibly tremendous help to me over the years. That being said, maybe you should lighten up. Maybe I'm reading the wrong tone into it, but I can almost hear the scolding condescending voice. (Or perhaps it's lack of sleep making me hear things.)

      Sometimes you have to take things apart and put them together just for the experience. You learn a lot more that way (Even if it doesn't go back together quite right.) Would you not agree that you will learn more by hacking through a problem then by saying use Some::Module;?

      I'm not discouraging the use of Modules, far from it. But sometimes you have to do things the hard way, just to do it. I don't think this is a particularly worthy example, but in general, there seems to be a bit of ambivalence towards people who want to solve problems themselves instead of using a CPAN solution.

      -Lee

      "To be civilized is to deny one's nature."
        Based on having seen multiple people with the same exact misunderstanding over the years, and having talked about it with merlyn before, I can virtually guarantee that you are misreading his tone.

        Also note that modules are best for solving problems that are frequently reinvented badly. Web counters certainly are a prime example of that...