Article
Embedding Perl Into Web Pages
'HTML::Mason'
This module, which is described in more detail at http://www.masonhq.com/, uses the idea of components, which are mixtures of HTML, Perl code, and Mason commands. A top-level component represents an entire page, while smaller components can be used to generate snippets of HTML to be used in a larger component. Such a design can simplify site management significantly, as changing a shared sub-component can instantaneously change a large number of pages that use this component.
Embedding Perl into Web Pages 355 After installing the module, directives such as those in the following sample are placed in Apache's httpd.conf file:
PerlRequire /path/to/Mason/handler.pl # bring in the handler.pl file
Alias /mason/ "/home/www/mason/" # create a special directory for
# Mason files
<Location /mason>
SetHandler perl-script
PerlHandler HTML::Mason
</Location>
This assumes that we have a mod_perl-enabled Web server. The handler.pl file is used to start Mason and to define a routine to handle the requests passed to it under the Location directive. The following is a sample handler.pl file:
#!/usr/bin/perl
# handler.pl
package HTML::Mason;
use HTML::Mason;
use HTML::Mason::ApacheHandler;
use strict;
use warnings;
# list of modules that we want to use from components (see Admin
# manual for details)
{
package HTML::Mason::Commands;
use DBI;
use CGI qw(:all);
use CGI::Cookie;
use Fcntl;
use MLDBM;
use LWP;
}
# create Mason objects
my $parser = new HTML::Mason::Parser;
my $interp = new HTML::Mason::Interp (parser=>$parser,
comp_root => '/home/www/mason',
data_dir => '/usr/local/apache/mason');
my $ah = new HTML::Mason::ApacheHandler (interp => $interp);
chown (scalar(getpwnam "nobody"), scalar(getgrnam "nobody"),
$interp->files_written);
sub handler
{
my ($r) = @_;
my $status = $ah->handle_request($r);
return $status;
}
1;
The handler.pl file typically creates three objects:
- A parser โ transforms components into Perl subroutines
- An interpreter โ executes these subroutines
- A handler โ routes mod_perl to Mason
The comp_root directory is a virtual root for Mason's component file system, like the server's DocumentRoot directive (they may be the same). The data_dir directory will be used by Mason to generate various data files. Note the convenient ability in this file to load modules to be used in other components.
We illustrate the use of Mason with a simple example, which prints out the values of the environment variables:
<%perl>
my $col1 = "Key";
my $col2 = "Value";
</%perl>
<h2><% $headline %></h2>
<table width=450>
<tr <& .bgcolor &>>
<th align=left><% $col1 %></th>
<th align=left><% $col2 %></th>
</tr>
% foreach my $key (sort keys %ENV){
<tr <& .bgcolor &>>
<td valign=top><b><& .font, val=>$key &></b></td>
<td><& .font, val=>$ENV{$key} &></td>
</tr> % }
</table>
<%init>
my $headline = "The Environment variables:";
</%init>
<%def .font>
<font size=1 face='Verdana, sans-serif'> <% $val %> </font>
<%args>
$val=>""
</%args>
</%def>
<%def .bgcolor>
% my $color= $x++%2?$colors[0]:$colors[1];
bgcolor="#<% $color %>"
</%def>
<%once>
my $x = 0;
my @colors = ('FFFFFF', 'CCCCCC');
</%once>