NAME

Mason::Interp - Mason Interpreter

SYNOPSIS

my $interp = Mason->new(
    comp_root => '/path/to/comps',
    data_dir  => '/path/to/data',
    ...
);
my $output = $interp->run( '/request/path', foo => 5 )->output();

DESCRIPTION

Interp is the central Mason object, returned from Mason->new. It is responsible for creating new requests, compiling components, and maintaining the cache of loaded components.

PARAMETERS TO THE new() CONSTRUCTOR

allow_globals (varnames)

List of one or more global variable names that will be available in all components, like $m is by default.

allow_globals => [qw($dbh)]

As in any programming environment, globals should be created sparingly (if at all) and only when other mechanisms (parameter passing, attributes, singletons) will not suffice. Catalyst::View::Mason2, for example, creates a $c global set to the context object in each request.

Set the values of globals with set_global.

autobase_names

Array reference of autobase filenames to check in order when determining a component's superclass. Default is ["Base.mp", "Base.mc"].

autoextend_request_path

Whether to automatically add the top level extensions (by default ".mp" and ".mc") to the request path when searching for a matching page component. Defaults to true.

class_header

Perl code to be added at the top of the compiled class for every component, e.g. to bring in common features or import common methods. Default is the empty string.

# Add to the top of every component class:
#   use Modern::Perl;
#   use JSON::XS qw(encode_json decode_json);
#
my $mason = Mason->new(
    ...
    class_header => qq(
        use Modern::Perl;
        use JSON::XS qw(encode_json decode_json);
    ),
);

This is used by Mason::Compilation::output_class_header. For more advanced usage you can override that method in a subclass or plugin.

comp_root

Required. The component root marks the top of your component hierarchy and defines how component paths are translated into real file paths. For example, if your component root is /usr/local/httpd/docs, a component path of /products/sales.mc translates to the file /usr/local/httpd/docs/products/sales.mc.

This parameter may be either a single path or an array reference of paths. If it is an array reference, the paths will be searched in the provided order whenever a component path is resolved, much like Perl's @INC.

component_class_prefix

Prefix to use in generated component classnames. Defaults to 'MC' plus the interpreter's count, e.g. MC0. So a component '/foo/bar' would get a classname like 'MC0::foo::bar'.

data_dir

The data directory is a writable directory that Mason uses for various features and optimizations: for example, component object files and data cache files. Mason will create the directory on startup if necessary.

Defaults to a temporary directory that will be cleaned up at process end. This will hurt performance as Mason will have to recompile components on each run.

dhandler_names

Array reference of dhandler file names to check in order when resolving a top-level path. Default is ["dhandler.mp", "dhandler.mc"]. An empty list disables this feature.

index_names

Array reference of index file names to check in order when resolving a top-level path. Default is ["index.mp", "index.mc"]. An empty list disables this feature.

no_source_line_numbers

Do not put in source line number comments when generating code. Setting this to true will cause error line numbers to reflect the real object file, rather than the source component.

object_file_extension

Extension to add to the end of object files. Default is ".mobj".

plugins

A list of plugins and/or plugin bundles:

plugins => [
  'OnePlugin', 
  'AnotherPlugin',
  '+My::Mason::Plugin::AThirdPlugin',
  '@APluginBundle',
  '-DontLikeThisPlugin',
]);

See Mason::Manual::Plugins.

out_method

Default out_method passed to each new request.

pure_perl_extensions

A listref of file extensions of components to be considered as pure perl (see Pure Perl Components). Default is ['.mp']. If an empty list is specified, then no components will be considered pure perl.

static_source

True or false, default is false. When false, Mason checks the timestamp of the component source file each time the component is used to see if it has changed. This provides the instant feedback for source changes that is expected for development. However it does entail a file stat for each component executed.

When true, Mason assumes that the component source tree is unchanging: it will not check component source files to determine if the memory cache or object file has expired. This can save many file stats per request. However, in order to get Mason to recognize a component source change, you must touch the static_source_touch_file.

