The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Text::MicroMason::Base - Simple Compiler for Mason Templating

SYNOPSIS

Create a Mason object to interpret the templates:

    use Text::MicroMason;
    my $mason = Text::MicroMason->new();

Use the execute method to parse and evalute a template:

    print $mason->execute( text=>$template, 'name'=>'Dave' );

Or compile it into a subroutine, and evaluate repeatedly:

    $coderef = $mason->compile( text=>$template );
    print $coderef->('name'=>'Dave');
    print $coderef->('name'=>'Bob');

Templates stored in files can be run directly or included in others:

    print $mason->execute( file=>"./greeting.msn", 'name'=>'Charles');

DESCRIPTION

The Text::MicroMason::Base class provides a parser and execution environment for a simple templating system based on HTML::Mason.

Template Syntax

The template syntax supported by Text::MicroMason and some useful template developer techniques are described in Text::MicroMason::Devel.

Public Methods

new()
  $mason = $class->new( %options );
  $clone = $mason->new( %options );

Creates a new instance with the provided key value pairs.

compile()
  $code_ref = $mason->compile( text => $template, %options );
  $code_ref = $mason->compile( file => $filename, %options );

Parses the provided template and converts it into a new Perl subroutine.

execute()
  $result = $mason->execute( text => $template, @arguments );
  $result = $mason->execute( file => $filename, @arguments );
  $result = $mason->execute( code => $code_ref, @arguments );

  $result = $mason->execute( $type => $source, \%options, @arguments );

Returns the results produced by the template, given the provided arguments.

Private Methods

The following internal methods are used to implement the public interface described above, and may be overridden by subclasses and mixins.

defaults

This class method is called by new() to provide key-value pairs to be included in the new instance.

lex
  @tokens = $mason->lex( $template );

Parses the provided template text and returns a list of token types and values.

assemble
  $perl_code = $mason->assemble( @tokens );

Assembles the parsed token series into the source code for the equivalent Perl subroutine.

assemble_args

Called by assemble(), this method provides support for Mason's <%args> blocks.

eval_sub
  $code_ref = $mason->eval_sub( $perl_code );

Compiles the Perl source code for a template using eval(), and returns a code reference.

resolve
  ( $type, $data ) = $mason->resolve( $type, $data );

Called by compile(), the resolve method allows the template source type and value arguments to be normalized or resolved in various ways before the template is read using one of the read_type() methods.

read_text
  $template = $mason->read_text( $template );

Called by compile() when the template source type is "text", this method simply returns the value of the text string passed to it.

read_file
  ( $contents, %path_info ) = $mason->read_file( $filename );

Called by compile() when the template source type is "file", this method reads and returns the contents of the named file.

debug_msg

Called to provide a debugging message for developer reference. No output is produced unless the object's 'debug' flag is true.

croak_msg

Called when a fatal exception has occured.

Private Functions

_printable
  $special_characters_escaped = _printable( $source_string );

Converts non-printable characters to readable form using the standard backslash notation, such as "\n" for newline.

Package Variables

$Defaults{ debug }

Boolean value. Debugging flag activates warns throughout the code. Used by debug_msg().

$Defaults{ assembler }

Reference to a hash of text elements used for Perl subroutine assembly. Used by assemble().

The assembly template defines the types of blocks supported and the order they appear in, as well as where other standard elements should go. Those other elements also appear in the assembler hash.

SEE ALSO

For a full-featured web application system using this template syntax, see HTML::Mason.

For distribution, installation, support, copyright and license information, see Text::MicroMason::ReadMe.