Code Like Grandma Used to Make
July 12, 1999
In our survey of means to output HTML from your Perl scripts,
we'll start with the most basic methods (e.g. "from
scratch") and work forward from there. Even if your
fancy electric mixer prepares fresh dough perfectly well, it
still helps to know the feeling of kneading by hand!
As seen in the previous installments of "The Perl You
Need to Know", we output to the visitor's web browser
using Perl with the CGI module. The call to the header
method of the CGI object tells the web browser to receive any
forthcoming content as HTML.
#!/usr/local/bin/perl
use CGI;
$cgiobject=new CGI;
print $cgiobject->header;
#we can now output HTML
So far, so simple. The most basic means of creating HTML
on-the-fly, then, is to simply use Perl's print
statement to output raw HTML tags and content. We can build a
proper HTML page entirely from scratch using this technique --
simple, but laborious.
#!/usr/local/bin/perl
use CGI;
$cgiobject=new CGI;
print $cgiobject->header;
#build a Web page from scratch
print "<HTML><HEAD>";
print "<TITLE>Grandma\'s Old Tyme Web Page</TITLE>";
print "</HEAD><BODY BGCOLOR=\"#FFFFFF\">";
print "<P ALIGN=\"CENTER\">This page was created<BR>";
print "on-the-fly on <B>".localtime(time)."</B></P>";
print "</BODY></HTML>";
Building this web page from scratch is not an intellectually
difficult process, but would take quite a bit of code to
generate a page of any degree of size or complexity (after
all, the page above only has one sentence!). In addition, you
can see that the HTML itself is not "plain vanilla"
HTML -- rather, we have escaped quotation marks
(\' and \"). The upshot of this is that even if we coded
an entire page from scratch, it would be a challenge to
modify the page readily -- say, to go back and add a table
inside, for instance.
Now, we can improve on the problem of squeezing the HTML code
inside of print statements using a Perl construct known
as a here document. The
here document is Perl's way of letting
you provide a block of text to output, using the syntax:
print <<TOLABEL
text here
TOLABEL
You can select any TOLABEL you wish, although a common
choice might be EOF, the acronym for "end of file".
There should be no space between the << symbol and your
chosen label, as seen above. Of course, your label should be
something which does not appear in the here document
content itself! We can easily see how recasting Grandma's Old
Tyme Web Page using a here document frees the HTML code from
the print statement's grasp.
#!/usr/local/bin/perl
use CGI;
$cgiobject=new CGI;
print $cgiobject->header;
#build a Web page from scratch
$time=localtime(time);
print <<EOF
<HTML><HEAD>
<TITLE>Grandma's Old Tyme Web Page</TITLE>
</HEAD><BODY BGCOLOR="#FFFFFF">
<P ALIGN="CENTER">This page was created<BR>
on-the-fly on <B>$time</B></P>
</BODY></HTML>
EOF
It would be easier to cut-and-paste HTML from, say, an HTML
editor to this Perl script because it does not need to be
modified to reside in the here document. In the previous
example, we calculated the current time --
localtime(time) -- on-the-fly, but we cannot insert a
Perl expression within the here document. Fortunately, you
can insert a variable into a here document. We've
pre-calculated the time into $time which then appears
inside the here document.
Still, the here document technique still requires you to
output explicit HTML code, and this retains some inherent
problems:
- You must know HTML. Presumably, most readers are familiar
with HTML given that this is a web development publication,
after all. Still, coding literal HTML through Perl --
effectively using one language to code in another -- may
feel awkward or unnatural, like trying to swim with eyeglasses
on.
- Applying design changes to the HTML can be a hassle,
since you need to wade through the Perl code.
- Typically, you only need to generate a portion of the
page "live", while the rest of the page could
come from a canned source. Why use Perl to code the majority
of the Web page which does not change? We'll get back
to this point when we look at templates near the end of this
article.
The Perl You Need to Know
The Perl You Need to Know
CGI.pm, the Middleman
|