Tài liệu Embedding Perl in HTML with Mason Chapter 1: Introduction - Pdf 87

Embedding Perl in HTML with Mason

Chapter 1: Introduction
At its heart, Mason is simply a mechanism for embedding Perl code into
plain text. It is only one of many such mechanisms that all do more or less
the same thing. However, Mason represents a particular set of choices about
how this embedding should be done, and many people have found that the
way Mason does things is very straightforward and extremely conducive to
getting jobs done.
In this chapter we'll introduce you to some of Mason's key features and
strengths, show you a couple of examples of how to use Mason, and talk
about some alternatives to Mason. After reading this chapter, you should
have a fairly good idea of how Mason relates to its peers and what kinds of
tasks you can accomplish with Mason.
The most common application of Mason is in building large dynamic web
sites, and this book focuses mostly on web site building. Mason is broadly
applicable to any situation in which fine control over document content is
required, however, such as generating mail-merged form letters, creating
custom configuration file sets, and even building dynamic GIF images based
on varying input parameters. We intend to give you enough facility with
Mason that after reading this book, you can imagine Mason-based solutions
to problems we haven't ever thought of.
Before we get into the details of Mason and comparisons with its
alternatives, we'll just briefly mention some of its guiding design principles.
Mason was designed to help you build, organize, and maintain large web
sites or other groups of dynamically generated documents. It cooperates
fully with Perl, leveraging all the solutions and techniques that Perl
developers have come to depend on and that have made Perl such a powerful
and widespread tool. It encourages thinking about your site in structural
terms rather than as a collection of procedural scripts or modules. All of
these things are conducive to getting your job done effectively, letting you

the world, this section presents Mason's most important and distinctive
features. By the end of this section, you should see that Mason pushes the
boundaries of the term " templating system," with lots of features aimed at
helping you manage the larger tasks of site design and maintenance.
Components: Modular Design Elements
As we mentioned before, the basic unit of Mason code is called a
component. It is a chunk of Mason code that can accept input parameters
and generate output text. An important feature of Mason is that any
component may call any other component at any point during its execution,
much like a Perl subroutine calling another Perl subroutine. Because of this
feature, a component may represent a single web page, a part of a web page
(like a side navigation bar), or even a shared utility function that generates
no output of its own. This separation of design elements allows you to use
Mason as a sort of glorified server-side include (SSI) mechanism, as in
Example 1-1, Example 1-2, and Example 1-3. Executing mainpage.mas will
produce a full page of HTML with the header and footer inserted in place.
Example 1-1. header.mas
<html>
<head><title>Welcome to Wally
World!</title></head>
<body bgcolor="#CCFFCC">
Example 1-2. footer.mas
<center><a href="/">Home</a></center>
</body></html>
Example 1-3
introduces the component call tag syntax, <& &>, which is
used to call another component and insert its output into the surrounding
text. The component tag can also accept arguments, which in this case can
help unify site design by moving the page header text into the header.mas


</body></html>
In the mainpage.mas
component in Example 1-6, the arguments are passed
to the header.mas
component by using standard Perl syntax (i.e., commas,
quotes, and the => operator). In fact, any Perl syntax for passing a list can be
used, because the argument list is specified in real Perl syntax.
Example 1-6. mainpage.mas
<& header.mas, head => "Wally World Home" &>
Here at Wally World you'll find all the latest
accoutrements.
<& footer.mas &>
Mason will take the list of arguments passed to the header.mas
component
and assign the proper values to the variables specified in the <%args>
block.
Object-Style Component Inheritance
Aside from the fact that there's a little bit of Perl thrown into the mix for
passing parameters, the examples we've seen don't really show anything that
you couldn't do using standard server-side include (SSI) techniques. In fact,
the usage demonstrated in these examples is relatively uncommon in
building Mason sites, because there are better ways to get the job done. One
of the greatest features of Mason is that components can inherit behavior
from other components, much like classes and objects in an object-oriented
hierarchy.
1
Typically, each component will inherit from a single component
called the autohandler . The autohandler implements general behavior for all
components, such as the content of headers and footers. Individual
components implement specific behavior, such as the body text of the

component property that inherits via Mason's component inheritance chain.
There are zillions of other uses for Mason's inheritance mechanism, which
will be further explored in Chapter 5
.
Intelligent Caching Mechanisms
Anyone who has built any dynamically generated web sites knows that
sometimes certain portions of a site can take longer to generate and serve
than you want to make your users wait. Furthermore, portions of a site might
be only "semidynamic," meaning that their content changes periodically but
stays static for a long time between changes. Alternatively, as might happen
on a news site or for an online poll, content may change continually, but a
lag time of a few minutes in updating the content would be acceptable if it
improves site performance. For cases like these, Mason provides a very
sophisticated caching mechanism that you can use to control how often the
output of a component is rebuilt. You can base the expiration decision on
time, on certain key parameters like username or content ID, or on an
explicit agent that decides when specific data has expired.
The caching mechanism can be used for the output of a component, for an
arbitrary block of text, or for any Perl data structure you might want to
cache. The first-class support for caching is one of Mason's most endearing
qualities, and you'll learn to appreciate it the first time it saves you from
spending hours optimizing sluggish code.
To aid overall performance, Mason also has an intelligent internal caching
mechanism. During execution, Mason turns each component into Perl source
code on disk, then compiles the Perl code into bytecode, then executes the
bytecode to produce the component's output. It would be a waste of
computing resources to repeat this cycle every time a component needs to be
executed, so Mason caches at each stage. As an aid to rapid development,
Mason will check your components' modification times and invalidate its
cache when you make changes to your components, ensuring that any

as importantly, different people find that different systems suit the way they
think better than others do.
There are generally two kinds of systems that people consider to be
alternatives to Mason: lightweight solutions and heavyweight solutions.
Lightweight solutions generally have the goal of being small and fast and
leave much of the major work up to you. They are often simple templating
modules like Text::Template or HTML::Template or even
homegrown templating schemes. Using templates is certainly a good idea,
and it is one of the core ideas in Mason itself. However, when designing an
entire site, you're usually going to need some more sophisticated system that
helps you manage your site-building resources; if you choose a templating-
only solution, you'll probably end up writing this management code yourself.
You may have a good idea of what such a system would entail only after
writing and maintaining dozens of complicated web sites, so you'd likely
spend more time working on your management code than on building your
sites. This is the main trade-off with lightweight solutions: you gain
flexibility because you can manage your site however you want, but since
the burden rests entirely on you, you might end up preferring to use a tool
that handles many of these management issues for you.
By contrast, heavyweight solutions implement several layers on top of their
templating capabilities. Despite some disagreement on proper use of the
term, "application server" is often used to describe such heavyweight
systems. Each anticipates the typical needs of a large, sophisticated web site
and provides methods for dealing with these situations cleanly. A
heavyweight system will typically have support for integrating a site with a
database, working with HTML and URLs, preserving state from one request
to the next, caching often-used resources, and dealing with error conditions.
Each heavyweight solution is tailored to different site requirements and
makes different assumptions about the best ways to deal with them.
Good solutions, such as Mason, Embperl, and Apache::ASP, also help

$cnt :
[- @k = qw(zero one two) -]
<table>
<tr>
<td>[+ $row +]</td>
<td>[+ $k[$row] +]</td>
</tr>
</table>
This would output:


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status