...making Linux just a little more fun!
By Ben Okopnik
Recently, I needed to generate a Web page - the Linux Gazette's "Mirrors and Translations"
page, actually - based on the contents of a database. Perl is famous for
its ability to connect to almost any database via a common interface, given
its DBD::DBI
module kit; however, the challenge in this case
came from the front end, the HTML generation. Sure, I could use the CGI
module to output whatever I needed - but in this case, I already had the
static page that I wanted to create, and saw no reason to rewrite all the
static content in CGI. Also, the final product was not to be a CGI file but
a generated HTML page. In fact, everything in this case hinted at
templating, a process in which I would use the static HTML with a
few special tags and a script which would then apply processing based on
those tags. This made especially good sense since it drew a clean
separating line between writing HTML and creating code, very different
tasks and ones for which I have different mental states (layout designer
vs. programmer.)
As with anything in Perl, TMTOWTDI
- there was a number of modules available on CPAN
(the Comprehensive Perl Archive Network) that could do the job. However, I
had used the HTML::Template
module in the past, and the job
wasn't particularly complicated (although HTML::Template
can
handle some very complex jobs indeed), so that's what I settled on. My
first task was to hunt through the HTML, removing the dozens of repetitive
stanzas and replacing them with the appropriate tag framework that the
module would utilize later. We had also made the decision not to display
the maintainers email addresses, even in the munged form that I use to deter
spammers; those of you who use our mirrors and want to thank these fine
folks for making LG available should be able to find an address link on the
mirror site without much trouble.
Fragment of the old page (there were several dozen entries like this):
... <A name="AU"></A> <DT><B><font color="maroon">AUSTRALIA (AU)</font></B></DT> <DD> <STRONG><FONT COLOR="green"><TT>[WWW]</TT></FONT></STRONG> <A HREF="http://www.localnet.com.au/lg/index.html">http://www.localnet.com.au/lg/index.html</A> <BR> <SMALL> Maintainer: Jim McGregor <<A HREF="mailto:nospam@here.please">nospam@here.please</A>> </SMALL> <P> </DD> <DD> <STRONG><FONT COLOR="green"><TT>[WWW]</TT></FONT></STRONG> <A HREF="http://www.eastwood.apana.org.au/Linux/LinuxGazette/">http://www.eastwood.apana.org.au/Linux/LinuxGazette/</A> <BR> <SMALL> Maintainer: Mick Stock <<A HREF="mailto:nospam@here.please">nospam@here.please</A>> </SMALL> <P> </DD>