HTML::Mason::Interp - Mason Component Interpreter
| Use Case | Command | Description |
|---|---|---|
| Create interpreter | my $i = HTML::Mason::Interp->new(...) | Initialize Mason with data_dir and comp_root |
| Execute component | $interp->exec($comp, @args) | Run a component (standalone mode) |
| Load component | $interp->load($path) | Get component object by absolute path |
| Make anonymous component | $interp->make_component(comp_source=>'...') | Compile inline Mason source |
| Set global variable | $interp->set_global($varname, $value) | Define global for components |
| Flush code cache | $interp->flush_code_cache() | Clear in-memory component cache |
| Check component exists | $interp->comp_exists($path) | Boolean check for component path |
| Apply escapes | $interp->apply_escapes($text, $flags) | Apply one or more escape flags |
| Set escape flag | $interp->set_escape($name => $sub) | Register custom escape |
| Modify comp_root | $interp->comp_root([...]) | Change component roots (if dynamic_comp_root enabled) |
my $i = HTML::Mason::Interp->new (data_dir=>'/usr/local/mason',
comp_root=>'/usr/local/www/htdocs/',
...other params...);
Interp is the Mason workhorse, executing components and routing their output and errors to all the right places. In a mod_perl environment, Interp objects are handed off immediately to an ApacheHandler object which internally calls the Interp implementation methods. In that case the only user method is the new() constructor.
new() Constructor [ [ foo => '/usr/local/foo' ],
[ bar => '/usr/local/bar' ] ]
This is an array of two-element array references, not a hash. The "keys" for each path must be unique and their "values" must be filesystem paths. These paths will be searched in the provided order whenever a component path is resolved. For example, given the above component roots and a component path of /products/index.html, Mason would search first for /usr/local/foo/products/index.html, then for /usr/local/bar/products/index.html. The keys are used in several ways. They help to distinguish component caches and object files between different component roots, and they appear in the "title()" of a component. When you specify a single path for a component root, this is actually translated into
[ [ MAIN => path ] ]
If you have turned on dynamic_comp_root, you may modify the component root(s) of an interpreter between requests by calling "$interp->comp_root" with a value. However, the path associated with any given key may not change between requests. For example, if the initial component root is
[ [ foo => '/usr/local/foo' ],
[ bar => '/usr/local/bar' ], ]
then it may not be changed to
[ [ foo => '/usr/local/bar' ],
[ bar => '/usr/local/baz' ],
but it may be changed to
[ [ foo => '/usr/local/foo' ],
[ blarg => '/usr/local/blarg' ] ]
In other words, you may add or remove key/path pairs but not modify an already-used key/path pair. The reason for this restriction is that the interpreter maintains a component cache per key that would become invalid if the associated paths were to change.ignore_warnings_expr => 'Global symbol.*requires explicit package' If set to undef, all warnings are heeded. If set to '.', warnings are turned off completely as a specially optimized case. By default, this is set to 'Subroutine .* redefined'. This allows you to declare global subroutines inside <%once> sections and not receive an error when the component is reloaded.preloads => ['/foo/index.html','/bar/*.pl'] Default is the empty list. For maximum performance, this should only be used for components that are frequently viewed and rarely updated. See the preloading components section of the administrator's manual for further details. As mentioned in the developer's manual, a component's "<%once>" section is executed when it is loaded. For preloaded components, this means that this section will be executed before a Mason or Apache request exist, so preloading a component that uses $m or $r in a "<%once>" section will fail.All of the above properties have standard accessor methods of the same name. Only comp_root and ignore_warnings_expr can be modified in an existing interpreter; the rest are read-only.
apply_escapes ($text, $flags, [more flags...]) โ ๐จ This method applies a one or more escapes to a piece of text. The escapes are specified by giving their flag. Each escape is applied to the text in turn, after which the now-modified text is returned.remove_escape ($name) โ โ Given an escape name, this removes that escape from the interpreter's known escapes. If the name is not recognized, it is simply ignored.set_escape ($name => see below) โ โ This method is called to add an escape flag to the list of known escapes for the interpreter. The flag may only consist of the characters matching "\w" and the dash (-). It must start with an alpha character or an underscore (_). The right hand side may be one of several things. It can be a subroutine reference. It can also be a string match "/^\w+$/", in which case it is assumed to be the name of a subroutine in the "HTML::Mason::Escapes" module. Finally, if it is a string that does not match the above regex, then it is assumed to be "eval"able code, which will return a subroutine reference. When setting these with "PerlSetVar" directives in an Apache configuration file, you can set them like this:
PerlSetVar MasonEscapeFlags "h => \&HTML::Mason::Escapes::basic_html_escape"
PerlSetVar MasonEscapeFlags "flag => \&subroutine"
PerlSetVar MasonEscapeFlags "uc => sub { ${$_[0]} = uc ${$_[0]}; }"
PerlAddVar MasonEscapeFlags "thing => other_thing"
comp_exists (path) โ ๐ Given an *absolute* component path, this method returns a boolean value indicating whether or not a component exists for that path.exec (comp, args...) โ โถ๏ธ Creates a new HTML::Mason::Request object for the given *comp* and *args*, and executes it. The return value is the return value of *comp*, if any. This is useful for running Mason outside of a web environment. See "using Mason from a standalone script" in HTML::Mason::Admin for examples. This method isn't generally useful in a mod_perl environment; see subrequests instead.flush_code_cache โ ๐๏ธ Empties the component cache. When using Perl 5.00503 or earlier, you should call this when finished with an interpreter, in order to remove circular references that would prevent the interpreter from being destroyed.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.make_component (comp_source => ... ) / make_component (comp_file => ... ) โ ๐ ๏ธ This method compiles Mason component source code and returns a Component object. The source may be passed in as a string in "comp_source", or as a filename in "comp_file". When using "comp_file", the filename is specified as a path on the file system, not as a path relative to Mason's component root (see $m->fetch_comp for that). If Mason encounters an error during processing, an exception will be thrown. Example of usage:
# Make an anonymous component
my $anon_comp =
eval { $interp->make_component
( comp_source => '<%perl>my $name = "World";</%perl>Hello <% $name %>!' ) };
die $@ if $@;
$m->comp($anon_comp);
make_request (@request_params) โ ๐ This method creates a Mason request object. The arguments to be passed are the same as those for the "HTML::Mason::Request->new" constructor or its relevant subclass. This method will likely only be of interest to those attempting to write new handlers or to subclass "HTML::Mason::Interp". If you want to create a *subrequest*, see subrequests instead.purge_code_cache () โ ๐งน Called during request execution in order to clear out the code cache. Mainly useful to subclasses that may want to take some custom action upon clearing the cache.set_global ($varname, [values...]) โ ๐ This method sets a global to be used in components. "varname" is a variable name, optionally preceded with a prefix ("$", "@", or "%"); if the prefix is omitted then "$" is assumed. "varname" is followed by a value, in the case of a scalar, or by one or more values in the case of a list or hash. For example:
# Set a global variable $dbh containing the database handle
$interp->set_global(dbh => DBI->connect(...));
# Set a global hash %session from a local hash
$interp->set_global('%session', %s);
The global is set in the package that components run in: usually "HTML::Mason::Commands", although this can be overridden via the in_package parameter. The lines above, for example, are equivalent to:
$HTML::Mason::Commands::dbh = DBI->connect(...);
%HTML::Mason::Commands::session = %s;
assuming that in_package has not been changed. Any global that you set should also be registered with the allow_globals parameter; otherwise you'll get warnings from "strict".Generated by phpman v4.10.0-7-g98e9fd5 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-09-17 10:28 @216.73.216.11
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)