Tài liệu Embedding Perl in HTML with Mason Chapter 2: Components- P2 doc - Pdf 87

Chapter 2: Components- P2

This example demonstrates all the syntax possibilities for this block. First of
all, we have argument types and names. The valid types are scalar, array,
and hash, represented by their corresponding Perl sigil ($, @, or %), exactly
as would be expected.
It is possible to give an argument a default value to be used if none is
provided when the component is called. 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.
An argument's default can refer to an earlier argument, so this is completely
legal:
<%args>
$x
$y => $x * 2 > 20 ? 50 : 100
</%args>
While this block looks as if it contains Perl, it is important to realize that its
syntax is actually something unique to Mason. Importantly, lines should not
end with a semicolon or comma, and each variable definition must be on a
single line.
It is possible to have comments both after an argument declaration and on
their own line. Comments start with the # character and continue to the end
of the line, just as in Perl. Blank lines are also allowed.
<%filter> blocks
A <%filter> 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. For example,
this filter uppercases all of the component's output:
<%filter>
s/(\w+)/\U$1/g
</%filter>

their name is cleanup .
Cleanup blocks are not executed if the component dies or aborts.
<%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. For example:
<%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. In the future Mason may do
something more useful with them.
<%doc>
=head1 My Story

This is the part where I tell you what the
component does. But I'd
rather tell you a story about my childhood. When
I was but a
child, my mother said to me ...
</%doc>
As you can see, there's no reason not to use POD (Perl's Plain Old
Documentation markup language) in these blocks, and you can even run
perldoc on a component file.
<%flags> and <%attr> blocks
These two blocks share the same syntax and are used to declare one or more
key/value pairs. The key can contain only letters, numbers, and the
underscore character ( _ ). The value can be any Perl expression whose

These two blocks use a syntax slightly different from any other Mason block
because their contents are, in turn, components. The <%def> block contains
a subcomponent, an embedded component that can be called via the normal
Mason component calling syntax. A <%method> block also contains an
embedded component, but one that may be inherited by a component's
children. <%def> and <%method> blocks require a name in the initial tag.
In the following example, a subcomponent named .make_a_link is
defined:
<%def .make_a_link>
<a href="<% $url %>"><% $text %></a>
<%args>
$path
%query => ( )
$text
</%args>
<%init>
my $url = ...
</%init>
</%def>
The name of a subcomponent or method may contain alphanumerics,
underscores ( _ ), dashes ( - ), or periods ( . ). Customarily, a period is the
first character of subcomponent names, in order to distinguish them from
nonembedded components. Methods generally do not follow this
convention; they have names without leading periods.
The main difference between subcomponents and methods is simply that
subcomponents are visible only within the component in which they are
defined, whereas methods are visible outside of the component and can be
inherited via Mason's component inheritance mechanism. Subcomponents
and methods are covered in Chapter 5
.

provides the ability to get rid of a newline simply by preceding it with a
backslash ( \ ).
If we rewrote the preceding example with escaped newlines, it would look
like this:
<pre>
I am\
% if ($height < 5) {


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