Help-Site Computer Manuals
  Algorithms & Data Structures   Programming Languages   Revision Control
  Cameras   Computers   Displays   Keyboards & Mice   Motherboards   Networking   Printers & Scanners   Storage
  Windows   Linux & Unix   Mac

Mason Component Interpreter

HTML::Mason::Interp - Mason Component Interpreter


HTML::Mason::Interp - Mason Component Interpreter


    my $i = HTML::Mason::Interp->new (data_dir=>'/usr/local/mason',


                                     ...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.


File name used for autohandlers. Default is ``autohandler''. If this is set to an empty string (``'') then autohandlers are turned off entirely.

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 Compiler object to associate with this Interpreter. By default a new object of class compiler_class will be created.

The class to use when creating a compiler. Defaults to HTML::Mason::Compiler.

Interpreter's notion of the current time (deprecated).

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.

A hash reference of escape flags to set for this object. See the section on the set_escape method for more details.

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:

    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.

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.

The class to use when creating requests. Defaults to HTML::Mason::Request.

The Resolver object to associate with this Compiler. By default a new object of class resolver_class will be created.

The class to use when creating a resolver. Defaults to HTML::Mason::Resolver::File.

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.


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 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:

  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.

comp_root (comp_root)
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.

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 the HTML::Mason::Admin manpage for examples.

This method isn't generally useful in a mod_perl environment; see subrequests instead.

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.

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 $@;


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.


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.


HTML::Mason, HTML::Mason::Admin, HTML::Mason::ApacheHandler