6.2 MasonJonathan Swartz, aided and abetted by Dave Rolsky, has created another fine Perl templating package in HTML::Mason. Unlike Embperl, which has a C-based back end, Mason is built purely with Perl, and this implementation tends to reduce potential complexities during installation. You can download the latest package from CPAN and learn much more about Mason at the following sites: The following sections describe how to install Mason on Unix and Win32 and then work through an example. 6.2.1 Installing Mason on UnixWe need to pre-install the following Perl modules for Mason on Unix platforms:
Now follow these steps:
6.2.2 Installing Mason on Win32To install Mason on Win32 platforms, simply connect to the Internet, call up PPM, and install HTML::Mason direct from the University of Winnipeg. (You'll notice on the actual install that you'll get a lot of other modules delivered.) PPM> set repository theoryx5 http://theoryx5.uwinnipeg.ca/cgi-bin/ppmserver.pl?urn:/PPMServer PPM> install HTML-Mason Install package 'HTML-Mason?' (y/N): y Installing package 'HTML-Mason'... ... You then can install the following additional modules from the ActiveState site: PPM> install Time-HiRes Install package 'Time-HiRes?' (y/N): y Installing package 'Time-HiRes'... ... PPM> install MLDBM Install package 'MLDBM?' (y/N): y Installing package 'MLDBM'... ... PPM> quit 6.2.2.1 Installing Params::ValidateThere is a slight complication with Mason on Win32: it requires the Params::Validate module. This module may not be available from either the University of Winnipeg, which specializes in difficult-to-install XS modules, or ActiveState itself. Fear not, for HTML::Mason is a pure Perl module, so in the worst case we can create our own PPM installation with it very easily. Here's how:
6.2.3 Configuring Mason for ApacheThere is plenty of excellent documentation on configuring Mason for use with Apache. Simply point your browser at the HTML documents that come with the Mason download: ../htdocs/index.html To configure Mason, visit the following page: ../htdocs/Mason.html#configuring_mason In this section, we'll illustrate a fairly simple setup — we'll treat all the files found with the .mcomp suffix as special Mason templates files. We added the following to our httpd.conf file, including two alternative commented-out lines for Win32: PerlSetVar MasonCompRoot /usr/local/apache/htdocs PerlSetVar MasonDataDir /usr/local/apache/mason #PerlSetVar MasonCompRoot "C:/Program Files/Apache Group/Apache/htdocs" #PerlSetVar MasonDataDir "C:/Program Files/Apache Group/Apache/mason" PerlModule HTML::Mason::ApacheHandler <FilesMatch "*.mcomp"> SetHandler perl-script PerlHandler HTML::Mason::ApacheHandler </FilesMatch> AddType text/html .mcomp Let's see what's going on here:
Now that we've loaded Mason, let's take a look at using it. Like Embperl, Mason is component-based, a mixture of Perl and HTML that gives you powerful direct access into the heart of the Apache server. We've briefly summarized its major features in Table 6-2.
Next, we'll work through Mason's inline use of Perl (here's where it comes closest in functionality to Embperl). The three inline methods are all used in Example 6-2, which is executed and displayed in Figure 6-2. Example 6-2. hello.mcomp<html><head><title>Hello HTML::Mason</title></head><body> <center> <p><h1>HTML::Mason :-)</h1><hr> <%perl> my $noun = 'World'; my @time = split /[\s:]/, localtime; </%perl> <h2>Hello <% $noun %>, % if ( $time[4] < 12 ) { Good morning. % } elsif ( $time[4] < 18 ) { Good afternoon. % } else { Good evening. % } </h2><br> <h3><% scalar(localtime) %></h3> <hr></center></body></html> Figure 6-2. Hello Mason!Table 6-3 provides examples of the three types of inline Perl available with Mason.
For a database-related example, we've provided MasonBlock.mcomp in Example 6-3. Once again, set mod_perl running, along with the target database, and call up the page as in Figure 6-3. Incidentally, one of the things we especially like about Mason is the comprehensive error browser reporting it provides. This feature greatly aids the development of code, especially when it's all spaghettified across the httpd server, mod_perl, Perl itself, and the Oracle database! Example 6-3. MasonBlock.mcomp — Combining Mason with DBI<html><head><title>Hello HTML::Mason and DBI</title></head><body> <center> <p><h1>Chiseling into DBI with HTML::Mason 8)</h1><hr> <%perl> use DBI; my $url = 'dbi:Oracle:orcl'; my $user = 'system'; my $passwd = 'manager'; my $dbh = DBI->connect($url, $user, $passwd, {RaiseError=>1}); my $sth = $dbh->prepare( 'select tablespace_name tabSpace, ' . 'segment_type segType, ' . 'owner, ' . 'segment_name segName, ' . 'blocks, ' . 'bytes, ' . 'extents, ' . 'next_extent nextExt ' . 'from dba_segments ' . 'where owner != \'SYS\' ' . 'order by 1, 2, 3, 4'); $sth->execute; my $rs = $sth->{NAME}; </%perl> <table border="2"> <tr> % for my $heading (@$rs) % { <th><% $heading %></th> % } </tr> % while (my @row = $sth->fetchrow_array) % { <tr> % for my $data (@row) % { <td><% $data %></td> % } </tr> % } <caption>DBA Segments</caption></table> % $dbh->disconnect; <hr></center></body></html> Figure 6-3. Mason, Oracle, and DBIIf you still hunger for more Perl HTML templating, you may to try out Sam Tregar's HTML::Template module. This module is based on the use of extended HTML tags. HTML::Template aims for a more lightweight, streamlined interface than those offered by Embperl and Mason, while also stressing the separation of design and content production. You may also be tempted by the larger solutions of Andy Wardley's Template Toolkit or Matt Sergeant's XML-based AxKit (you'll find more on XML in Appendix D). Check out the following:
|