We recommend turning this mode on in your production sites if possible, if performance is of any concern.

static_source_touch_file

Specifies a filename that Mason will check once at the beginning of every request when in static_source mode. When the file timestamp changes (indicating that a component has changed), Mason will clear its in-memory component cache and recheck existing object files.

top_level_extensions

A listref of file extensions of components to be considered "top level", accessible directly from $interp->run or a web request. Default is ['.mp', '.mc']. If an empty list is specified, then there will be no restriction; that is, all components will be considered top level.

CUSTOM MASON CLASSES

These parameters specify alternate classes to use instead of the default Mason:: classes.

For example, to use your own Compilation base class:

my $interp = Mason->new(base_compilation_class => 'MyApp::Mason::Compilation', ...);

Relevant plugins, if any, will applied to this class to create a final class, which you can get with

$interp->compilation_class
base_code_cache_class

Specify alternate to Mason::CodeCache

base_compilation_class

Specify alternate to Mason::Compilation

base_component_class

Specify alternate to Mason::Component

base_component_moose_class

Specify alternate to Mason::Component::Moose

base_component_class_meta_class

Specify alternate to Mason::Component::ClassMeta

base_component_import_class

Specify alternate to Mason::Component::Import

base_request_class

Specify alternate to Mason::Request

base_result_class

Specify alternate to Mason::Result

PUBLIC METHODS

all_paths ([dir_path])

Returns a list of distinct component paths under dir_path, which defaults to '/' if not provided. For example,

$interp->all_paths('/foo/bar')
   => ('/foo/bar/baz.mc', '/foo/bar/blargh.mc')

Note that these are all component paths, not filenames, and all component roots are searched if there are multiple ones.

comp_exists (path)

Returns a boolean indicating whether a component exists for the absolute component path.

count

Returns the number of this interpreter, a monotonically increasing integer for the process starting at 0.

flush_code_cache

Empties the component cache and removes all component classes.

glob_paths (pattern)

Returns a list of all component paths matching the glob pattern. e.g.

$interp->glob_paths('/foo/b*.mc')
   => ('/foo/bar.mc', '/foo/baz.mc')

Note that these are all component paths, not filenames, and all component roots are searched if there are multiple ones.

load (path)

Returns the component object corresponding to an absolute component path, or undef if none exists. Dies with an error if the component fails to load because of a syntax error.

object_dir

Returns the directory containing component object files.

run ([request params], path, args...)

Creates a new Mason::Request object for the given path and args, and executes it. Returns a Mason::Result object, which is generally accessed to get the output. e.g.

my $output = $interp->run('/foo/bar', baz => 5)->output;

The first argument may optionally be a hashref of request parameters, which are passed to the Mason::Request constructor. e.g. this tells the request to output to standard output:

$interp->run({out_method => sub { print $_[0] }}, '/foo/bar', baz => 5);
set_global (varname, value)

Set the global varname to value. This will be visible in all components loaded by this interpreter. The variables must be on the allow_globals list.

$interp->set_global('$scalar', 5);
$interp->set_global('$scalar2', $some_object);

See also set_global.

MODIFIABLE METHODS

These methods are not intended to be called externally, but may be useful to modify with method modifiers in plugins and subclasses. Their APIs will be kept as stable as possible.

is_pure_perl_comp_path ($path)

Determines whether $path is a pure Perl component - by default, uses pure_perl_extensions.

is_top_level_comp_path ($path)

Determines whether $path is a valid top-level component - by default, uses top_level_extensions.

modify_loaded_class ( $compc )

An opportunity to modify loaded component class $compc (e.g. add additional methods or apply roles) before it is made immutable.

write_object_file ($object_file, $object_contents)

Write compiled component $object_contents to $object_file. This is an opportunity to modify $object_contents before it is written, or $object_file after it is written.

SEE ALSO

Mason

AUTHOR

Jonathan Swartz <swartz@pobox.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Jonathan Swartz.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.