Chris Pollett > Old Classes > PIC40 ( Print View ) Lecture Notes-PDF Spring '01 Ad: Enrollment info Course Info:
Practice Exams: PIC: |
HW3 Solutions Page#!/usr/local/bin/perl # # FileName: p40hw3file1.cgi # # Purpose: This program maintains a database of remote hosts who have # accessed this script and how many times they have done it. # It returns an HTML file displaying a table of these statistics. # my %table; my $remoteHost= $ENV{REMOTE_HOST}; BeginHtml(); MakeHead("Remote Host Counter"); BeginBody(); %table = GetUpdate("data", $remoteHost); DrawTable($remoteHost, %table); EndBody(); EndHtml(); # # Function: GetUpdate($dataFile, $remHost) # # Purpose: reads in table of (remotehost count) pairs from the file $datafile # add one to the count of $remHost and write back out the result # # Remarks: In the specification of the homework GetUpdate didn't # have a second argument. However, it seemed cleaner this way. # If you did it either way in your solution you will receive full # credit. # sub GetUpdate { my $dataFile = $_[0]; my $remHost = $_[1]; my %hostTable; my $host; my $cnt; #Read in file open(IN, $dataFile); while(<IN>) { chomp; ($host, $cnt) = split; $hostTable{$host} = $cnt; } close(IN); #Check if $remHost row exists if not create if(!$hostTable{$remHost}) {$hostTable{$remHost}=0; } $hostTable{$remHost}++; # bump count of this site #Write out file open(OUT, ">$dataFile"); foreach $key (keys (%hostTable)) { print OUT "$key $hostTable{$key}\n"; } close(OUT); return %hostTable; } # # Function: DrawTable($host, %table) # # Purpose: outputs the html for a table that lists the # number of times each address has accessed # the given machine. This information if passed # into the function via %table. # sub DrawTable { my $host; my %table; ($host, %table) = @_; print <<THEADSTART; <table width="100%" border="2" cellpadding="4" > <tr> <td bgcolor="gray" color="black"> <h1><b>Welcome.</b> You are the <tt style="background: black; color:white"> THEADSTART printf("%04d</tt> visitor from <tt>$host.</tt></h1>\n", $table{$host}); print <<THEADFINISH; <p>Below is a frequency table of other sites requesting this page: </p> </td> </tr> <tr bgcolor="gray" color="black"> <td> <table width = "100%" border="0" cellpadding ="0"> <tr> THEADFINISH foreach $key (keys (%table)) #print out rows of the table { if($key eq $host) {next;} print "<tr><td><tt>$key</tt></td>\n"; print "<td><tt style=\"background: black; color: white\">"; printf("%04d</tt></td></tr>\n",$table{$key}); } print <<TFINISH; </table> </td> </tr> </table> TFINISH } #END of DrawTable functions # # Function: MakeHead($title) # # Purpose: given a $title passed as it 0th argument, this # function output the head part of an html # document with that $title. # sub MakeHead { my $title=$_[0]; print "<head><title>$title</title></head>\n" } # # Function: BeginHtml # # Purpose: Outputs the content-type and # begin document tag for an Html document # sub BeginHtml { print "Content-type: text/html\n\n"; print "<html>\n"; } # # Function: EndHtml # # Purpose: Outputs the end document tag for an Html document # sub EndHtml { print "</html>\n"; } # # Function: BeginBody # # Purpose: Outputs the start body tag for an Html document # sub BeginBody { print "<body>\n"; } # # Function: EndBody # # Purpose: Outputs the end body tag for an Html document # sub EndBody { print "</body>\n"; } |