![]() |
Exploiting the Information World
|
|
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. |
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:
html_row: This subroutine should return a string that
holds an HTML table row given an array. The row should have one cell for each
element of the array passed in as parameters. For example, the call:
$foo = html_row(3,"HI",17);
should produce something like this (this would be the value of $foo after the call):
"<TR> <TD>3</TD> <TD>HI</TD> <TD>17</TD></TR>"
NOTES:
@_.@blah
you could do this:
$length = @blah;. Now the variable $length
is the number of elements in the array.
html_table: This subroutine should return a string that
holds an HTML table given an array. The subroutine should assume that each
parameter passed in is an HTML table row (just like one constructed
by the html_row subroutine).
So if the following call is made:
$blah = html_table("<TR><TD>HI</TD><TD>Dave</TD></TR>",
"<TR><TD>33</TD><TD>17</TD></TR>");
The resulting value of $blah should be something like this:
"<TABLE> <TR><TD>HI</TD><TD>Dave</TD></TR> <TR><TD>33</TD><TD>17</TD></TR> </TABLE>"
NOTES:
TABLE.Testing your subroutines: The following code can be used to test your subroutines. Just include this code in your file as the main program.
|
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 |
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:
@_, so you have to treat the first element
$_[0] as the border width and the second element $_[1] as the color. The remaining elements of the array @_ are the
rows of the table (as in the previous exercise). There are a couple of ways
to handle this, here is a real easy way:
|
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:
|
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:
|
NOTES:
\r\n\r\n makes sure that the header line is
followed by a CRLF pair (carrige-return, line-feed), and that there
is a blank line following the header line.