Sie sind auf Seite 1von 35

Mason

An Introduction

About Mason

Mason was designed to build, organize and


maintain large web sites or other groups of
dynamically generated documents.
Mason is simply a mechanism for embedding
Perl code into plain text.
Mason is broadly applicable to any situation in
which fine control over document content is
required.

The Main Features of Mason

Components: Modular Design Elements

Object-Style Component Inheritance

Intelligent Caching Mechanisms

Integration with Apache and mod_perl

Components

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.
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.

Component

A component is a combination of text and


Mason-specific markup.
The markup sections may contain Perl code or
special Mason directives.

Basic Component Syntax

The markup language used for Mason


components contains a simple tag to do in-place
substitution of Perl expressions.

Substitution Tags: <% %>

Used to insert the results of a Perl expression


into your text.
The contents of the tag are evaluated in a list
context and joined together just as if they had
been passed as arguments to Perl's built-in
print() function.

Eg for Substitution Tags


<% Welcome $name ! %>
if $name has Dennis. The output will be as
Welcome Dennis !
<% $platform ? 'Windows' : 'Unix' %>
if $platform is 0 then Unix will be shown else
Windows will be shown.

Embed Perl Line

A line that starts with a percent sign ( % ).


The rest of that line (up to the newline
character) is interpreted as Perl code.
This percent sign cannot be preceded by any
horizontal whitespace such as spaces or tabs.
A typical use of these lines is to implement Perl
control structures.

Eg for Embedded Perl Line


% my @dt = localtime;
Now it is <% $dt[2].':'.$dt[1].':'.$dt[0] %> (EST)
Type the code in a file with extension cmp.

http://www.diya-gachar.bl.hostrehearsal.com/test/eg/page001.cmp

Eg for Embedded Perl Line


% my $no = 5;
% foreach my $i(1..10) {
<br><% $no %> x <% $i %> = <% $no*$i %>
%}

http://www.diya-gachar.bl.hostrehearsal.com/test/eg/page002.cmp

Embed Perl Block

It begins with the start tag <%perl> and ends


with the end tag </%perl>
If more than a few lines of Perl code, it is best
to use a Perl block instead.

<%perl>
my %emp = ( id => 605 , name => 'Guruprasad' ,
post => 'Software Engineer' );
foreach my $data (keys %emp ){
print "$data : $emp{$data}<br>";
}
</%perl>
http://www.diya-gachar.bl.hostrehearsal.com/test/eg/page003.cmp

Calling Other Components

Syntax : <& cmp filename &>


Ability of one Component calling another
component.
The called component's output to appear inside
the calling component's output.
The called component can, in turn, call other
components, and so on.

Eg for calling component


page004h.cmp

<html>
<head>
<title>
Testing Header
</title>
Header Page - Hello
</head>
<body bgcolor='aqua'>
<hr>

page004f.cmp

</body>
<hr>
For further details contact ....
</html>
page004.cmp

<& page004h.cmp &>


This is content of the page shown from page004.cmp
% foreach my $no (1..10){
<br><% $no %>
%}
<& page004f.cmp &>
http://www.diya-gachar.bl.hostrehearsal.com/test/eg/page004.cmp

$m

Special Globals

It is an HTML::Mason::Request object

Through methods of the object one can

retrieve information on the current request

call other components

affect the flow of execution

$r

If Mason is running under mod_perl, all components


also have access to the Apache request object via the
global variable $r.

Alternate way to call component

An alternative to the <& &> syntax for calling other


components is the $m->comp() method.
The $m variable contains the
HTML::Mason::Request object for the current
request

% $m->comp('page004h.cmp');
This is content of the page shown from
page004.cmp
% foreach my $no (1..10){
<br><% $no %>
%}
% $m->comp('page004f.cmp');

http://www.diya-gachar.bl.hostrehearsal.com/test/eg/page004b.cmp

args block

It is a component's input argument


declarations
Components can take a variety of arguments,
from either an external source (an HTTP
request, for example) or an internal one (one
component calling another).
Declare the names and datatypes of the
arguments that a component expects, as well
as default values for these arguments via the <
%args> block

<%args>
$no
</%args>
% foreach my $i(1..10){
<br><% $no %> x <% $i %> = <% $no*$i %>
%}

http://www.diya-gachar.bl.hostrehearsal.com/test/eg/page005.cmp?no=8

Any argument without a default is considered a


required argument.
Calling a component without specifying all its
required arguments will cause a fatal exception
to be thrown.

http://www.diya-gachar.bl.hostrehearsal.com/test/eg/page005.cmp

<%args>
$no => 5
</%args>
% foreach my $i(1..10){
<br><% $no %> x <% $i %> = <% $no*$i %>
%}
http://www.diya-gachar.bl.hostrehearsal.com/test/eg/page006.cmp

Pass arguements from


component to another component
<%args>
$no => 5
</%args>
<& page006.cmp, no => $no &>
http://www.diya-gachar.bl.hostrehearsal.com/test/eg/page007.cmp?no=4

