Open Source Development with LAMP: Using Linux, Apache, MySQL, Perl, and PHP

HTML::Mason (aka Mason ”see www.masonhq.com/) is a Perl module created by Jonathan Swartz. Its function is similar to Embperl: to include Perl code within an HTML page. The following are some of the features of Mason:

  • Simple embedded Perl syntax ” An HTML file can harness the power of Perl, including variables (scalar, arrays, hashes, etc.), complex data types, and flow-control constructs.

  • Modular pages with components ” Create subroutine-like code, known as components, to generate headers, footers, menus , and so on.

  • Parameter handling ” It provides easy access to GET/POST parameters, including assigning default values and triggering error conditions.

  • Templates and filters ” A template can be created and applied to the entire site or a portion of the site.

  • Object-oriented techniques ” The power of Perl and its object-oriented features can be used.

  • Caching ” When the code on a page or component is cached, the next time the page or component is requested , the cached version is executed.

  • Previewing ” A built-in previewer can be used to review a page, using a number of client and request conditions.

  • Staging/production modes ” Mason can be configured to use a staging server or staging area for development.

11.1.1 Mason Compared with Embperl

Mason and Embperl are similar: Both embed Perl code directly into HTML pages. This allows the creation of a web page to be divided into two parts : the web designers (graphic artists ) can concentrate on creating the page with static (and easy to use) HTML, and the programmers can focus on adding the programming code.

Both Embperl and Mason allow the use of Perl variables, complex data types, and Perl classes and objects (if we want to use Perl's obect-oriented features). Both provide an interface to Apache::Session to create user sessions and the ability to create templates to apply to the entire web site or portions of the web site. Both cache the compiled Perl code within the HTML, recompiling only when the file timestamp changes.

Why have both? The answer is TMTOWTDI. One of the benefits of Open Source is that we have choices, so here again we have the choice of Embperl or Mason (or Apache::ASP , or eperl, or ...).

We will try to point out some differences between these two approaches, but we think the most important difference lies in this simple idea: It is a matter of style and preference. Because almost every situation encountered in developing a web site is solvable by either Embperl or Mason, the choice comes down to which you prefer. Given that, here are some differences between the two.

Embperl's focus is on easily generating HTML. It is known for its speed and ease of use. Its syntax is straightforward and clean, and there is an easy-to-use Apache::Session interface. It is tightly integrated with HTML, allowing the programmer to dynamically create tables and escape HTML and URLs (with $escmode ), and its form widgets are sticky.

Mason is geared toward a more sitewide approach ”it is more than a template tool. Mason's approach aims less toward making it easy to use and more toward a system that is very flexible and allows the programmers to code "my way" at the cost of being a bit more complicated. An example is that the interface to Apache::Session is not built in ”programmers must add the code to hook into this module; but that enables programmers to do what they want with this module instead of relying on Mason to do it for them. Mason can be a bit more complicated and intimidating to new users, but good online documentation and a helpful user community go a long way toward helping new programmers learn the rich feature set.

An important feature of Mason is that with a special syntax tag ( <%init> ”more on this later), executable code can exist at the bottom of the HTML page, yet be executed first thing. The model here is that in a web design shop, HTML is created by graphic designers; then programmers come in and add code. With Mason, much of that code can simply be added at the bottom of the HTML file, so when the graphic designers update the HTML, they don't have to contend with the Mason code.

Категории