Article
Embedding Perl Into Web Pages
The meaning of the various lines of the Embperl page are described below:
- The syntax
[!...!]first encountered is used to compile (once) a block of Perl code. The first such block is used to set up the colors used in the table rows and to define a Perl subroutine that will determine the particular color to use. - Next is the
[-...-]syntax and this is used to compile a block of Perl code. This particular one establishes arrays to be used to set the table headings and the value of the various environment variables. - The color subroutine is called through the
&_colorsyntax. This is wrapped within a[+...+]block, which will print out the returned value. - A very powerful feature of
Embperlillustrated in this example is automatic looping over of variables in a table context. In the first illustration, which sets the table headings, as many<TH>tags will be generated as needed, based on the input from the@headlinesarray. The iteration is done with the special global variable $col as the iterator, which holds the number associated with the current column. - This same feature of automatic looping is also illustrated for the table rows, where in this case the required number of rows is automatically generated from input from the %ENV hash. Here, the special global variable
$rowis used as the iterator, which holds the number associated with the current row.
The templating capabilities of Embperl are based on the use of the HTML::EmbperlObject Apache handler, which functions in a manner similar to HTML::Mason's autohandler. To illustrate this, we place the above file, say environ2.html, in the /embperl/object directory specified in httpd.conf. We then construct a file, in the same directory, specified by EMBPERL_OBJECT_BASE (in our example, base.html), which will hold the master template that will be applied to any requested page. A sample base.html appears below:
<HTML>
<HEAD>
<title>Embedding Perl into Web Pages with HTML::Embperl</title>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
[- Execute ('header.html') -]
[- Execute ('*') -]
[- Execute ('footer.html') -]
</BODY>
</HTML>
Here we have included two other files, header.html and footer.html, defining an included header and footer. These are to be placed in the same directory as base.html. An example header.html is:
<H2>HTML::Embperl - EmbperlObject example</H2>
An example footer.html is:
<BR>
<HR size=1 noshade width=450 align=left>
Wrox Press, 2000<BR>
[+ localtime +]
Embedding Perl into Web Pages 365 Finally, requesting the http://localhost/embperl/object/environ2.html will generate the following results. The screenshots show the header and footer files implemented:

