File: Interp.html

package info (click to toggle)
libhtml-mason-perl 1%3A1.26-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,636 kB
  • ctags: 1,260
  • sloc: perl: 13,880; sh: 154; makefile: 47
file content (448 lines) | stat: -rw-r--r-- 16,902 bytes parent folder | download
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
442
443
444
445
446
447
448
<!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>
<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="#memory_leak_warning">MEMORY LEAK WARNING</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-&gt;new (data_dir=&gt;'/usr/local/mason',
                                     comp_root=&gt;'/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><br />
</dt>
<dd>
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.
</dd>
<p></p>
<dt><strong><A NAME="item_code_cache_max_size">code_cache_max_size</a></strong><br />
</dt>
<dd>
Specifies the maximum size, in bytes, of the in-memory code cache
where components are stored. Default is 10 MB. See the <A HREF="Admin.html#code_cache">code cache</a> section of the administrator's manual
for further details.
</dd>
<p></p>
<dt><strong><A NAME="item_compiler">compiler</a></strong><br />
</dt>
<dd>
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.
</dd>
<p></p>
<dt><strong><A NAME="item_compiler_class">compiler_class</a></strong><br />
</dt>
<dd>
The class to use when creating a compiler. Defaults to
<a HREF="Compiler.html">HTML::Mason::Compiler</a>.
</dd>
<p></p>
<dt><strong><A NAME="item_current_time">current_time</a></strong><br />
</dt>
<dd>
Interpreter's notion of the current time (deprecated).
</dd>
<p></p>
<dt><strong><A NAME="item_data_dir">data_dir</a></strong><br />
</dt>
<dd>
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.
</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>
<p></p>
<dt><strong><A NAME="item_escape_flags">escape_flags</a></strong><br />
</dt>
<dd>
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.
</dd>
<p></p>
<dt><strong><A NAME="item_ignore_warnings_expr">ignore_warnings_expr</a></strong><br />
</dt>
<dd>
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:
</dd>
<dd>
<pre>
    ignore_warnings_expr =&gt;
        '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 &lt;%once&gt; sections and not receive
an error when the component is reloaded.</p>
</dd>
<p></p>
<dt><strong><A NAME="item_preloads">preloads</a></strong><br />
</dt>
<dd>
A list of component paths, optionally with glob wildcards, to load
when the interpreter initializes. e.g.
</dd>
<dd>
<pre>
    preloads =&gt; ['/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>&lt;%once&gt;</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>&lt;%once&gt;</code> section will fail.</p>
</dd>
<p></p>
<dt><strong><A NAME="item_request_class">request_class</a></strong><br />
</dt>
<dd>
The class to use when creating requests. Defaults to
<a HREF="Request.html">HTML::Mason::Request</a>.
</dd>
<p></p>
<dt><strong><A NAME="item_resolver">resolver</a></strong><br />
</dt>
<dd>
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.
</dd>
<p></p>
<dt><strong><A NAME="item_resolver_class">resolver_class</a></strong><br />
</dt>
<dd>
The class to use when creating a resolver. Defaults to
<a HREF="Resolver/File.html">HTML::Mason::Resolver::File</a>.
</dd>
<p></p>
<dt><strong><A NAME="item_static_source">static_source</a></strong><br />
</dt>
<dd>
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.
</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 remove object files and restart the server (so
as to clear the memory cache).</p>
</dd>
<dd>
<p>Use this feature for live sites where performance is crucial and
where updates are infrequent and well-controlled.</p>
</dd>
<p></p>
<dt><strong><A NAME="item_use_object_files">use_object_files</a></strong><br />
</dt>
<dd>
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.
</dd>
<p></p></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. In general, no arguments retrieves the value, and one argument
sets and returns the value.  For example:</p>
<pre>
    my $interp = HTML::Mason::Interp-&gt;new (...);
    my $c = $interp-&gt;compiler;
    $interp-&gt;code_cache_max_size(20 * 1024 * 1024);</pre>
<p>The following properties can be queried but not modified: data_dir,
preloads.</p>
<p>
</p>
<hr />
<h1><A NAME="escape_flag_methods">ESCAPE FLAG METHODS</a></h1>
<A NAME="item_apply_escapes"></a><dl>
<dt><strong><A NAME="item_apply_escapes">apply_escapes ($text, $flags, [more flags...])</a></strong><br />
</dt>
<dd>
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.
</dd>
<A NAME="item_remove_escape"></a><p></p>
<dt><strong><A NAME="item_remove_escape">remove_escape ($name)</a></strong><br />
</dt>
<dd>
Given an escape name, this removes that escape from the interpreter's
known escapes.  If the name is not recognized, it is simply ignored.
</dd>
<A NAME="item_set_escape"></a><p></p>
<dt><strong><A NAME="item_set_escape">set_escape ($name =&gt; see below])</a></strong><br />
</dt>
<dd>
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 (_).
</dd>
<dd>
<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>
</dd>
<dd>
<p>When setting these with <code>PerlSetVar</code> directives in an Apache
configuration file, you can set them like this:</p>
</dd>
<dd>
<pre>
  PerlSetVar  MasonEscapeFlags  &quot;h =&gt; \&amp;HTML::Mason::Escapes::basic_html_escape&quot;
  PerlSetVar  MasonEscapeFlags  &quot;flag  =&gt; \&amp;subroutine&quot;
  PerlSetVar  MasonEscapeFlags  &quot;uc    =&gt; sub { ${$_[0]} = uc ${$_[0]}; }&quot;
  PerlAddVar  MasonEscapeFlags  &quot;thing =&gt; other_thing&quot;</pre>
</dd>
<p></p></dl>
<p>
</p>
<hr />
<h1><A NAME="other_methods">OTHER METHODS</a></h1>
<A NAME="item_comp_exists"></a><dl>
<dt><strong><A NAME="item_comp_exists">comp_exists (path)</a></strong><br />
</dt>
<dd>
Given an <em>absolute</em> component path, this method returns a boolean
value indicating whether or not a component exists for that path.
</dd>
<A NAME="item_comp_root"></a><p></p>
<dt><strong><A NAME="item_comp_root">comp_root (comp_root)</a></strong><br />
</dt>
<dd>
This is a convenience method which simply calls the <A HREF="#item_comp_root"><code>comp_root</code></a>
method in the resolver object, which by default is in the
HTML::Mason::Resolver::File class.
</dd>
<dd>
<p>Obviously, if you are using a custom resolver class which does not
have a <A HREF="#item_comp_root"><code>comp_root</code></a> method, then this convenience method will not
work.</p>
</dd>
<A NAME="item_exec"></a><p></p>
<dt><strong><A NAME="item_exec">exec (comp, args...)</a></strong><br />
</dt>
<dd>
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.
</dd>
<dd>
<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>
</dd>
<dd>
<p>This method isn't generally useful in a mod_perl environment; see
<A HREF="Devel.html#subrequests">subrequests</a> instead.</p>
</dd>
<A NAME="flush_code_cache"></a><p></p>
<dt><strong><A NAME="item_flush_code_cache">flush_code_cache</a></strong><br />
</dt>
<dd>
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.
</dd>
<A NAME="item_load"></a><p></p>
<dt><strong><A NAME="item_load">load (path)</a></strong><br />
</dt>
<dd>
Returns the component object corresponding to an absolute component
<code>path</code>, or undef if none exists.
</dd>
<A NAME="item_make_component"></a><p></p>
<dt><strong><A NAME="item_make_component">make_component (comp_source =&gt; ... )</a></strong><br />
</dt>
<dt><strong>make_component (comp_file =&gt; ... )</strong><br />
</dt>
<dd>
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-&gt;fetch_comp</a> for that).
</dd>
<dd>
<p>If Mason encounters an error during processing, an exception will be thrown.</p>
</dd>
<dd>
<p>Example of usage:</p>
</dd>
<dd>
<pre>
    # Make an anonymous component
    my $anon_comp =
      eval { $interp-&gt;make_component
               ( comp_source =&gt; '&lt;%perl&gt;my $name = &quot;World&quot;;&lt;/%perl&gt;Hello &lt;% $name %&gt;!' ) };
    die $@ if $@;</pre>
