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.
Specifies the maximum size, in bytes, of the in-memory code cache
where components are stored. Default is 10 MB. See the code cache section of the administrator's manual
for further details.
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, and set its
permissions according to the web server User/Group.
Under Apache, data_dir defaults to a
directory called ``mason'' under the Apache server root. You will
need to change this on certain systems that assign a high-level
server root such as /usr!
In non-Apache environments, data_dir has no default. If it is left
unspecified, Mason will not use object files, and the default
data cache class will be
MemoryCache instead of FileCache.
Regular expression indicating which warnings to ignore when loading
components. Any warning that is not ignored will prevent the
component from being loaded and executed. For example:
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.
A list of component paths, optionally with glob wildcards, to load
when the interpreter initializes. e.g.
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.
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 remove object files and restart the server (so
as to clear the memory cache).
Use this feature for live sites where performance is crucial and
where updates are infrequent and well-controlled.
True or false, default is true. Specifies whether Mason creates
object files to save the results of component parsing. You may want to
turn off object files for disk space reasons, but otherwise this
should be left alone.
All of the above properties have standard accessor methods of the same
name. In general, no arguments retrieves the value, and one argument
sets and returns the value. For example:
my $interp = HTML::Mason::Interp->new (...);
my $c = $interp->compiler;
$interp->code_cache_max_size(20 * 1024 * 1024);
The following properties can be queried but not modified: data_dir,
preloads.
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.
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 evalable code,
which will return a subroutine reference.
When setting these with PerlSetVar directives in an Apache
configuration file, you can set them like this:
This is a convenience method which simply calls the comp_root
method in the resolver object, which by default is in the
HTML::Mason::Resolver::File class.
Obviously, if you are using a custom resolver class which does not
have a comp_root method, then this convenience method will not
work.
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.
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 $@;
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.
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.
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:
When using Perl 5.00503 or earlier, using the code cache creates a
circular reference between Interp and component objects. This means
that Interp objects will not be destroyed unless you call
flush_code_cache. If you are
using Perl 5.6.0 or greater, and you have the XS version of
Scalar::Util installed, Mason uses weak references to prevent this
problem.
Win32 users should note that as of this writing, ActiveState's PPD for
Scalar-List-Utils only includes the pure Perl version of these
modules, which don't include the weak references functionality.