my $p = new HTML::Mason::Parser (...params...);
$@%
), that you intend to use as globals in components. Normally global
variables are forbidden by strict
, but any variable mentioned in this list is granted a reprieve via a ``use
vars'' statement. For example:
allow_globals => [qw($DBH %session)]
In a mod_perl environment, $r
(the request object) is automatically added to this list.
ignore_warnings_expr => 'Global symbol.*requires explicit package'
If undef, all warnings are heeded; if '.', all warnings are ignored.
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.
HTML::Mason::Commands
.
You will have to explicitly import the mc_* commands from HTML::Mason::Commands
into your package. Ideally you will put this in your handler.pl
:
{ package MyPackage; use HTML::Mason::Commands; }
This will bring in all of the mc_ commands. If you want to save some space on imported symbols, specify just the commands you want:
{ package MyPackage; use HTML::Mason::Commands qw(mc_comp mc_file mc_cache); }
This is the ideal place to translate accents into HTML entities. It could also be used to strip out comments that you have in your HTML files that you don't want the end user to see. See preprocess.
use vars qw($r);
to suppress strict warnings about uses of global $r
(the
Apache request object). See postamble.
% my $noun = "World"; Hello, <% $noun %>! How are ya?
Has 51 total bytes, 19 of which are plain text. This allows you to adjust the trade-off between the memory savings of source references and the performance advantage of in-line plain text. For example, to choose only components with at least 50% plain text:
source_refer_predicate => sub { return $_[1] / $_[0] >= 0.5 }
The current default is
sub { return $_[1] >= 500 }
i.e. any component with at least 500 characters. This is an experimental setting and may change.
-T
flag). If true, Mason will pass all component source and filenames through
a dummy regular expression match to untaint them. In the future this option
may become more sophisticated to allow stricter checks. Default is false.
my $parser = new HTML::Mason::Parser; my $strictmode = $parser->use_strict; $parser->use_strict(1);
The only exception is allow_globals, which works a bit differently.
Returns the new Component object on success, or undef if an error occurred. error is an optional scalar reference filled with the error message.
Example of usage:
comp_root and data_dir contain the Mason component root and data directory respectively. These are
required.
paths is a reference to a list of component paths to make recursively. By
default, makes '/' (the entire component tree).
verbose is a flag indicating whether to report components compiled and directories
created. True by default.
predicate is a subroutine that takes one argument, the component source file, and
returns true or false indicating whether or not to try to compile it. By
default predicate ignores all filenames ending with ``~''.
dir_create_mode contains the permissions mode for creating new directories, by default
0775.
update_reload_file is a flag indicating whether to update a reload file in the data directory
as components are recompiled. False by default.
Example of usage:
# Make a component
my $comp = $parser->make_component
(script=>'<%perl>my $name = "World";</%perl>Hello <% $name %>!',
error => \my $error)
or die "error while compiling component: $error";
# Call it from inside another component
$REQ->call($comp);
#!/usr/bin/perl
use HTML::Mason;
use HTML::Mason::ApacheHandler; # load explicitly to bring in special mc_ commands
my $p = new HTML::Mason::Parser;
$p->allow_globals(qw($r)); # allow Apache $r global
$p->make_dirs (comp_root=>'/usr/home/swartz/web/comps',
data_dir=>'/usr/home/swartz/web/mason');