Here is the count.pl program
[ 1]sub count
[ 2]{
[ 3]
[ 4]#
[ 5]# A simple counter program which maintains counts for
[ 6]# all files in a single counts file. Some systems may
[ 7]# not allow "." files to be created, so you should
[ 8]# rename the count file.
[ 9]#
[10]# Comments to pierre@best.com (Pierre Omidyar)
[11]#
[12]
[13]#
[14]# get name of accessed document
[15]#
[16]#
[17]# open counts file for read & write
[18]#
[19]$doc = $_[0];
[20]#print $doc;
[21]$host = $query->gt;remote_host();
[22]
[23]#we will place the Central.counts file in public_html.
[24]$location = "../../Central.counts";
[25]print $location
[26]open(COUNTS, $location) || print "<br>having trouble opening $location file";
[27]
[28]#it would be a good idea to lock the file, but that doesn't work here for some unknown reason.
[29]
[30]#
[31]# read counts database into associative array
[32]#
[33]while (<COUNTS>) {
[34] chop;
[35] ($count, $file) = split(/:/, $_);
[36]#print "<br> after split",$file;
[37] if ($file eq ''){$file = $doc;
[38] $count = 0;
[39] };
[40] $counts{$file} = $count;
[41]#print "<br>",$counts{$doc};
[42]}
[43]close(COUNTS);
[44]#
[45]# increment count of hit document
[46]#
[47]#
[48]
[49]#
[50]# increment count for this file
[51]#
[52]#print "<br>",$counts{$doc};
[53] $counts{$doc} = $counts{$doc}+1;
[54]#print "<br> after incrementing ",$counts{$doc};
[55] #
[56] # rewrite count file
[57] # put file marker back at beginning
[58] #
[59]open(COUNTS, ">$location") || print "<br>having trouble opening file";
[60] foreach $file (keys %counts) {
[61] print COUNTS $counts{$file}, ":", $file, "
";
[62] }
[63]close(COUNTS);
[64]
[65]#
[66]# print count string to STDOUT to be imbedded into WWW page
[67]#
[68]
[69]print "This document has been called ",$counts{$doc}," times.";
[70]print "<hr&&t;";
[71]}
[72]return -1;
and here is the program annotated:
[19]$doc = $_[0];
Line [19] retrieves the argument of CleanUp(argument)
and assigns it to $doc.
[21]$host = $query->gt;remote_host();
Line [21] retrieves the CGI.pm variable known as remote_host, and assigns it to $host.
[24]$location = "../../Central.counts";
[25]print $location
Here we define the file which is going to contain the counts of the various programs
and print it's name.
In our installation, the browser is not allowed to write to the cgi-bin directory,
so we have to put this file ``one back'', i.e., in public_html, which is world
writeable.
You will have to place your Central.counts file where the browser can write to it, which may
be different for different users.
Trial and error will work in getting this part set up correctly.
[26]open(COUNTS, $location) || print "<br>having trouble opening $location file";
Next, at Line [26], we open the file for input.
[33]while (<COUNTS>) {
Line [33] starts a while read group, i.e., it reads one line at a lime
of the opened file.
[34] chop;
Line [34] removes the last character of the last line read, which is
usually a carriage return.
[35] ($count, $file) = split(/:/, $_);
Line [35] splits the line (known here as $_) using a semi colon as a separator,
and assigns the first field to $count and the second field to $file.
[37] if ($file eq ''){$file = $doc;
[38] $count = 0;
[39] };
Lines [37] thru [39] are executed if this is the first time the
file has been counted.
[40] $counts{$file} = $count;
[41]#print "<br>",$counts{$doc};
[42]}
Lines [40] - [42] are executed to assign a count to each file.
[43]close(COUNTS);
[44]#
[45]# increment count of hit document
[46]#
[47]#
[48]
[49]#
[50]# increment count for this file
[51]#
[52]#print "<br>",$counts{$doc};
[53] $counts{$doc} = $counts{$doc}+1;
That count which corresponds to the file of interest is incremented.
[54]#print "<br> after incrementing ",$counts{$doc};
[55] #
[56] # rewrite count file
[57] # put file marker back at beginning
[58] #
[59]open(COUNTS, ">$location") || print "<br>having trouble opening file";
The COUNTS file is now opened for writing, really for overwriting, since it will
erase the last copy, and create a new copy of the count file.
[60] foreach $file (keys %counts) {
[61] print COUNTS $counts{$file}, ":", $file, "
";
[62] }
And lastly, we print out file count and file name, separated by the same colon
that we used in the split statement [36].
[63]close(COUNTS);
[64]
[65]#
[66]# print count string to STDOUT to be imbedded into WWW page
[67]#
[68]
As we prepare to leave, we print (to STDOUT, i.e., to the browser)
how many times we have called this document.
[69]print "This document has been called ",$counts{$doc}," times.";
[70]print "<hr&&t;";
[71]}
[72]return -1;