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

I wrote this small perl code to count the visitors in my web page, I have placed it in Apache directory`s cgi-bin folder, but everything (SSI commands, XTML) is working except the perl part, it doesn`t even show up, for perl section "an error occured while processing this directive" appears, could you check the below code and tell me where I went wrong?
#!/usr/bin/perl # Program to track the number of times # a Web page has been accessed. use CGI qw( :standard ); open( COUNTREAD, "counter.dat" ); $data = <COUNTREAD>; $data++; close( COUNTREAD ); open( COUNTWRITE, ">counter.dat" ); print( COUNTWRITE $data ); close( COUNTWRITE ); print( header(), "<div style = \"text-align: center; font-weight: bold\">" ); print( "You are visitor number", br() ); for ( $count = 0; $count < length( $data ); $count++ ) { $number = substr( $data, $count, 1 ); print( { $number }, "\n" ); }
I appreciate it...

Replies are listed 'Best First'.
Re: Perl counter in cgi-bin
by davido (Cardinal) on Jun 05, 2004 at 15:36 UTC
    This isn't directly related to your problem, but could/will become a problem once you do get the script running: You need to be flocking your counter datafile while it's in use. Otherwise, what's to prevent two scripts from opening it at the same time if two vistors hit your page at roughly the same instant?

    Also, why is your counter iterating through each digit of $data and printing it with a \n newline after each digit? The browser isn't going to render newlines unless you use the <br> tag.

    And why is $number wrapped in { ... } brackets within your final print statement?


      Thanks for your comments, Dave, I think I got carried away while I`m wrapping the $number with braces, I thought I was using normal parentheses, as you said I have to modify some directives on my Apache httpd.conf file to get the perl script runnnig, cos` the SSI began running after I changed / added some options and directives god bless
Re: Perl counter in cgi-bin
by Mr. Muskrat (Canon) on Jun 05, 2004 at 15:50 UTC

    You are not testing whether the open statements worked before continuing on.

    Also, what is the point of the for loop? A simple print $data; should suffice since the numbers will all appear on one line once the funky stuff is removed (since you are not printing a <br />). The funky stuff that I mentioned are the curly braces and they are killing the script.

    Here's your for loop with the curly braces removed (and a couple mys for good measure):

    for ( my $count = 0; $count < length( $data ); $count++ ) { my $number = substr( $data, $count, 1 ); print( $number, "\n" ); }

    Here is another way to do that loop (bonus! I added the <br />s):

    for my $number (split //, $data) { print "$number<br />\n"; }

    Someone is bound to say this if I don't.... You can even do it with a one-liner:

    print "$_<br />\n" for (split //, $data);


    Update: Rewrote the last sentence in the second paragraph for clarity.

      Dear friend, I have tried your solution, but still the same error "an error occurred while processing this directive" I think I have to find that directive first and correct or change then try the code, I`m gonna look at Apache conf. file god bless
        I didn't look at your code...You didn't mention anything about file permissions. Just in case you missed that part out, 'counter.dat' should be chmod'ed to 666 and your perl script to 755.
Re: Perl counter in cgi-bin
by neniro (Priest) on Jun 05, 2004 at 15:58 UTC
    Okay, it is often said, but using warnings and strict forces you to write better code. If you want to debug CGIs use CGI::Carp. And be sure that your webserver/script has the right to alter your counter.dat.
    use strict; use warnings; use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
      Yes, sir I have added that statement, still the same damn message, I`m gonna analyze that Apache conf. file in details, see what`s wrong with it God bless Perl Monks
Re: Perl counter in cgi-bin
by Theo (Priest) on Jun 07, 2004 at 00:24 UTC
    The error message looks (to me) like it's coming from the server attempting to execute, in the html file, whatever is calling your perl script. How closly have you looked at the invocation of your script?

    You might try replacing that script with a simple "Hello World" to test that the invocation is working properly.

    (so many nodes and so little time ... )