.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. \*(C+ will
.\" give a nicer C++. Capital omega is used to do unbreakable dashes and
.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{
. if \nF \{
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff
.if n \{\
. ds #H 0
. ds #V .8m
. ds #F .3m
. ds #[ \f1
. ds #] \fP
.\}
.if t \{\
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
. ds #V .6m
. ds #F 0
. ds #[ \&
. ds #] \&
.\}
. \" simple accents for nroff and troff
.if n \{\
. ds ' \&
. ds ` \&
. ds ^ \&
. ds , \&
. ds ~ ~
. ds /
.\}
.if t \{\
. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
. \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
. \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
. \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
. ds : e
. ds 8 ss
. ds o a
. ds d- d\h'-1'\(ga
. ds D- D\h'-1'\(hy
. ds th \o'bp'
. ds Th \o'LP'
. ds ae ae
. ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "Params::CallbackRequest 3pm"
.TH Params::CallbackRequest 3pm "2015-06-04" "perl v5.20.2" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
Params::CallbackRequest \- Functional and object\-oriented callback architecture
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
Functional parameter-triggered callbacks:
.PP
.Vb 2
\& use strict;
\& use Params::CallbackRequest;
\&
\& # Create a callback function.
\& sub calc_time {
\& my $cb = shift;
\& my $params = $cb\->params;
\& my $val = $cb\->value;
\& $params\->{my_time} = localtime($val || time);
\& }
\&
\& # Set up a callback request object.
\& my $cb_request = Params::CallbackRequest\->new(
\& callbacks => [ { cb_key => \*(Aqcalc_time\*(Aq,
\& pkg_key => \*(AqmyCallbacker\*(Aq,
\& cb => \e&calc_time } ]
\& );
\&
\& # Request callback execution.
\& my %params = (\*(AqmyCallbacker|calc_time_cb\*(Aq => 1);
\& $cb_request\->request(\e%params);
\&
\& # Demonstrate the result.
\& print "The time is $params{my_time}\en";
.Ve
.PP
Or, in a subclass of Params::Callback:
.PP
.Vb 3
\& package MyApp::Callback;
\& use base qw(Params::Callback);
\& _\|_PACKAGE_\|_\->register_subclass( class_key => \*(AqmyCallbacker\*(Aq );
\&
\& # Set up a callback method.
\& sub calc_time : Callback {
\& my $self = shift;
\& my $params = $self\->request_params;
\& my $val = $cb\->value;
\& $params\->{my_time} = localtime($val || time);
\& }
.Ve
.PP
And then, in your application:
.PP
.Vb 3
\& # Load order is important here!
\& use MyApp::Callback;
\& use Params::CallbackRequest;
\&
\& my $cb_request = Params::Callback\->new( cb_classes => [qw(myCallbacker)] );
\& my %params = (\*(AqmyCallbacker|calc_time_cb\*(Aq => 1);
\& $cb_request\->request(\e%params);
\& print "The time is $params{my_time}\en";
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
Params::CallbackRequest provides functional and object-oriented callbacks to
method and function parameters. Callbacks may be either code references
provided to the \f(CW\*(C`new()\*(C'\fR constructor, or methods defined in subclasses of
Params::Callback. Callbacks are triggered either for every call to the
Params::CallbackRequest \f(CW\*(C`request()\*(C'\fR method, or by specially named keys in the
parameters to \f(CW\*(C`request()\*(C'\fR.
.PP
The idea behind this module is to provide a sort of plugin architecture for
Perl templating systems. Callbacks are triggered by the contents of a request
to the Perl templating server, before the templating system itself executes.
This approach allows you to carry out logical processing of data submitted
from a form, to affect the contents of the request parameters before they're
passed to the templating system for processing, and even to redirect or abort
the request before the templating system handles it.
.SH "JUSTIFICATION"
.IX Header "JUSTIFICATION"
Why would you want to do this? Well, there are a number of reasons. Some I can
think of offhand include:
.IP "Stricter separation of logic from presentation" 4
.IX Item "Stricter separation of logic from presentation"
While some Perl templating systems enforce separation of application logic
from presentation (e.g., \s-1TT,\s0 HTML::Template), others do not (e.g.,
HTML::Mason, Apache::ASP). Even in the former case, application logic is often
put into scripts that are executed alongside the presentation templates, and
loaded on-demand under mod_perl. By moving the application logic into Perl
modules and then directing the templating system to execute that code as
callbacks, you obviously benefit from a cleaner separation of application
logic and presentation.
.IP "Widgitization" 4
.IX Item "Widgitization"
Thanks to their ability to preprocess parameters, callbacks enable developers
to develop easier-to-use, more dynamic widgets that can then be used in any
and all templating systems. For example, a widget that puts many related
fields into a form (such as a date selection widget) can have its fields
preprocessed by a callback (for example, to properly combine the fields into a
unified date parameter) before the template that responds to the form
submission gets the data. See Params::Callback for an example solution for this very problem.
.IP "Shared Memory" 4
.IX Item "Shared Memory"
If you run your templating system under mod_perl, callbacks are just Perl
subroutines in modules loaded at server startup time. Thus the memory they
consume is all in the Apache parent process, and shared by the child
processes. For code that executes frequently, this can be much less
resource-intensive than code in templates, since templates are loaded
separately in each Apache child process on demand.
.IP "Performance" 4
.IX Item "Performance"
Since they're executed before the templating architecture does much
processing, callbacks have the opportunity to short-circuit the template
processing by doing something else. A good example is redirection. Often the
application logic in callbacks does its thing and then redirects the user to a
different page. Executing the redirection in a callback eliminates a lot of
extraneous processing that would otherwise be executed before the redirection,
creating a snappier response for the user.
.IP "Testing" 4
.IX Item "Testing"
Templating system templates are not easy to test via a testing framework such
as Test::Harness. Subroutines in modules, on the other hand, are fully
testable. This means that you can write tests in your application test suite
to test your callback subroutines.
.PP
And if those aren't enough reasons, then just consider this: Callbacks are
just \fIway cool.\fR
.SH "USAGE"
.IX Header "USAGE"
Params::CallbackRequest supports two different types of callbacks: those
triggered by a specially named parameter keys, and those executed for every
request.
.SS "Parameter-Triggered Callbacks"
.IX Subsection "Parameter-Triggered Callbacks"
Parameter-triggered callbacks are triggered by specially named parameter
keys. These keys are constructed as follows: The package name followed by a
pipe character (\*(L"|\*(R"), the callback key with the string \*(L"_cb\*(R" appended to it,
and finally an optional priority number at the end. For example, if you
specified a callback with the callback key \*(L"save\*(R" and the package key \*(L"world\*(R",
a callback field might be specified like this:
.PP
.Vb 1
\& my $params = { "world|save_cb" => \*(AqSave World\*(Aq };
.Ve
.PP
When the parameters hash \f(CW$params\fR is passed to Params::CallbackRequest's
\&\f(CW\*(C`request()\*(C'\fR method, the \f(CW\*(C`world|save_cb\*(C'\fR parameter would trigger the callback
associated with the \*(L"save\*(R" callback key in the \*(L"world\*(R" package. If such a
callback hasn't been configured, then Params::CallbackRequest will throw a
Params::CallbackRequest::Exceptions::InvalidKey exception. Here's how to configure a
functional callback when constructing your Params::CallbackRequest object so
that that doesn't happen:
.PP
.Vb 4
\& my $cb_request = Params::CallbackRequest\->new
\& ( callbacks => [ { pkg_key => \*(Aqworld\*(Aq,
\& cb_key => \*(Aqsave\*(Aq,
\& cb => \e&My::World::save } ] );
.Ve
.PP
With this configuration, the \f(CW\*(C`world|save_cb\*(C'\fR parameter key will trigger the
execution of the \f(CW\*(C`My::World::save()\*(C'\fR subroutine during a callback request:
.PP
.Vb 2
\& # Execute parameter\-triggered callback.
\& $cb_request\->request($params);
.Ve
.PP
\fIFunctional Callback Subroutines\fR
.IX Subsection "Functional Callback Subroutines"
.PP
Functional callbacks use a code reference for parameter-triggered callbacks,
and Params::CallbackRequest executes them with a single argument, a
Params::Callback object. Thus, a callback subroutine will generally look
something like this:
.PP
.Vb 4
\& sub foo {
\& my $cb = shift;
\& # Do stuff.
\& }
.Ve
.PP
The Params::Callback object provides accessors to data relevant to the
callback, including the callback key, the package key, and the parameter
hash. It also includes an \f(CW\*(C`abort()\*(C'\fR method. See the
Params::Callback documentation for all the goodies.
.PP
Note that Params::CallbackRequest installs an exception handler during the
execution of callbacks, so if any of your callback subroutines \f(CW\*(C`die\*(C'\fR,
Params::CallbackRequest will throw an Params::Callback::Exception::Execution
exception. If your callback subroutines throw their own exception objects,
Params::CallbackRequest will simply rethrow them. If you don't like this
configuration, use the \f(CW\*(C`exception_handler\*(C'\fR parameter to \f(CW\*(C`new()\*(C'\fR to install
your own exception handler.
.PP
\fIObject-Oriented Callback Methods\fR
.IX Subsection "Object-Oriented Callback Methods"
.PP
Object-oriented callback methods, which are supported under Perl 5.6 or later,
are defined in subclasses of Params::Callback, and identified by attributes in
their declarations. Unlike functional callbacks, callback methods are not
called with a Params::Callback object, but with an instance of the callback
subclass. These classes inherit all the goodies provided by Params::Callback,
so you can essentially use their instances exactly as you would use the
Params::Callback object in functional callback subroutines. But because
they're subclasses, you can add your own methods and attributes. See
Params::Callback for all the gory details on
subclassing, along with a few examples. Generally, callback methods will look
like this:
.PP
.Vb 4
\& sub foo : Callback {
\& my $self = shift;
\& # Do stuff.
\& }
.Ve
.PP
As with functional callback subroutines, method callbacks are executed with a
custom exception handler. Again, see the \f(CW\*(C`exception_handler\*(C'\fR parameter to
install your own exception handler.
.PP
\&\fBNote:\fR Under mod_perl, it's important that you \f(CW\*(C`use\*(C'\fR any and all
Params::Callback subclasses \fIbefore\fR you \f(CW\*(C`use Params::CallbackRequest\*(C'\fR. This
is to get around an issue with identifying the names of the callback methods
in mod_perl. Read the comments in the Params::Callback source code if you're
interested in learning more.
.PP
\fIThe Package Key\fR
.IX Subsection "The Package Key"
.PP
The use of the package key is a convenience so that a system with many
functional callbacks can use callbacks with the same keys but in different
packages. The idea is that the package key will uniquely identify the module
in which each callback subroutine is found, but it doesn't necessarily have to
be so. Use the package key any way you wish, or not at all:
.PP
.Vb 3
\& my $cb_request = Params::CallbackRequest\->new
\& ( callbacks => [ { cb_key => \*(Aqsave\*(Aq,
\& cb => \e&My::World::save } ] );
.Ve
.PP
But note that if you don't specify the package key, you'll still need to
provide one in the parameter hash passed to \f(CW\*(C`request()\*(C'\fR. By default, that key
is \*(L"\s-1DEFAULT\*(R".\s0 Such a callback parameter would then look like this:
.PP
.Vb 1
\& my $params = { "DEFAULT|save_cb" => \*(AqSave World\*(Aq };
.Ve
.PP
If you don't like the \*(L"\s-1DEFAULT\*(R"\s0 package name, you can set an alternative
default using the \f(CW\*(C`default_pkg_name\*(C'\fR parameter to \f(CW\*(C`new()\*(C'\fR:
.PP
.Vb 4
\& my $cb_request = Params::CallbackRequest\->new
\& ( callbacks => [ { cb_key => \*(Aqsave\*(Aq,
\& cb => \e&My::World::save } ],
\& default_pkg_name => \*(AqMyPkg\*(Aq );
.Ve
.PP
Then, of course, any callbacks without a specified package key of their own
must then use the custom default:
.PP
.Vb 2
\& my $params = { "MyPkg|save_cb" => \*(AqSave World\*(Aq };
\& $cb_request\->request($params);
.Ve
.PP
\fIThe Class Key\fR
.IX Subsection "The Class Key"
.PP
The class key is essentially a synonym for the package key, but applies more
directly to object-oriented callbacks. The difference is mainly that it
corresponds to an actual class, and that all Params::Callback subclasses are
\&\fIrequired\fR to have a class key; it's not optional as it is with functional
callbacks. The class key may be declared in your Params::Callback subclass
like so:
.PP
.Vb 3
\& package MyApp::CallbackHandler;
\& use base qw(Params::Callback);
\& _\|_PACKAGE_\|_\->register_subclass( class_key => \*(AqMyCBHandler\*(Aq );
.Ve
.PP
The class key can also be declared by implementing a \f(CW\*(C`CLASS_KEY\*(C'\fR subroutine,
like so:
.PP
.Vb 4
\& package MyApp::CallbackHandler;
\& use base qw(Params::Callback);
\& _\|_PACKAGE_\|_\->register_subclass;
\& use constant CLASS_KEY => \*(AqMyCBHandler\*(Aq;
.Ve
.PP
If no class key is explicitly defined, Params::Callback will use the subclass
name, instead. In any event, the \f(CW\*(C`register_callback()\*(C'\fR method \fBmust\fR be
called to register the subclass with Params::Callback. See the
Params::Callback
documentation for complete details.
.PP
\fIPriority\fR
.IX Subsection "Priority"
.PP
Sometimes one callback is more important than another. For example, you might
rely on the execution of one callback to set up variables needed by another.
Since you can't rely on the order in which callbacks are executed (the
parameters are passed via a hash, and the processing of a hash is, of course,
unordered), you need a method of ensuring that the setup callback executes
first.
.PP
In such a case, you can set a higher priority level for the setup callback
than for callbacks that depend on it. For functional callbacks, you can do it
like this:
.PP
.Vb 7
\& my $cb_request = Params::CallbackRequest\->new
\& ( callbacks => [ { cb_key => \*(Aqsetup\*(Aq,
\& priority => 3,
\& cb => \e&setup },
\& { cb_key => \*(Aqsave\*(Aq,
\& cb => \e&save }
\& ] );
.Ve
.PP
For object-oriented callbacks, you can define the priority right in the
callback method declaration:
.PP
.Vb 4
\& sub setup : Callback( priority => 3 ) {
\& my $self = shift;
\& # ...
\& }
\&
\& sub save : Callback {
\& my $self = shift;
\& # ...
\& }
.Ve
.PP
In these examples, the \*(L"setup\*(R" callback has been configured with a priority
level of \*(L"3\*(R". This ensures that it will always execute before the \*(L"save\*(R"
callback, which has the default priority of \*(L"5\*(R". Obviously, this is true
regardless of the order of the fields in the hash:
.PP
.Vb 2
\& my $params = { "DEFAULT|save_cb" => \*(AqSave World\*(Aq,
\& "DEFAULT|setup_cb" => 1 };
.Ve
.PP
In this configuration, the \*(L"setup\*(R" callback will always execute first because
of its higher priority.
.PP
Although the \*(L"save\*(R" callback got the default priority of \*(L"5\*(R", this too can be
customized to a different priority level via the \f(CW\*(C`default_priority\*(C'\fR parameter
to \f(CW\*(C`new()\*(C'\fR for functional callbacks and the \f(CW\*(C`default_priority\*(C'\fR to the class
declaration for object-oriented callbacks. For example, this functional
callback configuration:
.PP
.Vb 8
\& my $cb_request = Params::CallbackRequest\->new
\& ( callbacks => [ { cb_key => \*(Aqsetup\*(Aq,
\& priority => 3,
\& cb => \e&setup },
\& { cb_key => \*(Aqsave\*(Aq,
\& cb => \e&save }
\& ],
\& default_priority => 2 );
.Ve
.PP
Or this Params::Callback subclass declaration:
.PP
.Vb 4
\& package MyApp::CallbackHandler;
\& use base qw(Params::Callback);
\& _\|_PACKAGE_\|_\->register_subclass( class_key => \*(AqMyCBHandler\*(Aq,
\& default_priority => 2 );
.Ve
.PP
Will cause the \*(L"save\*(R" callback to always execute before the \*(L"setup\*(R" callback,
since its priority level will default to \*(L"2\*(R".
.PP
In addition, the priority level can be overridden via the parameter key itself
by appending a priority level to the end of the key name. Hence, this example:
.PP
.Vb 2
\& my $params = { "DEFAULT|save_cb2" => \*(AqSave World\*(Aq,
\& "DEFAULT|setup_cb" => 1 };
.Ve
.PP
Causes the \*(L"save\*(R" callback to execute before the \*(L"setup\*(R" callback by
overriding the \*(L"save\*(R" callback's priority to level \*(L"2\*(R". Of course, any other
parameter key that triggers the \*(L"save\*(R" callback without a priority override
will still execute the \*(L"save\*(R" callback at its configured level.
.SS "Request Callbacks"
.IX Subsection "Request Callbacks"
Request callbacks come in two flavors: those that execute before the
parameter-triggered callbacks, and those that execute after the
parameter-triggered callbacks. Functional request callbacks may be specified
via the \f(CW\*(C`pre_callbacks\*(C'\fR and \f(CW\*(C`post_callbacks\*(C'\fR parameters to \f(CW\*(C`new()\*(C'\fR,
respectively:
.PP
.Vb 3
\& my $cb_request = Params::CallbackRequest\->new
\& ( pre_callbacks => [ \e&translate, \e&foobarate ],
\& post_callbacks => [ \e&escape, \e&negate ] );
.Ve
.PP
Object-oriented request callbacks may be declared via the \f(CW\*(C`PreCallback\*(C'\fR and
\&\f(CW\*(C`PostCallback\*(C'\fR method attributes, like so:
.PP
.Vb 4
\& sub translate : PreCallback { ... }
\& sub foobarate : PreCallback { ... }
\& sub escape : PostCallback { ... }
\& sub negate : PostCallback { ... }
.Ve
.PP
In these examples, the \f(CW\*(C`translate()\*(C'\fR and \f(CW\*(C`foobarate()\*(C'\fR subroutines or
methods will execute (in that order) before any parameter-triggered callbacks
are executed (none will be in these examples, since none are specified).
.PP
Conversely, the \f(CW\*(C`escape()\*(C'\fR and \f(CW\*(C`negate()\*(C'\fR subroutines or methods will be
executed (in that order) after all parameter-triggered callbacks have been
executed. And regardless of what parameter-triggered callbacks may be
triggered, the request callbacks will always be executed for \fIevery\fR request
(unless an exception is thrown by an earlier callback).
.PP
Although they may be used for different purposes, the \f(CW\*(C`pre_callbacks\*(C'\fR and
\&\f(CW\*(C`post_callbacks\*(C'\fR functional callback code references expect the same argument
as parameter-triggered functional callbacks: a Params::Callback object:
.PP
.Vb 4
\& sub foo {
\& my $cb = shift;
\& # Do your business here.
\& }
.Ve
.PP
Similarly, object-oriented request callback methods will be passed an object
of the class defined in the class key portion of the callback trigger \*(--
either an object of the class in which the callback is defined, or an object
of a subclass:
.PP
.Vb 4
\& sub foo : PostCallback {
\& my $self = shift;
\& # ...
\& }
.Ve
.PP
Of course, the attributes of the Params::Callback or subclass object will be
different than in parameter-triggered callbacks. For example, the \f(CW\*(C`priority\*(C'\fR,
\&\f(CW\*(C`pkg_key\*(C'\fR, and \f(CW\*(C`cb_key\*(C'\fR attributes will naturally be undefined. It will,
however, be the same instance of the object passed to all other functional
callbacks \*(-- or to all other class callbacks with the same class key \*(-- in a
single request.
.PP
Like the parameter-triggered callbacks, request callbacks run under the nose
of a custom exception handler, so if any of them \f(CW\*(C`die\*(C'\fRs, an
Params::Callback::Exception::Execution exception will be thrown. Use the
\&\f(CW\*(C`exception_handler\*(C'\fR parameter to \f(CW\*(C`new()\*(C'\fR if you don't like this.
.SH "INTERFACE"
.IX Header "INTERFACE"
.ie n .SS "Parameters To The ""new()"" Constructor"
.el .SS "Parameters To The \f(CWnew()\fP Constructor"
.IX Subsection "Parameters To The new() Constructor"
Params::CallbackRequest supports a number of its own parameters to the \f(CW\*(C`new()\*(C'\fR
constructor (though none of them, sadly, trigger callbacks). The parameters to
\&\f(CW\*(C`new()\*(C'\fR are as follows:
.ie n .IP """callbacks""" 4
.el .IP "\f(CWcallbacks\fR" 4
.IX Item "callbacks"
Parameter-triggered functional callbacks are configured via the \f(CW\*(C`callbacks\*(C'\fR
parameter. This parameter is an array reference of hash references, and each
hash reference specifies a single callback. The supported keys in the callback
specification hashes are:
.RS 4
.ie n .IP """cb_key""" 4
.el .IP "\f(CWcb_key\fR" 4
.IX Item "cb_key"
Required. A string that, when found in a properly-formatted parameter hash key,
will trigger the execution of the callback.
.ie n .IP """cb""" 4
.el .IP "\f(CWcb\fR" 4
.IX Item "cb"
Required. A reference to the Perl subroutine that will be executed when the
\&\f(CW\*(C`cb_key\*(C'\fR has been found in a parameter hash passed to \f(CW\*(C`request()\*(C'\fR. Each code
reference should expect a single argument: a Params::Callback object. The same
instance of a Params::Callback object will be used for all functional
callbacks in a single call to \f(CW\*(C`request()\*(C'\fR.
.ie n .IP """pkg_key""" 4
.el .IP "\f(CWpkg_key\fR" 4
.IX Item "pkg_key"
Optional. A key to uniquely identify the package in which the callback
subroutine is found. This parameter is useful in systems with many callbacks,
where developers may wish to use the same \f(CW\*(C`cb_key\*(C'\fR for different subroutines
in different packages. The default package key may be set via the
\&\f(CW\*(C`default_pkg_key\*(C'\fR parameter to \f(CW\*(C`new()\*(C'\fR.
.ie n .IP """priority""" 4
.el .IP "\f(CWpriority\fR" 4
.IX Item "priority"
Optional. Indicates the level of priority of a callback. Some callbacks are
more important than others, and should be executed before the others.
Params::CallbackRequest supports priority levels ranging from \*(L"0\*(R" (highest
priority) to \*(L"9\*(R" (lowest priority). The default priority for functional
callbacks may be set via the \f(CW\*(C`default_priority\*(C'\fR parameter.
.RE
.RS 4
.RE
.ie n .IP """pre_callbacks""" 4
.el .IP "\f(CWpre_callbacks\fR" 4
.IX Item "pre_callbacks"
This parameter accepts an array reference of code references that should be
executed for \fIevery\fR call to \f(CW\*(C`request()\*(C'\fR \fIbefore\fR any parameter-triggered
callbacks. They will be executed in the order in which they're listed in the
array reference. Each code reference should expect a Params::Callback object
as its sole argument. The same instance of a Params::Callback object will be
used for all functional callbacks in a single call to \f(CW\*(C`request()\*(C'\fR. Use
pre-parameter-triggered request callbacks when you want to do something with
the parameters submitted for every call to \f(CW\*(C`request()\*(C'\fR, such as convert
character sets.
.ie n .IP """post_callbacks""" 4
.el .IP "\f(CWpost_callbacks\fR" 4
.IX Item "post_callbacks"
This parameter accepts an array reference of code references that should be
executed for \fIevery\fR call to \f(CW\*(C`request()\*(C'\fR \fIafter\fR all parameter-triggered
callbacks have been called. They will be executed in the order in which
they're listed in the array reference. Each code reference should expect a
Params::Callback object as its sole argument. The same instance of a
Params::Callback object will be used for all functional callbacks in a single
call to \f(CW\*(C`request()\*(C'\fR. Use post-parameter-triggered request callbacks when you
want to do something with the parameters submitted for every call to
\&\f(CW\*(C`request()\*(C'\fR, such as encode or escape their values for presentation.
.ie n .IP """cb_classes""" 4
.el .IP "\f(CWcb_classes\fR" 4
.IX Item "cb_classes"
An array reference listing the class keys of all of the Params::Callback
subclasses containing callback methods that you want included in your
Params::CallbackRequest object. Alternatively, the \f(CW\*(C`cb_classes\*(C'\fR parameter may
simply be the word \*(L"\s-1ALL\*(R",\s0 in which case \fIall\fR Params::Callback subclasses
will have their callback methods registered with your Params::CallbackRequest
object. See the Params::Callback documentation for details
on creating callback classes and methods.
.Sp
\&\fBNote:\fR In a mod_perl environment, be sure to \f(CW\*(C`use Params::CallbackRequest\*(C'\fR
\&\fIonly\fR after you've \f(CW\*(C`use\*(C'\fRd all of the Params::Callback subclasses you need
or else you won't be able to use their callback methods.
.ie n .IP """default_priority""" 4
.el .IP "\f(CWdefault_priority\fR" 4
.IX Item "default_priority"
The priority level at which functional callbacks will be executed. Does not
apply to object-oriented callbacks. This value will be used in each hash
reference passed via the \f(CW\*(C`callbacks\*(C'\fR parameter to \f(CW\*(C`new()\*(C'\fR that lacks a
\&\f(CW\*(C`priority\*(C'\fR key. You may specify a default priority level within the range of
\&\*(L"0\*(R" (highest priority) to \*(L"9\*(R" (lowest priority). If not specified, it defaults
to \*(L"5\*(R".
.ie n .IP """default_pkg_key""" 4
.el .IP "\f(CWdefault_pkg_key\fR" 4
.IX Item "default_pkg_key"
The default package key for functional callbacks. Does not apply to
object-oriented callbacks. This value that will be used in each hash reference
passed via the \f(CW\*(C`callbacks\*(C'\fR parameter to \f(CW\*(C`new()\*(C'\fR that lacks a \f(CW\*(C`pkg_key\*(C'\fR
key. It can be any string that evaluates to a true value, and defaults to
\&\*(L"\s-1DEFAULT\*(R"\s0 if not specified.
.ie n .IP """ignore_nulls""" 4
.el .IP "\f(CWignore_nulls\fR" 4
.IX Item "ignore_nulls"
By default, Params::CallbackRequest will execute all callbacks triggered by
parameter hash keys. However, in many situations it may be desirable to skip
any callbacks that have no value for the callback field. One can do this by
simply checking \f(CW\*(C`$cbh\->value\*(C'\fR in the callback, but if you need to disable
the execution of all parameter-triggered callbacks when the callback parameter
value is undefined or the null string (''), pass the \f(CW\*(C`ignore_null\*(C'\fR parameter
with a true value. It is set to a false value by default.
.ie n .IP """leave_notes""" 4
.el .IP "\f(CWleave_notes\fR" 4
.IX Item "leave_notes"
By default, Params::CallbackRequest will clear out the contents of the hash
accessed via the \f(CW\*(C`notes()\*(C'\fR method just before returning from a call to
\&\f(CW\*(C`request()\*(C'\fR. There may be some circumstances when it's desirable to allow the
notes hash to persist beyond the duration of a a call to \f(CW\*(C`request()\*(C'\fR. For
example, a templating architecture may wish to keep the notes around for the
duration of the execution of a template request. In such cases, pass a true
value to the \f(CW\*(C`leave_notes\*(C'\fR parameter, and use the \f(CW\*(C`clear_notes()\*(C'\fR method to
manually clear out the notes hash at the appropriate point.
.ie n .IP """exception_handler""" 4
.el .IP "\f(CWexception_handler\fR" 4
.IX Item "exception_handler"
Params::CallbackRequest installs a custom exception handler during the
execution of callbacks. This custom exception handler will simply rethrow any
exception objects it comes across, but will throw a
Params::Callback::Exception::Execution exception object if it is passed only a
string value (such as is passed by \f(CW\*(C`die "fool!"\*(C'\fR).
.Sp
But if you find that you're throwing your own exceptions in your callbacks,
and want to handle them differently, pass the \f(CW\*(C`exception_handler\*(C'\fR parameter a
code reference to do what you need.
.SS "Instance Methods"
.IX Subsection "Instance Methods"
Params::CallbackRequest of course has several instance methods. I cover the most
important, first.
.PP
\fIrequest\fR
.IX Subsection "request"
.PP
.Vb 1
\& $cb_request\->request(\e%params);
\&
\& # If you\*(Aqre in a mod_perl environment, pass in an Apache request object
\& # to be passed to the Callback classes.
\& $cb_request\->request(\e%params, apache_req => $r);
\&
\& # Or pass in argument to be passed to callback class constructors.
\& $cb_request\->request(\e%params, @args);
.Ve
.PP
Executes the callbacks specified when the Params::CallbackRequest object was
created. It takes a single required argument, a hash reference of
parameters. Any subsequent arguments are passed to the constructor for each
callback class for which callbacks will be executed. By default, the only
extra parameter supported by the Params::Callback base class is an Apache
request object, which can be passed via the \f(CW\*(C`apache_req\*(C'\fR parameter. Returns
the Params::CallbackRequest object on success, or the code passed to
Params::Callback's \f(CW\*(C`abort()\*(C'\fR method if callback execution was aborted.
.PP
A single call to \f(CW\*(C`request()\*(C'\fR is referred to as a \*(L"callback request\*(R"
(naturally!). First, all pre-request callbacks are executed. Then, any
parameter-triggered callbacks triggered by the keys in the parameter hash
reference passed as the sole argument are executed. And finally, all
post-request callbacks are executed. \f(CW\*(C`request()\*(C'\fR returns the
Params::CallbackRequest object on successful completion of the request.
.PP
Any callback that calls \f(CW\*(C`abort()\*(C'\fR on its Params::Callback object will prevent
any other callbacks scheduled by the request to run subsequent to its
execution from being executed (including post-request callbacks). Furthermore,
any callback that \f(CW\*(C`die\*(C'\fRs or throws an exception will of course also prevent
any subsequent callbacks from executing, and in addition must also be caught
by the caller or the whole process will terminate:
.PP
.Vb 4
\& eval { $cb_request\->request(\e%params) };
\& if (my $err = $@) {
\& # Handle exception.
\& }
.Ve
.PP
\fInotes\fR
.IX Subsection "notes"
.PP
.Vb 3
\& $cb_request\->notes($key => $value);
\& my $val = $cb_request\->notes($key);
\& my $notes = $cb_request\->notes;
.Ve
.PP
The \f(CW\*(C`notes()\*(C'\fR method provides a place to store application data, giving
developers a way to share data among multiple callbacks over the course of a
call to \f(CW\*(C`request()\*(C'\fR. Any data stored here persists for the duration of the
request unless the \f(CW\*(C`leave_notes\*(C'\fR parameter to \f(CW\*(C`new()\*(C'\fR has been passed a true
value. In such cases, use \f(CW\*(C`clear_notes()\*(C'\fR to manually clear the notes.
.PP
Conceptually, \f(CW\*(C`notes()\*(C'\fR contains a hash of key-value pairs. \f(CW\*(C`notes($key,
$value)\*(C'\fR stores a new entry in this hash. \f(CW\*(C`notes($key)\*(C'\fR returns a previously
stored value. \f(CW\*(C`notes()\*(C'\fR without any arguments returns a reference to the
entire hash of key-value pairs.
.PP
\&\f(CW\*(C`notes()\*(C'\fR is similar to the mod_perl method \f(CW\*(C`$r\->pnotes()\*(C'\fR. The main
differences are that this \f(CW\*(C`notes()\*(C'\fR can be used in a non\-mod_perl
environment, and that its lifetime is tied to the lifetime of the call to
\&\f(CW\*(C`request()\*(C'\fR unless the \f(CW\*(C`leave_notes\*(C'\fR parameter is true.
.PP
For the sake of convenience, a shortcut to \f(CW\*(C`notes()\*(C'\fR is provide to callback
code via the \f(CW\*(C`notes()\*(C'\fR method in
Params::Callback.
.PP
\fIclear_notes\fR
.IX Subsection "clear_notes"
.PP
.Vb 1
\& $cb_request\->clear_notes;
.Ve
.PP
Use this method to clear out the notes hash. Most useful when the
\&\f(CW\*(C`leave_notes\*(C'\fR parameter to \f(CW\*(C`new()\*(C'\fR has been set to at true value and you
need to manage the clearing of notes yourself. This method is specifically
designed for a templating environment, where it may be advantageous for the
templating architecture to allow the notes to persist beyond the duration of a
call to \f(CW\*(C`request()\*(C'\fR, e.g., to keep them for the duration of a call to the
templating architecture itself. See
MasonX::Interp::WithCallbacks for an example
of this strategy.
.SS "Accessor Methods"
.IX Subsection "Accessor Methods"
The properties \f(CW\*(C`default_priority\*(C'\fR and \f(CW\*(C`default_pkg_key\*(C'\fR have standard
read-only accessor methods of the same name. For example:
.PP
.Vb 3
\& my $cb_request = Params::CallbackRequest\->new;
\& my $default_priority = $cb_request\->default_priority;
\& my $default_pkg_key = $cb_request\->default_pkg_key;
.Ve
.SH "ACKNOWLEDGMENTS"
.IX Header "ACKNOWLEDGMENTS"
Garth Webb implemented the original callbacks in Bricolage, based on an idea
he borrowed from Paul Lindner's work with Apache::ASP. My thanks to them both
for planting this great idea! This implementation is however completely
independent of previous implementations.
.SH "SEE ALSO"
.IX Header "SEE ALSO"
Params::Callback objects get passed as the sole argument
to all functional callbacks, and offer access to data relevant to the
callback. Params::Callback also defines the object-oriented callback
interface, making its documentation a must-read for anyone who wishes to
create callback classes and methods.
.PP
MasonX::Interp::WithCallbacks uses this
module to provide a callback architecture for HTML::Mason.
.SH "SUPPORT"
.IX Header "SUPPORT"
This module is stored in an open GitHub
repository . Feel free to
fork and contribute!
.PP
Please file bug reports via GitHub
Issues or by sending
mail to
bug\-params\-callbackrequest@rt.cpan.org .
.SH "AUTHOR"
.IX Header "AUTHOR"
David E. Wheeler
.SH "COPYRIGHT AND LICENSE"
.IX Header "COPYRIGHT AND LICENSE"
Copyright 2003\-2011 David E. Wheeler. Some Rights Reserved.
.PP
This library is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.