Perl code is marked with a % at the start of the
line:
% $now = localtime; # embedded Perl
This page was generated on <% $now %>.
Because substitutions can be almost any Perl code you like, this
could have been written more simply as:
This page was generated on <% scalar localtime %>.
If either of these variations were saved in
footer.mas, you could include it simply by
saying:
<& footer.mas &>
This is an example of a component call—Mason runs the component
and inserts its result into the document that made the call.
Block tags define different regions of your component.
<%perl> ... </%perl> identifies Perl
code. While % at the start of a line indicates
that just that line is Perl code, you can have any number of lines in
a <%perl> block.
A <%init> ... </%init> block is like
an INIT block in Perl. The code in the block is executed before the
main body of code. It lets you store definitions, initialization,
database connections, etc. at the bottom of your component, where
they're out of the way of the main logic.
The <%args> ... </%args> block lets
you define arguments to your component, optionally with default
values. For example, here's greet.mas:
<%args>
$name => "Larry"
$town => "Mountain View"
</%args>
Hello, <% $name %>. How's life in <% $town %>?
Calling it with:
<& greet.mas &>
emits:
Hello, Larry. How's life in Mountain View?
You can provide options on the component call:
<& greet.mas, name => "Nat", town => "Fort Collins" &>
That emits:
Hello, Nat. How's life in Fort Collins?
Because there are default values, you can supply only some of the
arguments:
<& greet.mas, name => "Bob" &>
That emits:
Hello, Bob. How's life in Mountain View?
Arguments are also how Mason components access form parameters. Take
this form:
<form action="compliment">
How old are you? <input type="text" name="age"> <br />
<input type="submit">
</form>
Here's a compliment component that could take
that parameter:
<%args>
$age
</%args>
Hi. Are you really <% $age %>? You don't look it!