my $i = new HTML::Mason::Interp (data_dir=>'/usr/local/mason', comp_root=>'/usr/local/www/htdocs/', ...other params...);
new()
constructor.
If you want to call components outside of mod_perl (e.g. from CGI or a stand-alone Perl script), see the STANDALONE MODE section below.
This parameter may be augmented in the future with more sophisticated caching and cache expiration options.
time()
value (seconds since the epoch).
With no current_time parameter (the default), mc_time and mc_date report the true time.
my $fh = new IO::File ">mason.out"; ... out_method => sub { $fh->print($_[0]) }
By default, out_method prints to standard output. (In a mod_perl environment this is automatically redirected to the HTTP client.)
my $interp = new HTML::Mason::Interp (...); my $p = $interp->parser; my $comproot = $interp->comp_root; $interp->out_method(\$buf);
The following properties can be queried but should not be modified:
o comp_root, data_dir: these are too integral to the interpreter to reliably change on-the-fly. Better to create multiple interpreters.
o preloads: changing this is ineffective since preloading happens when the interpreter is created
o system_log_file: changing this is ineffective since a persistent log file handle is opened when the interpreter is created
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 Parser parameter in_package. 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 Parser parameter allow_globals; otherwise you'll get warnings from
strict
.
When using Mason outside of mod_perl, just create a Parser and Interp
object; you do not need the ApacheHandler object. Once you've created an
interpreter, the main thing you'll want to do with it is call a component
and do something with the output. To call a component, use Interp's
exec()
method:
$interp->exec(<comp> [,<..list of component params..>]);
where comp is a component path or component object.
Component parameters are given as a series of name/value pairs, just as they are with mc_comp. exec returns the return value of the component. Component output is sent to standard output by default, but you can change this by specifying out_method.
Here is a skeleton script that calls a component and places the output in a file:
my $outbuf; my $parser = new HTML::Mason::Parser; my $interp = new HTML::Mason::Interp (parser=>$parser, comp_root=>'<component root>', data_dir=>'<data directory>', out_method=>\$outbuf); my $retval = $interp->exec('<component path>',<args>...); open(F,"mason.out"); print F $outbuf; close(F); print "return value of component was: $retval\n";