Databases Reference
In-Depth Information
"<title>List of Animals</title>".
"</head><body>".
"<h1>Pet roll call</h1>".
"<table border='true'>".
"<tr align='CENTER' valign='TOP' bgcolor='LIGHTGREEN'>".
"<th>Name</th><th>Count</th></tr>";
foreach my $Name (keys %$results)
{
$result.=
"<tr>".
"<th align='left' bgcolor='SKYBLUE'>$Name</th>".
"<td bgcolor='PINK'>".$results->{$Name}->{Count}."</td>".
"</tr>";
}
$result.=
"</table>".
"</body></html>";
print $result;
Here, we build up the HTML content as a string in the $result variable and print this
string out at the end of the script. We've made liberal use of whitespace to help make
the script clearer. If it's still daunting to you, try replacing the variable $Name with an
animal's name, and $results->{$Name}->{Count} with a number. The results will then
be essentially HTML markup, with some Perl wrapping—for example:
<tr>
<th align='left' bgcolor='SKYBLUE'>cats</th>
<td bgcolor='PINK'>2</td>
</tr>
The Perl CGI Module
The Perl CGI module has some helpful functions to simplify generating common snip-
pets of HTML. We can enable all these by modifying our use statement to:
use CGI ':all'
We can then generate HTML elements by calling the corresponding function. For ex-
ample, we can include text within a level-one heading tag pair ( <h1> text </h1> ) by writ-
ing h1("text") .
Many of these functions take attributes that are reflected in the generated HTML. For
example, to generate the tag <th align="LEFT" bgcolor="SKYBLUE"> , we would write:
th({-align=>"LEFT", -bgcolor=>"SKYBLUE"}, $Name).
Example 18-3 rewrites our our previous example using CGI functions.
 
Search WWH ::




Custom Search