in reply to simple web counter

I don't know why it doesn't work but I can give you a few things to test.

Change the following line so it has a "4" where the "0" is. Does it always return 5 now? That would smell like a DB problem. Probably permissions. $count{$url} = 0 unless exists $count{$url};

Also, are you sure that your ENVironment contains DOCUMENT_URI? Always having a "" for that could be a problem.

Minor nit, why is "use strict;" turned off? What error made you do that? Is that or any other error in your webserver logs?

--
$you = new YOU;
honk() if $you->love(perl)

Replies are listed 'Best First'.
Re^2: simple web counter
by Anonymous Monk on Mar 18, 2017 at 13:56 UTC
    Even i tried this code from the same book . Following is my code
    #!/usr/bin/perl use strict; use Fcntl qw (LOCK_EX); use Fcntl; use DB_File; use constant COUNT_FILE => "count.dbm"; my %count; my $num_hits; my $url = $ENV{REQUEST_URI}; local *DBM; print "Content-type : text/plain\n\n"; my $db = tie %count , "DB_File" , COUNT_FILE , O_RDWR | O_CREAT; if( $db ) { my $fd = $db->fd; open DBM , "+<&=$fd" or die "Count not dump DBM for lock: $!"; flock DBM,LOCK_EX; undef $db; $count{$url} = 0 unless exists $count{$url}; $num_hits = ++$count{$url}; untie %count; close DBM; print "$num_hits\n"; }else{ print "[Error processing counter data]\n"; }
    Following are my observations

    when run the code from my terminal it updates the count and prints

    root@inll11325816l:/usr/lib/cgi-bin# perl counter.cgi Content-type : text/plain 11 root@inll11325816l:/usr/lib/cgi-bin# vi counter.cgi root@inll11325816l:/usr/lib/cgi-bin# perl counter.cgi Content-type : text/plain 12 root@inll11325816l:/usr/lib/cgi-bin# vi counter.cgi root@inll11325816l:/usr/lib/cgi-bin# perl counter.cgi Content-type : text/plain 13 root@inll11325816l:/usr/lib/cgi-bin# perl counter.cgi Content-type : text/plain 14

    When i run from the Browser it just exits from the if loop and prints the statement in the else block I have another question also here , why there is no check for the URI we are accessing here , wht if the DOCUMENT_URI or REQUEST_URI ( in my case has a different .html file that is been accessed Can anyone please help here ?

      Add absolute /path/folder/ that is writeable by the webserver. For example /tmp/

      use constant COUNT_FILE => "/tmp/count.dbm";
      poj