</dd>
<dd>
<pre>
    $m-&gt;comp($anon_comp);</pre>
</dd>
<A NAME="item_make_request"></a><p></p>
<dt><strong><A NAME="item_make_request">make_request (@request_params)</a></strong><br />
</dt>
<dd>
This method creates a Mason request object. The arguments to be passed
are the same as those for the <code>HTML::Mason::Request-&gt;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.
</dd>
<A NAME="purge_code_cache"></a><p></p>
<dt><strong><A NAME="item_purge_code_cache">purge_code_cache ()</a></strong><br />
</dt>
<dd>
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.
</dd>
<A NAME="item_set_global"></a><p></p>
<dt><strong><A NAME="item_set_global">set_global ($varname, [values...])</a></strong><br />
</dt>
<dd>
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:
</dd>
<dd>
<pre>
    # Set a global variable $dbh containing the database handle
    $interp-&gt;set_global(dbh =&gt; DBI-&gt;connect(...));</pre>
</dd>
<dd>
<pre>
    # Set a global hash %session from a local hash
    $interp-&gt;set_global('%session', %s);</pre>
</dd>
<dd>
<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>
</dd>
<dd>
<pre>
    $HTML::Mason::Commands::dbh = DBI-&gt;connect(...);
    %HTML::Mason::Commands::session = %s;</pre>
</dd>
<dd>
<p>assuming that <A HREF="Params.html#in_package">in_package</a> has not been changed.</p>
</dd>
<dd>
<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>
</dd>
<p></p></dl>
<p>
</p>
<hr />
<h1><A NAME="memory_leak_warning">MEMORY LEAK WARNING</a></h1>
<p>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
<A HREF="Interp.html#flush_code_cache">flush_code_cache</a>.  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.</p>
<p>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.</p>
<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>