1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
<?xml version="1.0" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML::Mason::Interp - Mason Component Interpreter</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:root@localhost" />
</head>
<body style="background-color: white">
<p><A NAME="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><A HREF="#name">NAME</a></li>
<li><A HREF="#synopsis">SYNOPSIS</a></li>
<li><A HREF="#description">DESCRIPTION</a></li>
<li><A HREF="#parameters_to_the_new___construc">PARAMETERS TO THE <code>new()</code> CONSTRUCTOR</a></li>
<li><A HREF="#accessor_methods">ACCESSOR METHODS</a></li>
<li><A HREF="#escape_flag_methods">ESCAPE FLAG METHODS</a></li>
<li><A HREF="#other_methods">OTHER METHODS</a></li>
<li><A HREF="#see_also">SEE ALSO</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><A NAME="name">NAME</a></h1>
<p>HTML::Mason::Interp - Mason Component Interpreter</p>
<p>
</p>
<hr />
<h1><A NAME="synopsis">SYNOPSIS</a></h1>
<pre>
my $i = HTML::Mason::Interp->new (data_dir=>'/usr/local/mason',
comp_root=>'/usr/local/www/htdocs/',
...other params...);</pre>
<p>
</p>
<hr />
<h1><A NAME="description">DESCRIPTION</a></h1>
<p>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 <code>new()</code> constructor.</p>
<p>
</p>
<hr />
<h1><A NAME="parameters_to_the_new___construc">PARAMETERS TO THE <code>new()</code> CONSTRUCTOR</a></h1>
<dl>
<dt><strong><A NAME="item_autohandler_name">autohandler_name</a></strong>
<dd>
<p>File name used for
<A HREF="Devel.html#autohandlers">autohandlers</a>. Default is
``autohandler''. If this is set to an empty string (``'') then
autohandlers are turned off entirely.</p>
</dd>
</li>
<dt><strong><A NAME="item_buffer_preallocate_size">buffer_preallocate_size</a></strong>
<A NAME="item_buffer_preallocate_size"></a><p>Number of bytes to preallocate in the output buffer for each request.
Defaults to 0. Setting this to, say, your maximum page size (or close
to it) can reduce the number of reallocations Perl performs as
components add to the output buffer.</p>
<dt><strong><A NAME="item_code_cache_max_size">code_cache_max_size</a></strong>
<A NAME="item_code_cache_max_size"></a><p>Specifies the maximum number of components that should be held in the
in-memory code cache. The default is 'unlimited', meaning no
components will ever be discarded; Mason can perform certain
optimizations in this mode. Setting this to zero disables the code
cache entirely. See the <A HREF="Admin.html#code_cache">code cache</a>
section of the administrator's manual for further details.</p>
<dt><strong><A NAME="item_comp_root">comp_root</a></strong>
<A NAME="item_comp_root"></a><p>The component root marks the top of your component hierarchy and
defines how component paths are translated into real file paths. For
example, if your component root is <em>/usr/local/httpd/docs</em>, a component
path of <em>/products/index.html</em> translates to the file
<em>/usr/local/httpd/docs/products/index.html</em>.</p>
<p>Under <a HREF="ApacheHandler.html">Apache</a> and
<a HREF="CGIHandler.html">CGI</a>, comp_root defaults to the server's
document root. In standalone mode comp_root defaults to the current
working directory.</p>
<p>This parameter may be either a scalar or an array reference. If it is
a scalar, it should be a filesystem path indicating the component
root. If it is an array reference, it should be of the following form:</p>
<pre>
[ [ foo => '/usr/local/foo' ],
[ bar => '/usr/local/bar' ] ]</pre>
<p>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 <em>/products/index.html</em>, Mason
would search first for <em>/usr/local/foo/products/index.html</em>, then for
<em>/usr/local/bar/products/index.html</em>.</p>
<p>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 <code>title()</code> of a component.</p>
<p>When you specify a single path for a component root, this is actually
translated into</p>
<pre>
[ [ MAIN => path ] ]</pre>
<p>If you have turned on <A HREF="Params.html#dynamic_comp_root">dynamic_comp_root</a>, you may modify the
component <code>root(s)</code> of an interpreter between requests by calling
<A HREF="#item_comp_root"><code>$interp->comp_root</code></a> with a value. However, the path associated
with any given key may not change between requests. For example,
if the initial component root is</p>
<pre>
[ [ foo => '/usr/local/foo' ],
[ bar => '/usr/local/bar' ], ]</pre>
<p>then it may not be changed to</p>
<pre>
[ [ foo => '/usr/local/bar' ],
[ bar => '/usr/local/baz' ],</pre>
<p>but it may be changed to</p>
<pre>
[ [ foo => '/usr/local/foo' ],
[ blarg => '/usr/local/blarg' ] ]</pre>
<p>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.</p>
<dt><strong><A NAME="item_compiler">compiler</a></strong>
<dd>
<p>The Compiler object to associate with this Interpreter. By default a
new object of class <A HREF="Params.html#compiler_class">compiler_class</a> will be created.</p>
</dd>
</li>
<dt><strong><A NAME="item_compiler_class">compiler_class</a></strong>
<dd>
<p>The class to use when creating a compiler. Defaults to
<a HREF="Compiler.html">HTML::Mason::Compiler</a>.</p>
</dd>
</li>
<dt><strong><A NAME="item_data_dir">data_dir</a></strong>
<dd>
<p>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.</p>
</dd>
<dd>
<p>Under <a HREF="ApacheHandler.html">Apache</a>, 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 <em>/usr</em>!</p>
</dd>
<dd>
<p>In non-Apache environments, data_dir has no default. If it is left
unspecified, Mason will not use <A HREF="Admin.html#object_files">object files</a>, and the default
<A HREF="Request.html#item_cache">data cache class</a> will be
<code>MemoryCache</code> instead of <code>FileCache</code>.</p>
</dd>
</li>
<dt><strong><A NAME="item_dynamic_comp_root">dynamic_comp_root</a></strong>
<dd>
<p>True or false, defaults to false. Indicates whether the <A HREF="Params.html#comp_root">comp_root</a>
can be modified on this interpreter between requests. Mason can
perform a few optimizations with a fixed component root, so you
should only set this to true if you actually need it.</p>
</dd>
</li>
<dt><strong><A NAME="item_escape_flags">escape_flags</a></strong>
<dd>
<p>A hash reference of escape flags to set for this object. See the
section on the <A HREF="Interp.html#item_set_escape">set_escape method</a> for more details.</p>
</dd>
</li>
<dt><strong><A NAME="item_ignore_warnings_expr">ignore_warnings_expr</a></strong>
<dd>
<p>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:</p>
</dd>
<dd>
<pre>
ignore_warnings_expr =>
'Global symbol.*requires explicit package'</pre>
</dd>
<dd>
<p>If set to undef, all warnings are heeded. If set to '.', warnings
are turned off completely as a specially optimized case.</p>
</dd>
<dd>
<p>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.</p>
</dd>
</li>
<dt><strong><A NAME="item_object_file_extension">object_file_extension</a></strong>
<dd>
<p>Extension to add to the end of object files. Default is ``.obj''.</p>
</dd>
</li>
<dt><strong><A NAME="item_preloads">preloads</a></strong>
<dd>
<p>A list of component paths, optionally with glob wildcards, to load
when the interpreter initializes. e.g.</p>
</dd>
<dd>
<pre>
preloads => ['/foo/index.html','/bar/*.pl']</pre>
</dd>
<dd>
<p>Default is the empty list. For maximum performance, this should only
be used for components that are frequently viewed and rarely updated.
See the <A HREF="Admin.html#preloading_components">preloading components</a> section of the administrator's manual for further details.</p>
</dd>
<dd>
<p>As mentioned in the developer's manual, a component's <code><%once></code>
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 <code>$m</code> or <code>$r</code> in a
<code><%once></code> section will fail.</p>
</dd>
</li>
<dt><strong><A NAME="item_request_class">request_class</a></strong>
<dd>
<p>The class to use when creating requests. Defaults to
<a HREF="Request.html">HTML::Mason::Request</a>.</p>
</dd>
</li>
<dt><strong><A NAME="item_resolver">resolver</a></strong>
<dd>
<p>The Resolver object to associate with this Compiler. By default a new
object of class <A HREF="Params.html#resolver_class">resolver_class</a> will be created.</p>
</dd>
</li>
<dt><strong><A NAME="item_resolver_class">resolver_class</a></strong>
<dd>
<p>The class to use when creating a resolver. Defaults to
<a HREF="Resolver/File.html">HTML::Mason::Resolver::File</a>.</p>
</dd>
</li>
<dt><strong><A NAME="item_static_source">static_source</a></strong>
<dd>
<p>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.</p>
</dd>
<dd>
<p>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 flush the memory cache and remove object files.
See <A HREF="Params.html#static_source_touch_file">static_source_touch_file</a> for one easy way to arrange this.</p>
</dd>
<dd>
<p>We recommend turning this mode on in your production sites if
possible, if performance is of any concern.</p>
</dd>
</li>
<dt><strong><A NAME="item_static_source_touch_file">static_source_touch_file</a></strong>
<dd>
<p>Specifies a filename that Mason will check once at the beginning of
of every request. When the file timestamp changes, Mason will (1) clear
its in-memory component cache, and (2) remove object files if
they have not already been deleted by another process.</p>
</dd>
<dd>
<p>This provides a convenient way to implement <A HREF="Params.html#static_source">static_source</a> mode.
All you need to do is make sure that a single file gets touched
whenever components change. For Mason's part, checking a single
file at the beginning of a request is much cheaper than checking
every component file when static_source=0.</p>
</dd>
</li>
<dt><strong><A NAME="item_use_object_files">use_object_files</a></strong>
<dd>
<p>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.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><A NAME="accessor_methods">ACCESSOR METHODS</a></h1>
<p>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.</p>
<p>
</p>
<hr />
<h1><A NAME="escape_flag_methods">ESCAPE FLAG METHODS</a></h1>
<dl>
<dt><strong><A NAME="item_apply_escapes">apply_escapes ($text, $flags, [more flags...])</a></strong>
<A NAME="item_apply_escapes"></a><p>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.</p>
<dt><strong><A NAME="item_remove_escape">remove_escape ($name)</a></strong>
<A NAME="item_remove_escape"></a><p>Given an escape name, this removes that escape from the interpreter's
known escapes. If the name is not recognized, it is simply ignored.</p>
<dt><strong><A NAME="item_set_escape">set_escape ($name => see below])</a></strong>
<A NAME="item_set_escape"></a><p>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 <code>\w</code> and the dash (-). It must start with an
alpha character or an underscore (_).</p>
<p>The right hand side may be one of several things. It can be a
subroutine reference. It can also be a string match <code>/^\w+$/</code>, in
which case it is assumed to be the name of a subroutine in the
<code>HTML::Mason::Escapes</code> module. Finally, if it is a string that does
not match the above regex, then it is assumed to be <code>eval</code>able code,
which will return a subroutine reference.</p>
<p>When setting these with <code>PerlSetVar</code> directives in an Apache
configuration file, you can set them like this:</p>
<pre>
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"</pre>
</dl>
<p>
</p>
<hr />
<h1><A NAME="other_methods">OTHER METHODS</a></h1>
<dl>
<dt><strong><A NAME="item_comp_exists">comp_exists (path)</a></strong>
<A NAME="item_comp_exists"></a><p>Given an <em>absolute</em> component path, this method returns a boolean
value indicating whether or not a component exists for that path.</p>
<dt><strong><A NAME="item_exec">exec (comp, args...)</a></strong>
<A NAME="item_exec"></a><p>Creates a new HTML::Mason::Request object for the given <em>comp</em> and
<em>args</em>, and executes it. The return value is the return value of
<em>comp</em>, if any.</p>
<p>This is useful for running Mason outside of a web environment.
See <A HREF="Admin.html#using_mason_from_a_standalone_sc">using Mason from a standalone script in the HTML::Mason::Admin manpage</a>
for examples.</p>
<p>This method isn't generally useful in a mod_perl environment; see
<A HREF="Devel.html#subrequests">subrequests</a> instead.</p>
<dt><strong><A NAME="item_flush_code_cache">flush_code_cache</a></strong>
<A NAME="flush_code_cache"></a><p>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.</p>
<dt><strong><A NAME="item_load">load (path)</a></strong>
<A NAME="item_load"></a><p>Returns the component object corresponding to an absolute component
<code>path</code>, or undef if none exists. Dies with an error if the component
fails to load because of a syntax error.</p>
<dt><strong><A NAME="item_make_component">make_component (comp_source => ... )</a></strong>
<dt><strong>make_component (comp_file => ... )</strong>
<A NAME="item_make_component"></a><p>This method compiles Mason component source code and returns a
Component object. The source may be passed in as a string in <code>comp_source</code>,
or as a filename in <code>comp_file</code>. When using <code>comp_file</code>, the
filename is specified as a path on the file system, not as a path
relative to Mason's component root (see
<A HREF="Request.html#item_fetch_comp">$m->fetch_comp</a> for that).</p>
<p>If Mason encounters an error during processing, an exception will be thrown.</p>
<p>Example of usage:</p>
<pre>
# Make an anonymous component
my $anon_comp =
eval { $interp->make_component
( comp_source => '<%perl>my $name = "World";</%perl>Hello <% $name %>!' ) };
die $@ if $@;</pre>
<pre>
$m->comp($anon_comp);</pre>
<dt><strong><A NAME="item_make_request">make_request (@request_params)</a></strong>
<A NAME="item_make_request"></a><p>This method creates a Mason request object. The arguments to be passed
are the same as those for the <code>HTML::Mason::Request->new</code>
constructor or its relevant subclass. This method will likely only be
of interest to those attempting to write new handlers or to subclass
<code>HTML::Mason::Interp</code>. If you want to create a <em>subrequest</em>, see
<A HREF="Devel.html#subrequests">subrequests</a> instead.</p>
<dt><strong><A NAME="item_purge_code_cache">purge_code_cache ()</a></strong>
<A NAME="purge_code_cache"></a><p>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.</p>
<dt><strong><A NAME="item_set_global">set_global ($varname, [values...])</a></strong>
<A NAME="item_set_global"></a><p>This method sets a global to be used in components. <code>varname</code> is a
variable name, optionally preceded with a prefix (<code>$</code>, <code>@</code>, or
<code>%</code>); if the prefix is omitted then <code>$</code> is assumed. <code>varname</code> 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:</p>
<pre>
# Set a global variable $dbh containing the database handle
$interp->set_global(dbh => DBI->connect(...));</pre>
<pre>
# Set a global hash %session from a local hash
$interp->set_global('%session', %s);</pre>
<p>The global is set in the package that components run in: usually
<code>HTML::Mason::Commands</code>, although this can be overridden via the
<A HREF="Params.html#in_package">in_package</a> parameter.
The lines above, for example, are equivalent to:</p>
<pre>
$HTML::Mason::Commands::dbh = DBI->connect(...);
%HTML::Mason::Commands::session = %s;</pre>
<p>assuming that <A HREF="Params.html#in_package">in_package</a> has not been changed.</p>
<p>Any global that you set should also be registered with the
<A HREF="Params.html#allow_globals">allow_globals</a> parameter; otherwise you'll get warnings from
<code>strict</code>.</p>
</dl>
<p>
</p>
<hr />
<h1><A NAME="see_also">SEE ALSO</a></h1>
<p><a HREF="Mason.html">HTML::Mason</a>,
<a HREF="Admin.html">HTML::Mason::Admin</a>,
<a HREF="ApacheHandler.html">HTML::Mason::ApacheHandler</a>
</p>
</body>
</html>
|