.\" Automatically generated by Pod::Man 2.27 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Mason::Manual::Components 3pm" .TH Mason::Manual::Components 3pm "2014-02-01" "perl v5.18.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Mason::Manual::Components \- The building blocks of Mason .SH "DESCRIPTION" .IX Header "DESCRIPTION" The \fIcomponent\fR \- a file with a mix of Perl and \s-1HTML \-\s0 is Mason's basic building block. Pages are usually formed by combining the output from multiple components. An article page for a online magazine, for example, might call separate components for the company masthead, ad banner, left table of contents, and article body. .PP .Vb 10 \& +\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& |Masthead | Banner Ad | \& +\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& | | | \& |+\-\-\-\-\-\-\-+|Text of Article ..| \& || || | \& ||Related||Text of Article ..| \& ||Stories|| | \& || ||Text of Article ..| \& |+\-\-\-\-\-\-\-+| | \& | +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& | | Footer | \& +\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ .Ve .PP The top level component decides the overall page layout. Individual cells are then filled by the output of subordinate components. Pages might be built up from as few as one, to as many as hundreds of components, with each component contributing a chunk of \s-1HTML.\s0 .PP Splitting up a page into multiple components gives you roughly the same benefits as splitting up an application into multiple classes: encapsulation, reusability, development concurrency, separation of concerns, etc. .PP Mason actually compiles components down to Perl/Moose classes, which means that many of the tools you use to develop regular classes \- profilers, debuggers, and the like \- can be used with Mason components with slight tweaking. .SH "COMPONENT FILES" .IX Header "COMPONENT FILES" .SS "The component root and component paths" .IX Subsection "The component root and component paths" When you use Mason, you specify a component root that all component files live under. Thereafter, any component will be referred to by its virtual \fIpath\fR relative to the root, rather than its full filename. .PP For example, if the component root is '/opt/web/comps', then the component path \&'/foo/bar.mc' refers to the file '/opt/web/comps/foo/bar.mc'. .PP It is also possible to specify multiple component roots, ala Perl's \f(CW@INC\fR, in which case a component path might refer to one of several files. .SS "Component file extensions" .IX Subsection "Component file extensions" By default Mason facilitates and enforces standard file extensions for components. .IP ".mc \- top-level component" 4 .IX Item ".mc - top-level component" A top-level component can serve as the page component in a request. .IP ".mi \- internal component" 4 .IX Item ".mi - internal component" An internal component can only be accessed from other components. .IP ".mp \- pure-perl component" 4 .IX Item ".mp - pure-perl component" A pure-perl component contains only code; it is parsed as if its entire content was within a \f(CW%class\fR block. You do not need to (and are not allowed to) include Mason tags in this component, and it will not produce any output if called. This is just a way of defining a class that other components can easily interact with and extend. Some applications include: controller logic, web form handlers, and autobase components. .PP These extensions are configurable via \*(L"pure_perl_extensions\*(R" in Mason::Interp and \&\*(L"top_level_extensions\*(R" in Mason::Interp. .SH "CALLING COMPONENTS" .IX Header "CALLING COMPONENTS" The initial component in a request, called the page component, is called from run, which in turn may be called from a \s-1PSGI\s0 handler or an web framework view depending on your setup. See Mason::Manual::RequestDispatch for more information about how the page component is chosen. .PP A component can call another component with the <& &> tag: .PP .Vb 1 \& <& /path/to/comp.mi, name=>value, ... &> .Ve .PP or via the comp or scomp methods: .PP .Vb 4 \& <%init> \& $m\->comp(\*(Aq/some/component.mi\*(Aq, foo => 5); \& my $output = $m\->scomp(\*(Aq/some/other/component.mi\*(Aq); \& .Ve .PP From the implementation perspective, calling a component means creating a new instance of the component's class with the specified parameters, and then calling method \f(CW\*(C`handle\*(C'\fR (for the page component) or \f(CW\*(C`main\*(C'\fR (for an internal component) on the instance. .SH "ATTRIBUTES" .IX Header "ATTRIBUTES" You can declare attributes in components and pass them when calling components. .SS "Declaring attributes" .IX Subsection "Declaring attributes" Use Moose 'has' syntax to declare attributes within a \f(CW\*(C`<%class>\*(C'\fR section: .PP .Vb 5 \& <%class> \& has \*(Aqfoo\*(Aq; \& has \*(Aqbar\*(Aq => (required => 1); \& has \*(Aqbaz\*(Aq => (isa => \*(AqInt\*(Aq, default => 17); \& .Ve .SS "Attributes are read-write by default" .IX Subsection "Attributes are read-write by default" Mason::Component::Moose imports MooseX::HasDefaults::RW into all components, which makes attributes read-write unless stated otherwise. This is not considered best practice for general \s-1OO\s0 programming, but component instances are short-lived and not usually accessed outside of their class so we feel the convenience is warranted. .SS "Accessing attributes" .IX Subsection "Accessing attributes" A declared attribute 'foo' can be accessed inside the component via the Perl6\-ish syntax .PP .Vb 1 \& $.foo .Ve .PP which is transformed by DollarDot to .PP .Vb 1 \& $self\->foo .Ve .PP In the rest of this documentation we will use \f(CW$.\fR notation, but feel free to substitute \f(CW\*(C`$self\->\*(C'\fR conceptually and/or in reality. .PP To set the attribute, you must use: .PP .Vb 1 \& $.foo(5); .Ve .PP unless you're using LvalueAttributes, in which case you can say .PP .Vb 1 \& $.foo = 5; .Ve .PP \&\f(CW\*(C`$.args\*(C'\fR will return a hashref of all of the parameters passed to the component when it was created/called, regardless of whether they correspond to declared attributes. .SH "METHODS" .IX Header "METHODS" The base component class, Mason::Component, has but a few built-in methods: handle, render, wrap, main, m, and cmeta. .PP The \f(CW\*(C`main\*(C'\fR method contains the mix of \s-1HTML\s0 and Perl in the main part of the component. .PP You can add other methods that output \s-1HTML\s0 via the \f(CW\*(C`<$method>\*(C'\fR section; these methods automatically have access to \f(CW$self\fR and \f(CW$m\fR. .PP .Vb 6 \& <%method leftcol> \& \& \& ... \&
<% $foo %>
\& \& \& ... \& \& <% # call leftcol method and insert HTML here %> \& <% $.leftcol %> .Ve .PP Methods can also take argument lists: .PP .Vb 7 \& <%method list ($style, $items)> \& \& .Ve .PP Both \f(CW\*(C`main\*(C'\fR and other methods defined with \f(CW\*(C`<%method>\*(C'\fR automatically get a \f(CW\*(C`return undef\*(C'\fR at their end, so that they don't accidentally return values. .PP Pure-Perl methods that return a value can be added within the << <%class> >> section. .PP .Vb 5 \& <%class> \& method multiply ($a, $b) { \& return $a * $b; \& } \& \& \& ... \& \& <%init> \& my $value = $.multiply(5, 6); \& .Ve .PP Note that Method::Signatures::Simple provides the \f(CW\*(C`method\*(C'\fR keyword and argument lists; this is used throughout Mason internals as well. If you prefer straight-up Perl subroutines: .PP .Vb 6 \& <%class> \& sub multiply { \& my ($self, $a, $b) = @_; \& return $a * $b; \& } \& .Ve .SS "Output versus return value" .IX Subsection "Output versus return value" Most Mason methods output content such as \s-1HTML.\s0 The content is not actually returned, but is instead appended to an implicit buffer. This is slightly more complicated but is necessary for supporting streaming applications. .PP When Mason generates \f(CW\*(C`main\*(C'\fR and other methods declared with \f(CW\*(C`<%method>\*(C'\fR, it puts an implicit .PP .Vb 1 \& return undef; .Ve .PP at the bottom of the method, so that unless you specify otherwise, there will be no return value. This is important because of syntactical shortcuts like .PP .Vb 2 \& <% inner() %> \& <% $.leftcol %> .Ve .PP which would (undesirably) print the return value if it existed. .SH "INHERITANCE" .IX Header "INHERITANCE" Each component class naturally inherits from (or 'extends') a superclass. The default superclass for components is Mason::Component, but this may be overridden in two ways: the \fIextends flag\fR and \fIautobase components\fR. .SS "Extends flag" .IX Subsection "Extends flag" A component can declare its superclass via the \f(CW\*(C`extends\*(C'\fR flag: .PP .Vb 3 \& <%flags> \& extends => \*(Aq/some/other/component\*(Aq \& .Ve .PP The path may be absolute as shown above, or relative to the component's path. .PP Note that including a raw \f(CW\*(C`extends\*(C'\fR keyword in a \f(CW\*(C`<%class>\*(C'\fR section will not work reliably. .SS "Autobase components" .IX Subsection "Autobase components" Autobase components are specially named components that automatically become the superclass of all components in their directory and subdirectories. The default names are \*(L"Base.mp\*(R" and \*(L"Base.mc\*(R" \- you can customize this with the \&\f(CW\*(C`autobase_names\*(C'\fR parameter. .PP For example, in this directory hierarchy, .PP .Vb 9 \& Base.mp \& main.mc \& colors/ \& red.mc \& blue.mc \& flavors/ \& Base.mc \& vanilla.mc \& chocolate.mc .Ve .PP assuming that no components have \f(CW\*(C`extends\*(C'\fR flags, .IP "\(bu" 4 /Base.mp is the superclass of /main.mc, /colors/red.mc, /colors/blue.mc, and /flavors/Base.mc. .IP "\(bu" 4 /flavors/Base.mc is the superclass of vanilla.mc and chocolate.mc. .PP If \f(CW\*(C`Base.mp\*(C'\fR and \f(CW\*(C`Base.mc\*(C'\fR appear in the same directory, they will both be recognized; everything below will inherit from \f(CW\*(C`Base.mc\*(C'\fR, and \f(CW\*(C`Base.mc\*(C'\fR will inherit from \f(CW\*(C`Base.mp\*(C'\fR. This might be useful for separating content wrapping from shared method definitions, for example. .SH "GENERATED CLASS" .IX Header "GENERATED CLASS" It can be helpful to understand how Mason generates component classes, especially for troubleshooting unexpected component behavior. .SS "Object files" .IX Subsection "Object files" Mason writes the generated class into an \fIobject file\fR, located in .PP .Vb 1 \& /obj/.mobj .Ve .PP For example if your data directory is \&\fI/home/myapp/data\fR and the component path is \fI/foo/bar.mc\fR, the corresponding object file will be .PP .Vb 1 \& /home/myapp/data/obj/foo/bar.mc.mobj .Ve .PP The object file is rewritten whenever Mason detects a change in the source file. .PP Object files aren't generated in a particularly clean way, so if you're going to be peeking at them, consider using the TidyObjectfiles plugin. .SS "Class name" .IX Subsection "Class name" The class name is determined at load time by prepending the \&\f(CW\*(C`Mason::Interp/component_class_prefix\*(C'\fR to the component path, which slashes replaced with '::'. Two different Interp objects loading the same object file will thus create two separate classes. .SS "A simple example" .IX Subsection "A simple example" Here's a simple component: .PP .Vb 1 \& Hello world! The local time is <% scalar(localtime) %>. .Ve .PP and here's the class that gets generated for it, filtered with \&\f(CW\*(C`TidyObjectFiles\*(C'\fR: .PP .Vb 10 \& 1 use Mason::Component::Moose; \& 2 our ( $m, $_m_buffer ); \& 3 *m = \e$Mason::Request::current_request; \& 4 *_m_buffer = \e$Mason::Request::current_buffer; \& 5 sub _inner { inner() } \& 6 my $_class_cmeta; \& 7 \& 8 method _set_class_cmeta ($interp) { \& 9 $_class_cmeta = $interp\->component_class_meta_class\->new( \& 10 \*(Aqclass\*(Aq => CLASS, \& 11 \*(Aqdir_path\*(Aq => \*(Aq/\*(Aq, \& 12 \*(Aqinterp\*(Aq => $interp, \& 13 \*(Aqis_top_level\*(Aq => \*(Aq1\*(Aq, \& 14 \*(Aqobject_file\*(Aq => _\|_FILE_\|_, \& 15 \*(Aqpath\*(Aq => \*(Aq/hi.mc\*(Aq, \& 16 \*(Aqsource_file\*(Aq => \*(Aq/home/myapp/comps/hi.mc\*(Aq, \& 17 ); \& 18 } \& 19 sub _class_cmeta { $_class_cmeta } \& 20 \& 21 method main { \& 22 #line 1 "/home/myapp/comps/hi.mc" \& 23 $$_m_buffer .= \*(AqHi there! The time is \*(Aq; \& 24 #line 1 "/home/myapp/comps/hi.mc" \& 25 for ( scalar( scalar(localtime) ) ) { $$_m_buffer .= $_ if defined } \& 26 #line 1 "/home/myapp/comps/hi.mc" \& 27 $$_m_buffer .= \*(Aq. \& 28 \*(Aq; \& 29 \& 30 return; \& 31 } .Ve .PP (Caveat: the above is as of time of writing and may well be out of date with the current code generator, but it is accurate enough for explanatory purposes.) .PP Line 1 brings in Mason::Component::Moose, which imports Moose, \s-1CLASS\s0, Method::Signatures::Simple and other things into the current package. .PP Lines 2\-4 defines two dynamic globals, \f(CW$m\fR (the current request) and \&\f(CW$_m_buffer\fR (the current output buffer). These are aliased so that they can be changed for every component from a single place. .PP Lines 6\-19 create the Mason::Component::ClassMeta object returned from cmeta. .PP Lines 21\-31 contain the main method, which encapsulates all the output and Perl statements in the component that aren't explicitly inside a \f(CW\*(C`<%method>\*(C'\fR or \f(CW\*(C`<%class>\*(C'\fR block. .PP Lines 22, 24, and 26 contain '#line' statements which make error messages appear to come from the source file rather than the object file (and hence more useful). This can be disabled with no_source_line_numbers. .PP Lines 23, 25, and 27 output plain strings or the results of code by appending them to the current output buffer. The current output buffer can change within a request, for example when capture or scomp is called. .PP Two things that would be in a normal class are missing above: the \f(CW\*(C`package\*(C'\fR and \f(CW\*(C`extends\*(C'\fR declarations. These are added dynamically when the object file is evaluated. .SH "SEE ALSO" .IX Header "SEE ALSO" Mason .SH "AUTHOR" .IX Header "AUTHOR" Jonathan Swartz .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2012 by Jonathan Swartz. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.