Building Sites with Mason

This month, Mr. Lerner introduces us to a Mod-perl module to aid in building large, dynamic web sites.
autohandler and dhandler

While Mason components can create headers and footers using the <& &> syntax we saw above, it becomes cumbersome to put such sections inside each top-level component we create. For this reason, Mason supports two special kinds of components, one called autohandler and the other dhandler.

If an autohandler component exists, it is invoked before each component in the directory. That is, the autohandler is invoked and can produce HTML output of its own before retrieving the component that was actually requested, with $m->call_next. For example, the following autohandler will put a uniform title and footer on each document in its directory:

<Head><Title>Welcome to our
<% $m->call_next %>

dhandler, by contrast, is invoked if a component does not exist. In some ways, this allows us to rewrite the “404--No such file” error message that web sites often produce.

While autohandlers normally influence only their own directories, dhandlers affect all subdirectories. Thus, a dhandler in /foo will affect all documents in /foo/bar, but not in /bar. However, an autohandler in /foo will not affect items in either /foo/bar or /bar.

Slide Show

Now that we have seen how Mason can work for some simple tasks, let's look at some components I wrote for creating slide shows. Such presentations will not have the fancy wipes and graphics available with Microsoft's PowerPoint, but are more than adequate for most technically oriented groups.

The slide show component consists of an autohandler, a dhandler and one or more slides (text files) written in HTML. Each slide consists of a piece of HTML that will be stuck inside the <Body>. For example, the following could be a slide:

<H1>Short Presentation</H1>
<P>This is my short presentation.</P>

Inside the autohandler (Listing 2; see Resources) we have a <%once> section that defines several constants we will reuse, as well as @slides, an array containing the list of slides. For example, here is the value of @slides from a talk I recently gave:

my @slides = qw(start whoami free-software just-in-time
  databases mysql postgresql
  cgi mod_perl
  templates text::template minivend minivend-example
  mason mason-example mason-autohandler
  php jsp zope acs xml
By reordering the file names within @slides, I change the order of my presentation, and by removing or adding elements from @slides, I can change the length of the presentation.

The autohandler uses $m->scomp, described earlier, to retrieve the HTML associated with a slide. It uses this to retrieve any headline (in <H1> tags) it might find within the slide and uses the headline in the <Title> tag.

In addition, the autohandler produces links for the “previous” and “next” slides. We do this by getting the index of the current slide and retrieving the names from the array:

my $previous_slide = $slides[$current_slide_index - 1] || $slides[0];
my $next_slide = $slides[$current_slide_index + 1] || $slides[0];

Once we have the names of the previous and next slides, we can retrieve their headlines, making for attractive “previous” and “next” links:

# Grab the headline from the next component
my $next_headline = $next_slide;
my $next_contents = ($m->scomp($next_slide));
if ($next_contents =~ m|<H1>(.+)</H1>|igs)
    $next_headline = $1;
One of the nice things about using this autohandler for slides is that it allows me to reorder or modify a talk by shifting the names of the files.

In addition to the autohandler, I installed a dhandler to take care of mistaken filenames:

<Head><Title>Error: No such
<P>Sorry, but the page <i><% $r->filename() %></i> does not exist.</P>

Mason provides an environment balanced nicely between simple, easy-to-use templates and the complex, powerful underpinnings of mod_perl. If you ever considered using mod_perl on your site, but were scared away by the complexity, consider looking into Mason. Not only is Mason free software—a good thing, for a variety of reasons—but it is a proven tool that makes web development significantly easier than many of its counterparts. I hope to do much development in Mason over the coming months, and hope to share many of my experiences and code as I grow to enjoy this new tool.


Reuven M. Lerner , an Internet and Web consultant, recently moved to Modi'in, Israel following his marriage to Shira Friedman-Lerner. His book Core Perl will be published by Prentice-Hall in the spring. Reuven can be reached at The ATF home page, including archives and discussion forums, is at