Article
Embedding Perl Into Web Pages
'HTML::Embperl'
HTML::Embperl is another approach to being able to embed Perl code in HTML pages. For more details than are provided here, see the documentation at http://perl.apache.org/embperl/. After installation of the module, directives such as the following samples are to be placed into Apache's httpd.conf file:
PerlModule HTML::Embperl
Alias /embperl/ "/home/www/embperl/"
<Location /embperl>
SetHandler perl-script
PerlHandler HTML::Embperl
Options ExecCGI FollowSymLinks
</Location>
PerlModule HTML::EmbperlObject
<Location /embperl/object>
PerlSetEnv EMBPERL_OBJECT_BASE base.html
PerlSetEnv EMBPERL_FILESMATCH "\.htm.?|\.epl$"
PerlSetEnv EMBPERL_OPTIONS 16
SetHandler perl-script
PerlHandler HTML::EmbperlObject
Options ExecCGI
</Location>
This assumes a mod_perl-enabled server. Any file placed in the /embperl directory will be parsed by the HTML::Embperl Apache handler first. Any file that matches the rule stated by the EMBPERL_FILESMATCH environment variable and placed in the /embperl/object directory will be delivered through the HTML::EmbperlObject Apache handler.
To give a flavor of how Embperl is used, we give the simple example of printing out the values of the various environment variables:
[!
$x = 0;
@colors = ("#FFFFFF", "#CCCCCC");
sub _color{
return $x++ % 2 ? $colors[0] : $colors[1];
}
!]
<br>
<TABLE width=450>
[-
@k = sort keys %ENV;
@headlines = ('Key', 'Value');
-]
<tr bgcolor="[+ &_color() +]">
<th align=left>[+ $headlines[$col] +]</th>
</tr>
<TR bgcolor="[+ &_color() +]">
<TD><font size=1 face="Verdana, sans-serif">
[+ $k[$row] +] </font></TD>
<TD><font size=1 face="Verdana, sans-serif">
[+ $ENV{$k[$row]} +] </font></TD>
</TR>
</TABLE>
If this file, say environ2.html, is placed in the directory specified by the /embperl location in httpd.conf, the result of calling http://localhost/embperl/environ2.html is:
