#!/usr/local/bin/perl -- -*-perl-*-
use CGI;
$query = new CGI;
print $query->header;
print $query->start_html('count.pl');
print <<EOD;
Here is the count.pl program
<pre>
[ 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 "&lt;br&gt;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 (&lt;COUNTS&gt;) {
[34]	chop;
[35]	(\$count, \$file) = split(/:/, \$_);
[36]#print "&lt;br&gt; after split",\$file;
[37]	if (\$file eq ''){\$file = \$doc;
[38]			\$count = 0;
[39]			};
[40]	\$counts{\$file} = \$count;
[41]#print "&lt;br&gt;",\$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 "&lt;br&gt;",\$counts{\$doc};
[53]	\$counts{\$doc} = \$counts{\$doc}+1;
[54]#print "&lt;br&gt; after incrementing ",\$counts{\$doc};
[55]	#
[56]	# rewrite count file
[57]	# put file marker back at beginning
[58]	#
[59]open(COUNTS, "&gt;\$location") || print "&lt;br&gt;having trouble opening file";
[60]	foreach \$file (keys %counts) {
[61]		print COUNTS \$counts{\$file}, ":", \$file, "\n";
[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 "&lt;hr&&t;";
[71]}
[72]return -1;
</pre>
<img src=../../icons/RAINBOW2.GIF>
<br>
and here is the program annotated:
<pre>
[19]\$doc = \$_[0];
</pre>
Line [19] retrieves the argument of CleanUp(argument)
and assigns it to \$doc.
<pre>
[21]\$host = \$query->gt;remote_host();
</pre>
Line [21] retrieves the CGI.pm variable known as remote_host, and assigns it to \$host.
<pre>
[24]\$location = "../../Central.counts";
[25]print \$location
</pre>
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.
<pre>
[26]open(COUNTS, \$location) || print "&lt;br&gt;having trouble opening \$location file";
</pre>
Next, at Line [26], we open the file for input.
<pre>
[33]while (&lt;COUNTS&gt;) {
</pre>
Line [33] starts a while read group, i.e., it reads one line at a lime
of the opened file.
<pre>
[34]	chop;
</pre>
Line [34] removes the last character of the last line read, which is
usually a carriage return.
<pre>
[35]	(\$count, \$file) = split(/:/, \$_);
</pre>
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.
<pre>
[37]	if (\$file eq ''){\$file = \$doc;
[38]			\$count = 0;
[39]			};
</pre>
Lines [37] thru [39] are executed if this is the first time the
file has been counted.
<pre>
[40]	\$counts{\$file} = \$count;
[41]#print "&lt;br&gt;",\$counts{\$doc};
[42]}
</pre>
Lines [40] - [42] are executed to assign a count to each file.
<pre>
[43]close(COUNTS);
[44]#
[45]# increment count of hit document
[46]# 
[47]#
[48]	
[49]#
[50]# increment count for this file
[51]#
[52]#print "&lt;br&gt;",\$counts{\$doc};
[53]	\$counts{\$doc} = \$counts{\$doc}+1;
</pre>
That count which corresponds to the file of interest is incremented.
<pre>
[54]#print "&lt;br&gt; after incrementing ",\$counts{\$doc};
[55]	#
[56]	# rewrite count file
[57]	# put file marker back at beginning
[58]	#
[59]open(COUNTS, "&gt;\$location") || print "&lt;br&gt;having trouble opening file";
</pre>
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.
<pre>
[60]	foreach \$file (keys %counts) {
[61]		print COUNTS \$counts{\$file}, ":", \$file, "\n";
[62]	}
</pre>
And lastly, we print out file count and file name, separated by the same colon
that we used in the split statement [36].
<pre>
[63]close(COUNTS);
[64]
[65]#
[66]# print count string to STDOUT to be imbedded into WWW page
[67]#
[68]
</pre>
As we prepare to leave, we print (to STDOUT, i.e., to the browser)
how many times we have called this document.
<pre>
[69]print "This document has been called ",\$counts{\$doc}," times.";
[70]print "&lt;hr&&t;";
[71]}
[72]return -1;
</pre>
EOD
print $query->end_html;
