Apache::ASP -- Ebony and Ivory Living Together in Harmony
July 17, 2000
HTML and Perl ... why can't they just get along? They can! In fact, they can
get along quite well, and leveraging Apache::ASP to code Perl directly into
your HTML is an exercise in powerful web design. A segment of Perl code can
be wedged anywhere in your HTML document. Want to simply output the date, in
red sans-serif font?
<DIV STYLE="font-family: arial, helvetica, sans-serif; color: red;">
<% print scalar localtime(time); %>
</DIV>
We can even use Perl to modify the construction of HTML tags themselves.
Let's take a silly but illustrative example: on Thursdays, we want the
background color of the page set to a steel-grey.
<HTML>
<BODY BGCOLOR="<% if ((localtime)[6]==4)
{ print "#CCCCDD" }
else { print "#FFFFFF" }; %>">
etc...
</BODY>
</HTML>
In setting the BGCOLOR attribute for the <BODY> tag, we
use ASP to embed Perl code. The Perl seen above simply determines whether
the current day is Thursday (the fourth day of the week), and if so, outputs
a steel-grey color code, otherwise pure white. The upshot is that the output
from this fragment of Perl code will replace the Perl code itself, so that,
on a Thursday, the web browser will ultimately receive the HTML tag
<BODY BGCOLOR="#CCCCDD">
While we're keeping the embedded Perl examples short, any amount of Perl
code can appear within the ASP tag, and we can even call external Perl
scripts, as we saw earlier with the do statement. What's more
impressive, each ASP tag is not an island -- Perl code can span multiple
segments of HTML. Scenario: we want to query a database, and output a table
wherein each row contains a record retrieved from the database.
<HTML>
<BODY>
<TABLE width='100%' border=2>
<TR>
<TD><B>FIELD 1</B></TD>
<TD><B>FIELD 2</B></TD>
</TR>
<% use DBI;
my $dbh=DBI->connect("dbi:mysql:dbname","dbuser","dbpass");
my $sql="select FIELD1,FIELD2 from BIGTABLE where FIELD2 like 'z%'";
my $sth=$dbh->prepare($sql);
$sth->execute || die "Could not execute SQL query: ".$dbh->errstr;
while (my @row=$sth->fetchrow_array) {
%>
<TR>
<TD><% = $row[0] %></TD>
<TD><% = $row[1] %></TD>
</TR>
<% } %>
</TABLE>
</BODY>
</HTML>
We've liberally sprinkled embedded Perl throughout this page, with great
success. After we build the first row of the table, containing field labels,
we begin an ASP segment. The embedded Perl opens a connection to a database,
and sends an SQL query to this database, requesting FIELD1 and
FIELD2 from all records where FIELD2 begins with the letter
"z". In the last code line of this ASP segment, we begin a
while loop which will iterate through each returned record. Notice
how we close the ASP segment right after opening the loop.
With the ASP segment closed, we can insert plain HTML. So, we begin a table
row with the <TR> tag. We then create the first table column,
using <TD></TD> tags. However, an ASP segment is embedded
within this table column. In this ASP segment, we retrieve the value of the
first column in the returned record, or $row[0]. Actually, there are
two syntaxes we could have used here:
<TD><% = $row[0] %></TD>
or
<TD><% print $row[0]; %></TD>
The "print" syntax is a legitimate Perl statement, and so it must
be terminated with a semicolon. Alternatively, we can use ASP shorthand to
simply retrieve the value of a given variable, as seen in the first syntax
above. No semicolon follows the variable because this is not a Perl
statement, it's simply telling ASP to drop the value of this variable in
this spot.
We mustn't forget to close the while loop, and so you see a final ASP
segment which contains only a closing curly brace, thus completing the
while loop. As you can see, the Perl code is broken up over various
ASP segments, with HTML sprinkled within.
Apache::ASP -- CGI Migration
The Perl You Need to Know
To Embed ... or Not to Embed
|