<%args>
$no => 5
</%args>
% $m->comp('page006.cmp', no => $no);
http://www.diya-gachar.bl.hostrehearsal.com/test/eg/page008.cmp

<%init> blocks

The Perl code it contains is run before any


other code except for code in <%once> or <
%shared> blocks.

It is run every time the component is called.

It is typically used for doing things like


checking arguments, creating objects, or
retrieving data from a database.

The variables created here are used in


substitutions and perl lines throughout the rest
of the component.

Eg for init block


<%args>
$username => 'test'
</%args>
UserName : <% $username %><br>
Name : <% $user->personal_name %><br>
Host Name : <% $user->hostname %><br>
Status : <% $user->exists ? "Active" : "Deleted" %>
<%init>
use BL::User;
my $user = BL::User->new($username);
</%init>
http://www.diya-gachar.bl.hostrehearsal.com/test/eg/page009.cmp?username=pow.testrajesh

<%filter> blocks

This block is called after a component has


finished running.

It is given the entire output of the component


in the $_ variable, and any changes to this
variable are reflected in the output of the
component.

Eg for <%filter> block


<%args>
$username => 'test'
</%args>
UserName : <% $username %><br>
Name : <% $user->personal_name %><br>
Host Name : <% $user->hostname %><br>
Status : <% $user->exists ? "Active" : "Deleted" %>
<%init>
use BL::User;
my $user = BL::User->new($username);
</%init>
<%filter>
s/(\w+)/\U$1/g
</%filter>
http://www.diya-gachar.bl.hostrehearsal.com/test/eg/page010.cmp?username=pow.testrajesh

<%once> blocks

This block is executed whenever the


component is loaded into memory.

It is executed before any other block (including


an <%init> block).

Any variables declared here remain in


existence (and in scope) until the component
is flushed from memory or the Perl interpreter
running Mason shuts down, whichever comes
first.

The <%once> section is useful for things like


creating database handles or instantiating
large, resource-intensive objects.

<%cleanup> blocks

The cleanup block is executed right


before the component exits and is the
counterpart to the <%init> block.

It useful to free resources used in the


component.

<%args>
$username => 'testrajesh'
</%args>
<table align='center' border=1>
<tr><th>ACL</th></tr>
% foreach my $data( @$acl_data ){
<tr><td><% $data->[0] %></td></tr>
%}
</table>
<%once>
use BL::SQL;
my $dbh_billing = BL::SQL::ConnectToDB('billing','','','db-report');
my $acl_cur = $dbh_billing->prepare( <<EOQ );
SELECT ProductName FROM ACL a,Product b
WHERE a.ProductID=b.ProductID AND a.UserName = ? AND a.Status=1
EOQ
</%once>
<%init>
$acl_cur->execute($username);
my $acl_data = $acl_cur->fetchall_arrayref;
</%init>
<%cleanup>
$acl_cur->finish;
</%cleanup>
http://www.diya-gachar.bl.hostrehearsal.com/test/eg/page011.cmp?username=testrajesh

<%text> blocks

The contents of this block are output exactly


as they are, without any parsing.

This if useful if you need to write a component


containing text about Mason.
<%text>
Substitution tags look like this: <% $var %>.
</%text>

<%doc> blocks

This block is intended for use by component


authors for documentation purposes.

Its contents are completely ignored.

One can use POD (Perl's Plain Old


Documentation markup language) in these
blocks

perldoc can be run on a component file

%ARGS

Each component body also has a lexically


scoped hash called %ARGS.

This hash contains all of the arguments with


which the component was called.

The %ARGS hash contains the arguments


exactly as they were passed to a component,
whether or not they are declared in the
<%args> block.

An argument declared as $color in the


<%args> block would therefore be available
via $ARGS{color}.

Any assignment of defaults by the <%args>


block is not visible in %ARGS; the values are
given exactly as they were passed.

the %ARGS hash is always present, but the


<%args> block is optional.

Declaring large number of arguements in an <


%args> block may be a bit unwieldy. In this
case, the %ARGS hash can be quite handy.

% foreach my $i(1..10){
<br><% $ARGS{no} %> x <% $i %> = <%
$ARGS{no}*$i %>
%}

http://www.diya-gachar.bl.hostrehearsal.com/test/eg/page012.cmp?no=9

Component return values

By default, Mason components return undef.


To return something else, you can add an
explicit return() statement inside that
component.
Perl's return() function will end processing of
the component, and any values specified will be
the return value of $m->comp().
A component may return either a scalar or a list.

Eg. Component returning value


page013.cmp
<%init>
my $amt = $ARGS{qty} * $ARGS{rate} ;
return $amt;
</%init>
page014.cmp
Amount is <% $amount %>
<%init>
my $amount = $m->comp('page013.cmp' , %ARGS);
</%init>
https://diya-gachar.bl.hostrehearsal.com/test/eg/page014.cmp?qty=5&rate=4

Autohandler

if all pages on a given site should use the same (or


similar) header and footer content

Das könnte Ihnen auch gefallen