anmaco has asked for the wisdom of the Perl Monks concerning the following question:

This is really embarrassing. I have just read some of your questions and you people are way out of my league! Hey, I don't even know what the word 'geek' means! Still we all have to start somewhere! I'v just tried my first script for my first page and, surprise surprise, it doesn't work. Could someone take the time to look at it and show me where I am going wrong (or if there's anywhere I'm going right). By the way don't worry about the French/English mix - I live in France and am learning this in French, but write my commentaries in English 'cos I'm lazy!
#!/usr/local/bin/perl # A visit counter ###################################### # Definition of variables # fichierhtml is the file in which the visits are counted # fichiernombre saves the visits ########################################### $ fichiernombre = "/cgi-bin/compteleshits"; ########################################### #First stage : open the file containing the hits to read open(hits,"$fichiernombre"); # Second stage : read the visits and save them in a # new variable $visites=(hits); #Third stage : close the file containing the hits close $fichiernombre #Fourth stage : open the file containing the hits for writing open(hits,">$fichiernombre "); #Fifth stage : increase by 1 the variables which contain #the visits made and rewrite them in fichiernombre print hits ++ $visites; #Sixth stage : close the file containing the visits close %

Replies are listed 'Best First'.
Re: Hit Counter
by turnstep (Parson) on May 21, 2000 at 22:16 UTC
    Here's a quick rewrite:
    #!/usr/local/bin/perl -w use strict; my $fichiernombre = "/cgi-bin/compteleshits"; open(HITS, "+< $fichiernombre") or die "Could not open $fichiernombre: $!\n"; flock(HITS, 2) or die "Could not lock $fichiernombre: $!\n"; my $num=<HITS>; seek(HITS, 0, 0); $num++; print HITS "$num"; close(HITS) or die "Could not close $fichiernombre: $!\n";
    This is a rough, yet workable, version of what you want. Note that if you ar doing anything but simply incrementing the number in the file, you should add a 'truncate' command in there as well. See my tutorial on File Locking to understand *why* you need file locking.

Re: Hit Counter
by btrott (Parson) on May 21, 2000 at 21:55 UTC
    Right off the bat, two lines look quite wrong. The "second stage", where you read from the file, should look like
    $visites = <hits>;
    That's how you read from a file. The sixth stage, where you close the file, should look like
    close hits;
    In addition, though, you should be:
    • Checking for errors on filesystem calls (like open and close).
    • Performing some sort of file locking.
    • Turning warnings on (#!/usr/local/bin/perl -w)
    • Using strict:
      use strict;
    These are good practices for any programmer. The second, file locking, is especially important, because you could have more than one person updating your hit counter file at the same time. Thus you need file locking.

    One of Randal Schwartz's Web Techniques columns focuses on file locking.

      Thanks for the help - I'll try it out! Hey - I still don't know what 'geek' means!
Re: Hit Counter
by Punto (Scribe) on May 22, 2000 at 05:25 UTC
    Also, on first stage, you are opening /cgi-bin/compteleShits (hehe), the correct path of that file would be /home/your_site/cgi-bin/compteleshits
    you shoud probably do:
    open(hits,"$filename") or print "Can't open $filename"; #"die" could + give you an "Internal server error" without any description.
    So you know if the file is opened correctly.
Re: Hit Counter
by KM (Priest) on May 22, 2000 at 07:25 UTC
    Firstly, you should really try to use strict, and -w. One small nit is that you do not need the quotes in the line:

    open(hits,"$fichiernombre");

    You should really have error checking (as the above messages also state). And you should look at my reply to the File Locking article mentioned above.

    Cheers,
    KM