Mason::Interp(3pm)
π Mason::Interp β the Mason Interpreter π
okay bestie, welcome to Mason::Interp, which is a page from the User Contributed Perl Documentation section (3pm, so like, module-land). Sit down, hydrate, and let's get into it β¨
πΈ NAME πΈ
Mason::Interp β the Mason Interpreter. that's it, that's the name, short and iconic π
β¨ SYNOPSIS β¨
my $interp = Mason->new(
comp_root => '/path/to/comps',
data_dir => '/path/to/data',
...
);
my $output = $interp->run( '/request/path', foo => 5 )->output();
like. that's the whole vibe. make an interp, run a path, grab the output. slay π«Ά
π¦ DESCRIPTION π¦
Interp is the central Mason object, and you get it back from Mason->new. It is literally the main character here β it's responsible for creating new requests, compiling components, and maintaining the cache of loaded components. no cap, everything flows through it π₯
π PARAMETERS TO THE new() CONSTRUCTOR π
okay so this is the big one. these are all the things you can hand to Mason->new. every single one. we're not skipping any, bestie, we're doing the full set π
allow_globals(varnames) β a list of one or more global variable names that will be available in all components, the same way$mis by default.
allow_globals => [qw($dbh)]
and listen, 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.mpand.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. fancy π§
comp_rootβ Required, so like, do not forget it. 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.mctranslates 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. very @INC-coded of it π
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/barwould get a classname likeMC0::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. heads up though β 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β defaultout_methodpassed 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. fr fr π₯
static_source_touch_fileβ specifies a filename that Mason will check once at the beginning of every request when instatic_sourcemode. 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->runor 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. it's giving choose your own adventure π¦
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
and here's the full roster of swappable base classes, no skips:
base_code_cache_classβ specify alternate toMason::CodeCachebase_compilation_classβ specify alternate toMason::Compilationbase_component_classβ specify alternate toMason::Componentbase_component_moose_classβ specify alternate toMason::Component::Moosebase_component_class_meta_classβ specify alternate toMason::Component::ClassMetabase_component_import_classβ specify alternate toMason::Component::Importbase_request_classβ specify alternate toMason::Requestbase_result_classβ specify alternate toMason::Result
π PUBLIC METHODS π
the ones you're actually allowed to call, bestie. go off:
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. bye bye π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, orundefif 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 newMason::Requestobject for the given path and args, and executes it. returns aMason::Resultobject, 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 theallow_globalslist.
$interp->set_global('$scalar', 5);
$interp->set_global('$scalar2', $some_object);
see also set_global. (yes, it really says that. iconic circular energy π)
π§ 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. lowkey the plugin-author zone πΈ
is_pure_perl_comp_path ($path)β determines whether$pathis a pure Perl component β by default, usespure_perl_extensions.is_top_level_comp_path ($path)β determines whether$pathis a valid top-level component β by default, usestop_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_contentsto$object_file. this is an opportunity to modify$object_contentsbefore it is written, or$object_fileafter 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.
and that's the whole page, bestie π«Ά this was perl v5.34.0, dated 2022-05-27, and it's the Mason::Interp(3pm) page from User Contributed Perl Documentation. periodt. go compile something gorgeous β¨π
π
π¦ where this page comes from
- debian bookworm: libmason-perl 2.24-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmason-perl 2.24-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmason-perl 2.24-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmason-perl 2.24-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmason-perl 2.24-2 Β· 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