HTML::Mason::Compiler(3)

NAME
HTML::Mason::Compiler - Compile Mason component source

SYNOPSIS
  package My::Funky::Compiler;

  use base qw(HTML::Mason::Compiler);

DESCRIPTION
The compiler starts the compilation process by calling its lexer's "lex" method and passing itself as the "compiler" parameter. The lexer then calls various methods in the compiler as it parses the component source.

PARAMETERS TO THE new() CONSTRUCTOR

allow_globals
    List of variable names, complete with prefix ("$@%"), that you intend to use as globals in components. Normally global variables are forbidden by "strict", but any variable mentioned in this list is granted a reprieve via a "use vars" statement. For example:

        allow_globals => [qw($DBH %session)]

    In a mod_perl environment, $r (the request object) is automatically added to this list.

default_escape_flags
    Escape flags to apply to all <% %> expressions by default. The current valid flags are

        h - escape for HTML ('<' => '&lt;', etc.)
        u - escape for URL (':' => '%3A', etc.)

    The developer can override default escape flags on a per-expression basis; see the escaping expressions section of the developer's manual.

    If you want to set multiple flags as the default, this should be given as a reference to an array of flags.

enable_autoflush
    True or false, default is true. Indicates whether components are compiled with support for autoflush. The component can be compiled to a more efficient form if it does not have to check for autoflush mode, so you should set this to 0 if you can.

lexer
    The Lexer object to associate with this Compiler. By default a new object of class lexer_class will be created.

lexer_class
    The class to use when creating a lexer. Defaults to HTML::Mason::Lexer.

preprocess
    Sub reference that is called to preprocess each component before the compiler does it's magic. The sub is called with a single parameter, a scalar reference to the script. The sub is expected to process the script in-place. This is one way to extend the HTML::Mason syntax with new tags, etc., although a much more flexible way is to subclass the Lexer or Compiler class. See also postprocess_text and postprocess_perl.

postprocess_text
    Sub reference that is called to postprocess the text portion of a compiled component, just before it is assembled into its final subroutine form. The sub is called with a single parameter, a scalar reference to the text portion of the component. The sub is expected to process the string in-place. See also preprocess and postprocess_perl.

postprocess_perl
    Sub reference that is called to postprocess the Perl portion of a compiled component, just before it is assembled into its final subroutine form. The sub is called with a single parameter, a scalar reference to the Perl portion of the component. The sub is expected to process the string in-place. See also preprocess and postprocess_text.

use_source_line_numbers
    True or false, default is true. Indicates whether component line numbers that appear in error messages, stack traces, etc. are in terms of the source file instead of the object file. Mason does this by inserting '#line' directives into compiled components. While source line numbers are more immediately helpful, object file line numbers may be more appropriate for in-depth debugging sessions.

ACCESSOR METHODS
All of the above properties have read-only accessor methods of the same name.

You cannot change any property of a compiler after it has been created - among other things, this would potentially invalidate any existing cached component objects or object files. Your best bet is to create different compiler objects and load them into different interpreters.

METHODS
There are several methods besides the compilation callbacks below that a Compiler subclass needs to implement.

compile(...)
    This method has several parameters:

    •   comp_source (required)

        Either a scalar or reference to a scalar containing the component source.

    •   name (required)

        The name of the component. This should be the filename of the component if it is file-based, or some other clear identifier of the component source.

    •   comp_path (required)

        This should be the component's path.

    •   fh (optional)

        If this is given then the output of the compiler will be sent directly to this handle, rather than being buffered in memory. This is an optimization to avoid memory usage.

object_id
    This method should return a unique id for the given compiler object. This is used by the interpreter when determining the object directory, for example.

  Compilation Callbacks
These are methods called by the Lexer while processing a component source. You may wish to override some of these methods if you're implementing your own custom Compiler class.

start_component()
    This method is called by the Lexer when it starts processing a component.

end_component()
    This method is called by the Lexer when it finishes processing a component.

start_block(block_type => <string>)
    This method is called by the Lexer when it encounters an opening Mason block tag like "<%perl>" or "<%args>". Its main purpose is to keep track of the nesting of different kinds of blocks within each other. The type of block ("init", "once", etc.) is passed via the "block_type" parameter.

end_block(block_type => <string>)
    This method is called by the Lexer when it encounters a closing Mason block tag like "</%perl>" or "</%args>". Like "start_block()", its main purpose is to help maintain syntactic integrity.

*_block(block => <string>, [ block_type => <string> ])
    Several compiler methods like "doc_block()", "text_block()", and "raw_block()" are called by the Lexer after "start_block()" when it encounters blocks of certain types. These methods actually do the work of putting the body of a block into the compiled data structure.

    The methods that follow this pattern are "init_block()", "perl_block()", "doc_block()", "text_block()", and "raw_block()". The last method is called for all "<%once>", "<%cleanup>", "<%filter>", "<%init>", "<%perl>", and "<%shared>" blocks.

text(text => <string>)
    Inserts the text contained in a "text" parameter into the component for verbatim output.

    This is called when the lexer finds plain text in a component.

variable_declaration( type => <string>, name => <string>, default => <string> )
    Inserts a variable declaration from the "<%args>" section into the component.

    The type will be either "$", "@", or "%", indicating a scalar, array, or hash. The name is the variable name without the leading sigil. The default is everything found after the first "=>" on an "<%args>" block line, and may include a comment.

key_value_pair(block_type => <string>, key => <string>, value => <string>)
    Inserts a key-value pair from a "<%flags>" or "<%attr>" section into the component.

    The "block_type" parameter will be either "flags" or "attr".

start_named_block(block_type => <string>, name => <name>)
    Analogous to item_start_block, but starts a "named" block ("<%method>" or "<%def>").

end_named_block()
    Called by the Lexer to end a "named" block.

substitution(substitution => <string>, escape => <string>)
    Called by the Lexer when it encounters a substitution tag ("<% ... %>").

    The value of the "escape" parameter will be everything found after the pipe (|) in the substitution tag, and may be more than one character such as "nh".

component_call(call => <string>)
    Called by the Lexer when it encounters a component call tag without embedded content ("<& ... &>").

    The "call" parameter contains the entire contents of the tag.

component_content_call(call => <string>)
    Called by the Lexer when it encounters a component call tag with embedded content ("<&| ... &>").

component_content_call_end()
    Called by the Lexer when it encounters an ending tag for a component call with content ("</&>"). Note that there is no corresponding "component_call_end()" method for component calls without content, because these calls don't have ending tags.

perl_line(line => <string>)
    Called by the Lexer when it encounters a "%"-line.

SUBCLASSING
We recommend that any parameters you add to Compiler be read-only, because the compiler object_id is only computed once on creation and would not reflect any changes to Lexer parameters.

SEE ALSO
HTML::Mason, HTML::Mason::Admin, HTML::Mason::Interp