Mason::Interp(3pm)

πŸ“¦
version 2.24 Β· 2 versions
  • 2.24 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 2.24 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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 $m is 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 .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. 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.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. 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/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. 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 β€” 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. fr fr πŸ”₯
  • 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. 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 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 πŸ’…

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, 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. (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 $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>


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