CECS 5100

Survey of Educational Computer Languages

Fall 2003

Assignments

Assignment #3 (part a) - Intro PERL Program that outputs HTML results

Due Date: October 17, 2003

Value: 5 points

Submission:
Send the instructor an e-mail containing the URL to run your PERL script (.cgi). greg@tapr.org.

Assignment:
Using PERL create a .cgi (using the example on page 10 of the PP book) that instead of outputing to the terminal screen it outputs to a Web Browser with the .cgi program is called. (We covered in class the print statement that tells the browser it is getting html text.)

  1. Upon viewing the HTML page the PERL .cgi program will provide output (see below).
  2. The student will either create the "grades" data file and .cgi program in an external editor (like NoteTab) and upload using WinSCP or use PICO locally on the saturn system using the PuTTY terminal program.
  3. See the class notes about where to download WinSCP and PuTTY from.

Test Data:

  1. Provided on page 9 of the PP book (leave out the ... at the bottom of the list). The data file must reside in the same directory as the .cgi program and must be called "grades". You may alter the loaction or the name, but the .cgi program example on page 10 must also be changed to reflect any changes.
    	Noel 25
    	Ben 76
    	Clementine 49
    	Norm 66
    	Chris 92
    	Doug 42
    	Carol 25
    	Ben 12
    	Clementine 0
    	Norm 66
    	
Output:
  1. Upon reading in the data file and running the program as described on page 10 of the PP book, the .cgi will output the following HTML.

    Example: http://saturn.cecs.unt.edu/cgi-bin/your_account/assignment3a.cgi would output the following HTML.

    <html><body>
    <h2>Assignment #3 (part a) - Your Name</h2>
    <p>
    <table border=1>
    <tr><td>Ben: 76 12 </td><td>Average: 44</td></tr>
    <tr><td>Carol: 25 </td><td>Average: 25</td></tr>
    <tr><td>Chris: 92 </td><td>Average: 92</td></tr>
    <tr><td>Clementine: 49 0 </td><td>Average: 24.5</td></tr>
    <tr><td>Doug: 42 </td><td>Average: 42</td></tr>
    <tr><td>Noel: 25 </td><td>Average: 25</td></tr>
    <tr><td>Norm: 66 66 </td><td>Average: 66</td></tr>
    </table>
    </body></html>
Expected Code:
  1. Student should take the example on page 10 of the 2nd edition of the book or page 17-18 in the 3rd edition and alter it to generate HTML output (see Output above). Example of the PERL program:
    #!/usr/bin/perl
    
    open (GRADES, "grades") or die "Can't open grades: $!\n";
    
    while ($line = <GRADES>) {
            ($student, $grade) = split (" ", $line);
            $grades{$student} .= $grade . " ";
    }
    
    foreach $student (sort keys %grades) {
            $scores = 0;
            $total = 0;
            @grades = split (" ", $grades{$student});
            foreach $grade (@grades) {
                    $total += $grade;
                    $scores++;
            }
            $average = $total / $scores;
            print "$student: $grades{$student}\tAverage: $average\n";
    }
    
    
Note:
  1. The trick to this assignment is 1) getting the program to work as the example shows and then 2) adding in print statements with HTML tags in different places that achive the desired output above. Since not everyone is an expert on HTML, I have provided the table format in the example output.
  2. When uploading files to the system, don't forget to set the premissions to exec. (chmod a+rx filename).
  3. If you get an error in the browser, try running the PERL .cgi program using the PuTTY termainl program, since it will give you the line number of any errors.


Copyright 2003, Dr. James G. Jones