Text::MicroMason(3pm)
π Text::MicroMason π
hey bestie!! this is the manual page for Text::MicroMason(3pm) β Simple and Extensible Templating. grab a lil snack, we're gonna go through it together π
β¨ NAME β¨
Text::MicroMason β Simple and Extensible Templating π
β¨ SYNOPSIS β¨
okay so Mason syntax is literally the moment β it gives you so many ways to mix Perl into a text template π¦
<%args>
$name
</%args>
% if ( $name eq 'Dave' ) {
I'm sorry <% $name %>, I'm afraid I can't do that right now.
% } else {
<%perl>
my $hour = (localtime)[2];
my $daypart = ( $hour > 11 ) ? 'afternoon' : 'morning';
</%perl>
Good <% $daypart %>, <% $name %>!
% }
make yourself a MicroMason object to interpret those templates π
use Text::MicroMason;
$mason = Text::MicroMason->new();
then use the compile method to turn templates into subroutines π₯
$coderef = $mason->compile( text=>$template );
print $coderef->('name'=>'Alice');
orrrr use the execute method to parse and evaluate in one single call, slay β¨
print $mason->execute( text=>$template, 'name'=>'Bob' );
templates living in files can be run directly or included in others, no cap π«Ά
print $mason->execute( file=>"./greeting.msn", 'name'=>'Charles');
want extra features? pick mixin classes to add to your MicroMason object π
$mason = Text::MicroMason->new( qw( -CatchErrors -Safe -Filters ) );
and if you'd rather skip method calls entirely, you can import various functions πΈ
use Text::MicroMason::Functions qw( compile execute );
print execute($template, 'name'=>'Dave');
$coderef = compile($template);
print $coderef->('name'=>'Bob');
β¨ DESCRIPTION β¨
Text::MicroMason interpolates blocks of Perl code embedded into text strings. it's giving template compiler energy π
every MicroMason object acts as a "template compiler," which converts templates from text-with-embedded-code formats into ready-to-execute Perl subroutines. iconic. π
π MicroMason Initialization π
use the new() method to create a Text::MicroMason object with the appropriate mixins and attributes π
$mason = Text::MicroMason->new( %attribs );
you can pass attributes as key-value pairs to the new() method to save various options for later use by the compile() method. lowkey handy fr fr β¨
π Template Compilation π
to compile a text template, pass it to the compile() method to produce a new Perl subroutine to be returned as a code reference π
$code_ref = $mason->compile( $type => $source, %attribs );
any attributes you hand to compile() will temporarily override the persistent options defined by new() β for that template only, bestie. no drama π
you can provide the template as a text string, a file name, or an open file handle π¦
$code_ref = $mason->compile( text => $template );
$code_ref = $mason->compile( text => \$template );
$code_ref = $mason->compile( file => $filename );
$code_ref = $mason->compile( handle => $fh );
$code_ref = $mason->compile( handle => \*FILE );
template files are just plain text files that contains the string to be parsed. the files may have any name and extension you wish, literally whatever you want π the filename specified can either be absolute or relative to the program's current directory.
π Template Execution π
to execute the template and obtain the output, call a compiled function β¨
$result = $code_ref->( @arguments );
(note that the$code_ref->()syntax is unavailable in older versions of Perl; use the equivalent&$code_ref()syntax instead.) just so you know, bestie π
as a shortcut, the execute method compiles and runs the template one time π«
$result = $mason->execute( $type => $source, @arguments );
$result = $mason->execute( $type => $source, \%attribs, @arguments );
π Argument Passing π
you can pass arguments to a template subroutine using positional or named arguments, your choice babe πΈ
for positional arguments, pass the argument list and read from @_ as usual:
$mason->compile( text=>'Hello <% shift(@_) %>.' )->( 'Dave' );
for named arguments, pass in a hash of key-value pairs to be made accessible in an %ARGS hash within the template subroutine:
$mason->compile( text=>'Hello <% $ARGS{name} %>.' )->( name=>'Dave' );
additionally, you can use named arguments with the %args block syntax π«Ά
$mason->compile( text=>'<%args>$name</%args>Hello <% $name %>.' )->( name=>'Dave' );
π Mixin Selection π
arguments passed to new() that begin with a dash will be added as mixin classes π
$mason = Text::MicroMason->new( -Mixin1, %attribs, -Mixin2 );
every MicroMason object inherits from an abstract Base class and some set of mixin classes. by combining mixins you can create subclasses with the desired combination of features. see Text::MicroMason::Base for documentation of the base class, including private methods and extension mechanisms. iykyk π
if you call the new method on Text::MicroMason, it automatically includes the HTMLMason mixin, which provides the standard template syntax. if you want to create an object without the default HTMLMason functionality, call Text::MicroMason::Base->new() instead. plot twist π
some mixins define the syntax for a particular template format. you will generally need to select one, and only one, of the mixins listed in "TEMPLATE SYNTAXES". one. only one. don't be greedy bestie π
other mixins provide optional functionality. those mixins may define additional public methods, and may support or require values for various additional attributes. for a list of such mixin classes, see "MIXIN FEATURES". β
β¨ TEMPLATE SYNTAXES β¨
templates contain a mix of literal text to be output with some type of markup syntax which specifies more complex behaviors π¦
the Text::MicroMason::HTMLMason mixin is selected by default. to enable an alternative, pass its name to Text::MicroMason::Base->new( -MixinName ). periodt π
π HTMLMason π
the HTMLMason mixin provides lexer and assembler methods that handle most elements of HTML::Mason's template syntax. main character energy fr fr π
my $mason = Text::MicroMason::Base->new( -HTMLMason );
my $output = $mason->execute( text => $template, name => 'Bob' );
<%args>
$name => 'Guest'
</%args>
% if ( $name eq 'Dave' ) {
I'm sorry <% $name %>, I'm afraid I can't do that right now.
% } else {
<%perl>
my $hour = (localtime)[2];
my $daypart = ( $hour > 11 ) ? 'afternoon' : 'morning';
</%perl>
Good <% $daypart %>, <% $name %>!
% }
<& "includes/standard_footer.msn" &>
<%doc>
Here's a private developr comment describing this template.
</%doc>
for a definition of the template syntax, see Text::MicroMason::HTMLMason. π
π DoubleQuote π
the DoubleQuote mixin uses Perl's double-quoting interpolation as a minimalist syntax for templating β¨
my $mason = Text::MicroMason::Base->new( -DoubleQuote );
my $output = $mason->execute( text => $template, name => 'Bob' );
${ $::hour = (localtime)[2];
$::daypart = ( $::hour > 11 ) ? 'afternoon' : 'morning';
\'' }
Good $::daypart, $ARGS{name}!
for more information see Text::MicroMason::DoubleQuote. πΈ
π Embperl π
the Embperl mixin support a template syntax similar to that used by the HTML::Embperl module π¦
my $mason = Text::MicroMason::Base->new( -Embperl );
my $output = $mason->execute( text => $template, name => 'Bob' );
[- my $name = $ARGS{name}; -]
[$ if $name eq 'Dave' $]
I'm sorry [+ $name +], I'm afraid I can't do that right now.
[$ else $]
[-
my $hour = (localtime)[2];
my $daypart = ( $hour > 11 ) ? 'afternoon' : 'morning';
-]
Good [+ $daypart +], [+ $name +]!
[$ endif $]
for more information see Text::MicroMason::Embperl. π
π HTMLTemplate π
the HTMLTemplate mixin supports a syntax similar to that used by the HTML::Template module π
my $mason = Text::MicroMason::Base->new( -HTMLTemplate );
my $output = $mason->execute( text => $template, name => 'Bob' );
<TMPL_IF NAME="user_is_dave">
I'm sorry <TMPLVAR NAME="name">, I'm afraid I can't do that right now.
<TMPL_ELSE>
<TMPL_IF NAME="daytime_is_morning">
Good morning, <TMPLVAR NAME="name">!
<TMPL_ELSE>
Good afternoon, <TMPLVAR NAME="name">!
</TMPL_IF>
</TMPL_IF>
for more information see Text::MicroMason::HTMLTemplate. β¨
π ServerPages π
the ServerPages mixin supports a syntax similar to that used by the Apache::ASP module π«
my $mason = Text::MicroMason::Base->new( -ServerPages );
my $output = $mason->execute( text => $template, name => 'Bob' );
<% my $name = $ARGS{name};
if ( $name eq 'Dave' ) { %>
I'm sorry <%= $name %>, I'm afraid I can't do that right now.
<% } else {
my $hour = (localtime)[2];
my $daypart = ( $hour > 11 ) ? 'afternoon' : 'morning';
%>
Good <%= $daypart %>, <%= $name %>!
<% } %>
for more information see Text::MicroMason::ServerPages. π«Ά
π Sprintf π
the Sprintf mixin uses Perl's sprintf formatting syntax for templating π
my $mason = Text::MicroMason::Base->new( -Sprintf );
my $output = $mason->execute( text => $template, 'morning', 'Bob' );
Good %s, %s!
for more information see Text::MicroMason::Sprintf. β
π TextTemplate π
the TextTemplate mixin supports a syntax similar to that used by the Text::Template module π§
my $mason = Text::MicroMason::Base->new( -TextTemplate );
my $output = $mason->execute( text => $template, name => 'Bob' );
{ $hour = (localtime)[2];
$daypart = ( $hour > 11 ) ? 'afternoon' : 'morning';
'' }
Good { $daypart }, { $name }!
for more information see Text::MicroMason::TextTemplate. π
π MIXIN FEATURES π
Okay bestie, here's the tea: these mixin classes are like little accessories you layer onto your MicroMason object to unlock extra functionality. It's giving build-your-own-queen. π
To add a mixin's functionality, pass it's name with a dash to the new() method:
$mason = Text::MicroMason->new( -CatchErrors, -PostProcess );
β¨ AllowGlobals β¨
Enables access to a set of package variables to be shared with templates.
For details see Text::MicroMason::AllowGlobals.
π CatchErrors π
Both compilation and run-time errors in your template are handled as fatal exceptions. To prevent a template error from ending your program, enclose it in an eval block:
my $result = eval { $mason->execute( text => $template ) };
if ( $@ ) {
print "Unable to execute template: $@";
} else {
print $result;
}
To transparently add this functionality to your MicroMason object, see Text::MicroMason::CatchErrors.
β‘ CompileCache β‘
Calling execute repeatedly will be slower than compiling once and calling the template function repeatedly, unless you enable compilation caching.
For details see Text::MicroMason::CompileCache.
π¦ Debug π¦
When trying to debug a template problem, it can be helpful to watch the internal processes of template compilation. This mixin adds controllable warning messages that show the intermediate parse information.
For details see Text::MicroMason::Debug.
π§ LineNumbers π§
Provide better line numbers when compilation fails, at the cost of potentially slower compilation and execution.
For details see Text::MicroMason::LineNumbers.
π ExecuteCache π
Each time you execute the template all of the logic will be re-evaluated, unless you enable execution caching, which stores the output of each template for each given set of arguments.
For details see Text::MicroMason::ExecuteCache.
πΈ Filters πΈ
HTML::Mason provides an expression filtering mechanism which is typically used for applying HTML and URL escaping functions to output.
Text::MicroMason->new(-Filters)->compile( text => $template );
<p> Hello <% $name |h %>!
The Filters mixin provides this capability for Text::MicroMason templates. To select it, add its name to your Mason initialization call:
my $mason = Text::MicroMason->new( -Filters );
Output expressions may then be followed by "|h" or "|u" escapes; for example this line would convert any ampersands in the output to the equivalent HTML entity:
Welcome to <% $company_name |h %>
For more information see Text::MicroMason::Filters.
π«Ά PassVariables π«Ά
Allows you to pass arguments to templates as variables instead of the basic argument list.
For details see Text::MicroMason::PostProcess.
π PostProcess π
Allows you to specify one or more functions through which all template output should be passed before it is returned.
For details see Text::MicroMason::PostProcess.
π₯ Safe π₯
By default, the code embedded in a template has access to all of the capabilities of your Perl process, and could potentially perform dangerous activities such as accessing or modifying files and starting other programs.
If you need to execute untrusted templates, use the Safe module, which can restrict the operations and data structures that template code can access.
To add this functionality to your MicroMason object, see Text::MicroMason::Safe.
π TemplateDir π
The filenames passed to the compile() or execute() methods can be looked up relative to a base directory path or the current template file.
To add this functionality to your MicroMason object, see Text::MicroMason::TemplateDir.
β TemplatePath β
The filenames passed to the compile() or execute() methods are looked up relative to a list of multiple base directory paths, in order. It tries as hard as possible to maintain compatibility with caching and <& &> template includes.
To add this functionality to your MicroMason object, see Text::MicroMason::TemplatePath.
π OTHER INTERFACES π
π Function Exporter π
Importable functions are provided for users who prefer a procedural interface.
The supported functions are listed in Text::MicroMason::Functions. (For backwards compatibility, those functions can also be imported from the main Text::MicroMason package.)
π« Template Frameworks π«
Adaptor modules are available to use MicroMason from within other frameworks. For more information, see Any::Template::Backend::Text::MicroMason and Catalyst::View::MicroMason.
β¨ Inline β¨
MicroMason templates can be embedded within your source code using Inline. For more information, see Inline::Mason.
π EXCEPTIONS π
Text::MicroMason croaks on error, with an appropriate error string. Some commonly occurring error messages are described below (where %s indicates variable message text). See also the pod for each mixin class, for additional exception strings that may be thrown.
MicroMason parsing halted at %s
Indicates that the parser was unable to finish tokenising the source text. Generally this means that there is a bug somewhere in the regular expressions used by lex().
(If you encounter this error, please feel free to file a bug report or send an example of the error to the author using the addresses below, and I'll attempt to correct it in a future release.)
MicroMason compilation failed: %s
The template was parsed successfully, but the Perl subroutine declaration it was converted to failed to compile. This is generally a result of a syntax error in one of the Perl expressions used within the template.
Error in template subroutine: %s
Additional diagnostic for compilation errors, showing the text of the subroutine which failed to compile.
Error in template file %s, interpreted as: %s
Additional diagnostic for compilation errors in external files, showing the filename and the text of the subroutine which failed to compile.
MicroMason execution failed: %s
After parsing and compiling the template successfully, the subroutine was run and caused a fatal exception, generally because that some Perl code used within the template caused die() to be called (or an equivalent function like croak or confess).
MicroMason: filename is missing or empty
One of the compile or execute methods was called with an empty or undefined filename, or one of the compile_file or execute_file methods was called with no arguments.
MicroMason can't read from %s: %s
One of the compile_file or execute_file functions was called but we were unable to read the requested file, because the file path is incorrect or we have insufficient privileges to read that file.
[Reference / Object]
If the code inside the template throws a reference or object while calling die(), MicroMason will re-throw the reference to the calling code.
π SEE ALSO π
For distribution, installation, support, copyright and license information, see Text::MicroMason::Docs::ReadMe.
and that's the whole page bestie, we ate and left no crumbs π«Ά This was Perl v5.36.0, dated 2023-08-10, from the Text::MicroMason(3pm) page β go forth and template, you absolute icon πβ¨
π¦ where this page comes from
- debian trixie: libtext-micromason-perl 2.23-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libtext-micromason-perl 2.23-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtext-micromason-perl 2.23-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtext-micromason-perl 2.23-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms