$m->abort (...) $m->comp (...) etc.
$m
.
Interp::exec
; in a web environment, this ultimately becomes the HTTP status code.
abort()
is implemented via die()
and can thus be
caught by eval().
Under the current implementation, any pending <%filter>
sections will not be applied to the output after an abort. This is a known
bug but there is no easy workaround.
The methods aborted
and aborted_value
return a boolean indicating whether the current request was aborted and the
argument with which it was aborted, respectively. These would be used, for
example, after an eval()
returned with a non-empty $@
.
The argument to action is one of:
o retrieve: returns the cache value if successful, or
o store: stores a new cache value under the given key. Default key is 'main'.
Returns the value being stored if successful.
o expire: expires a given cache value or values. key may be a single key or a list
reference. Default key is 'main'.
o keys: returns a list of all the keys in the cache.
value defines what to store. It can be a scalar or a reference to an arbitrary
data structure. The allowable size depends on your DBM implementation.
keep_in_memory indicates whether to save the value in memory once it is retrieved. Default
is 0, meaning that the value will be retrieved from the cache file each
time. If 1, each child server that retrieves this value will save its own
copy, which can result in substantial memory usage for larger values. Use
sparingly.
The various expiration options are:
o expire_at: takes an absolute expiration time, in Perl
o expire_in: takes a relative expiration time of the form ``<num><unit>'',
where <num> is a positive number and <unit> is one of seconds,
minutes, hours, days, or weeks, or any abbreviation thereof. E.g.
``10min'', ``30m'', ``1hour''.
o expire_next: takes a string, either 'hour' or 'day'. It indicates an expiration time
at the top of the next hour or day.
o expire_if: calls a given anonymous subroutine and expires if the subroutine returns
a non-zero value. The subroutine is called with one parameter, the time
when the cache value was last written.
To cache the component's output:
To cache the component's return value:
This only works with scalar and reference return values.
o A component foo calls
o
o foo again calls
o foo goes about its business and generates content into the
o When control is returned to
When called in scalar context, a hash reference is returned. When called in
list context, a list of arguments (which may be assigned to a hash) is
returned.
clear_buffer only works in batch output mode, and is thwarted by
Components work exactly like Perl subroutines in terms of return values and
context. A component can return any type of value, which is then returned
from the
The <& &> tag provides a convenient shortcut for
By using
abort
.
abort
when the request was aborted. Returns undef if the request was not aborted
or was aborted without an argument.
base_comp
is dynamically set to that component until call_method exits. See Object-Oriented Techniques for examples of usage.
$m->cache
lets you store and retrieve the results of computation for improved
performance. Each component has its own data cache for storing one or more
key/value pairs. The cache is implemented as a DBM database. See the data caching
section of the Component Developer's Guide for examples and caching strategies.
undef
if there was no value or if it has expired.
time()
format
(number of seconds since the epoch)
$m->cache
to cache the entire output and/or the return value of the current
component. It is typically used right at the top of an
<%init%>
section.
<%init>
return if $m->cache_self(expire_in=>'3 hours'[, key=>'fookey']);
... <rest of init> ...
</%init>
<%init>
my ($retval,$cached) = $m->cache_self
(expire_in=>'3 hours'[, key=>'fookey']);
return $retval if $cached;
... <rest of init> ...
</%init>
$m->cache_self
handles both the retrieve and store, so you can pass both kinds of options
to it. See $m->cache
for an explanation of options.
$m->cache_self
uses a bit of magic to accomplish everything in one line. You can use it
without understanding it, but if you're curious, here's how it works:
$m->cache_self
for the first time.
$m->cache_self
sees that the cache is empty and calls foo again recursively, with a STORE
option to capture its content into a buffer.
$m->cache_self
which immediately returns 0 this time.
$m->cache_self
buffer.
$m->cache_self
, it stores the content and return value in the cache and also outputs the
content normally. Finally $m->cache_self
returns the list (retval,1) which in turn causes foo to return immediately.
$m->caller_args(0) # arguments passed to current component
$m->caller_args(1) # arguments passed to component that called us
$m->caller_args(-1) # arguments passed to first component executed
my @comps = $m->callers # all components
$m->callers(0) # current component
$m->callers(1) # component that called us
$m->callers(-1) # first component executed
$m->comp
in terms of return value and scalar/list context. See the autohandlers section of the
Component Developer's Guide for examples.
flush_buffer
.
$m->comp
call.
$m->comp
.
Interp::exec
path when the dhandler directory is removed. Otherwise returns undef.
dhandler_arg
may be called from any component in the request, not just the dhandler.
$m->file
to resolve relative filenames.
$m->out
is useful if you need to output something in the middle of a Perl block.
$m->out
should be used instead of print
or $r->print
, since $m->out
may be redirected or buffered depending on the current state of the
interpreter.
$m->comp
, but returns the component output as a string instead of printing it.
(Think sprintf versus printf.) The component's return value is discarded.
time()
format (number of seconds since the epoch).
$m->time
rather than calling time()
directly, you enable the option of
previewer or port-based time/date simulations. e.g. a port that looks one
day into the future.
$m->call_next
chain.
AUTHOR
Jonathan Swartz, swartz@pobox.com