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

NAME

Text::MicroMason::Base - Abstract Compiler for Simple 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.

Public Methods

class()
  $class = Text::MicroMason::Base->class( @Mixins );

Creates a subclass of this package that also inherits from the other classes named.

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
  @token_pairs = $mason->lex( $template );

Parses the source text and returns a list of pairs of token types and values. Loops through repeated calls to lex_token().

lex_token
  ( $type, $value ) = $mason->lex_token();

Attempts to parse a token from the template text stored in the global $_ and returns a token type and value. Returns an empty list if unable to parse further due to an error.

Abstract method; must be implemented by subclasses.

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

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

assembler_rules()

Returns 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.

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.

NEXT

Enhanced superclass method dispatch for use inside mixin class methods. Allows mixin classes to redispatch to other classes in the inheritance tree without themselves inheriting from anything.

(This is similar to the functionality provided by NEXT::ACTUAL, but without using AUTOLOAD; for a more generalized approach to this issue see NEXT.)

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.

Supported Attributes

debug

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

Extending

You can add functionality to this module by creating subclasses. Key areas for subclass writers are:

resolve

You can intercept and re-write template source arguments by implementing this method.

read_*

You can support a new template source type by creating a method with a corresponding name prefixed by "read_". It is passed the template source value and should return the raw text to be lexed.

For example, if a subclass defined a method named read_from_db, callers could compile templates by calling ->compile( from_db => 'welcome-page' ).

lex

Replace this to parse a new template syntax. Is passed the text to be parsed and should return a list of $type => $token pairs.

assembler_rules

The assembler data structure is used to construct the Perl subroutine for a parsed template.

assemble_*

You can support a new token type be creating a method with a corresponding name prefixed by "assemble_". It is passed the token value or contents, and should return a new token pair that is supported by the assembler template.

For example, if a subclass defined a method named assemble_sqlquery, callers could compile templates that contained a <%sqlquery> ... </%sqlquery> block. The assemble_sqlquery method could return a perl = $statements> pair with Perl code that performed some appropriate action.

compile

You can wrap or cache the results of this method, which is the primary public interface.

execute

You typically should not depend on overriding this method because callers can invoke the compiled subroutines directly without calling execute.

SEE ALSO

For an overview of this templating framework, see Text::MicroMason.

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