Exploiting the Information World
ITEC-2110 - Fall 2000

These exercises are part of your grade!

For each exercise that you complete during class you must make record your results to show the TA (or show the TA your screen). If you don't have time to complete everything during class you must record your results and give them to the TA by the next class meeting.

EIW Exercises: Perl Subroutines


1. HTML Table Construction

You are to create perl subroutines that makes it easy to generate an HTML table. You will test the subroutines by constructing a table that shows the first 10 powers of 2. Test code (a main program) that calls the subroutines to generate this table is included below.

The subroutines you need to write are:

Testing your subroutines: The following code can be used to test your subroutines. Just include this code in your file as the main program.

# generate an HTML table of the powers of 2

for ($i=0;$i<10;$i++) {
    $rows[$i] = html_row($i,2**$i);
}

$table = html_table(@rows);
print $table;

You can save the output of this perl program in a file, and then load the file into your browser to see what the result looks like. From the DOS prompt use the ">" redirection operator to send the output of the perl program to the file named ex12out.html:

C:\EIWFILES> perl ex12.pl > ex12out.html

2. Improved Tables

Now you should add an extra parameter to your subroutines to make it possible to generate tables with borders and background colors. You need to add 2 parameters to your html_table subroutine, the first parameter specifies the border width and the second paremeter specifies the background color. For example, the following call of html_table:

$foo = html_table(3,"blue",@rows);

should result in a table that has something like the following in the TABLE tag:

<TABLE BORDER=3 BGCOLOR="blue">

NOTES:

3. Improved Rows

Now add the background color to the html_row subroutine as well, so now the first parameter to html_row is the background color for the row. The attribute BGCOLOR goes in the TR tag just like in the TABLE tag.

Test your subroutines with this program:

# generate an HTML table of the powers of 2,3 and 4

$rows[0] = html_row("white"," n " , 
                              "nth power of 2",
                              "nth power of 3",
                              "nth power of 4");

for ($i=0;$i<10;$i++) {
    if ($i % 2) {
	$rows[$i+1] = html_row("#cccccc",$i,2**$i,3**$i,4**$i);
    } else {
	$rows[$i+1] = html_row("#ccccff",$i,2**$i,3**$i,4**$i);
    }
}
$table = html_table(1,"",@rows);
$table2 = html_table(0,"black",html_row("",$table));
print "Here are the powers of 2, 3 and 4<BR><BR>\n";
print $table2;

4. Turn it into a CGI program

Turn your perl program from part 3 into a CGI program. Since your program doesn't need any input, we can ignore any query string that is sent and just worry about what gets sent back to the browser. We just send output to the browser via STDOUT, and since print already does that, we don't need to change much in the program. The only necessary change is that a CGI program must send back a Content-Type: HTTP response header that indicates what kind of document is being sent. In this case we are sending back an HTML document, so you need to add the following to your perl program:


# send back header that says this is HTML:

print "Content-type: text/html\r\n\r\n";

NOTES: