.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" 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 .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . 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 >0, 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 .\" ======================================================================== .\" .IX Title "HTML::Mason::Plugin 3pm" .TH HTML::Mason::Plugin 3pm 2024-03-05 "perl v5.38.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 HTML::Mason::Plugin \- Plugin Base class for Mason .SH SYNOPIS .IX Header "SYNOPIS" .Vb 3 \& package MasonX::Plugin::Timer; \& use base qw(HTML::Mason::Plugin); \& use Time::HiRes; \& \& sub start_component_hook { \& my ($self, $context) = @_; \& push @{$self\->{ timers }}, Time::HiRes::time; \& } \& \& sub end_component_hook { \& my ($self, $context) = @_; \& my $elapsed = Time::HiRes::time \- pop @{$self\->{ timers }}; \& printf STDERR "Component \*(Aq%s\*(Aq took %.1f seconds\en", \& $context\->comp\->title, $elapsed; \& } \& \& 1; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Use a Mason plugin to have actions occur at the beginning or end of requests or components. Plugins are activated by passing plugins in the interpreter or request object. Each plugin in the list can be specified as a class name (in which case the plugin object is created once for each request) or as an actual object of the plugin class. .PP If your plugin can be configured, place the configuration in class variables \- for example, .PP .Vb 1 \& $MasonX::Plugin::Timer::Units = \*(Aqseconds\*(Aq; .Ve .PP These can be set either from httpd.conf via PerlSetVar directives, or in perl directly from a handler.pl file. .SH "PLUGIN HOOKS" .IX Header "PLUGIN HOOKS" A plugin class defines one or more of the following hooks (methods): \&\fIstart_request_hook\fR, \fIend_request_hook\fR, \fIstart_component_hook\fR, and \fIend_component_hook\fR. .PP Every hook receives two arguments: the plugin object itself, and a context object with various methods. .IP start_request_hook 4 .IX Item "start_request_hook" \&\f(CW\*(C`start_request_hook\*(C'\fR is called before the Mason request begins execution. Its context has the following read-only methods: .Sp .Vb 2 \& request # the current request ($m) \& args # arguments the request was called with .Ve .Sp When called in scalar context, \fIargs\fR returns a list reference which may be modified to change or add to the arguments passed to the first component. When called in list context, \fIargs\fR returns a list (which may be assigned to a hash). .Sp Note that subrequests (see HTML::Mason::Request will create a new plugin object and execute this code again; you can skip your code for subrequests by checking \f(CW\*(C`is_subrequest\*(C'\fR on \fIrequest\fR. e.g. .Sp .Vb 6 \& sub start_request_hook { \& my ($self, $context) = @_; \& unless ($context\->request\->is_subrequest()) { \& # perform hook action \& } \& } .Ve .Sp Currently, this hook is called before any information about the requested component is available, so you cannot call methods like \&\f(CWbase_comp()\fR or \f(CWrequest_args()\fR on the Request object. .IP end_request_hook 4 .IX Item "end_request_hook" \&\f(CW\*(C`end_request_hook\*(C'\fR is called before the Mason request exits. Its context has the following read-only methods: .Sp .Vb 6 \& request # the current request ($m) \& args # arguments the request was called with \& output # reference to the contents of the output buffer \& wantarray # value of wantarray the request was called with \& result # arrayref of value(s) that the request is about to return \& error # reference to error, if any, that the request is about to throw .Ve .Sp When called in scalar context, \fIargs\fR returns a list reference; when called in list context, it returns a list (which may be assigned to a hash). .Sp \&\fIresult\fR always contains an array ref; if \fIwantarray\fR is 0, the return value is the the first element of that array. The plugin may modify \fIoutput\fR to affect what the request outputs, and \&\fIresult\fR and \fIerror\fR to affect what the request returns. .IP start_component_hook 4 .IX Item "start_component_hook" \&\f(CW\*(C`start_component_hook\*(C'\fR is called before a component begins executing. Its context has the following read-only methods: .Sp .Vb 3 \& request # the current request ($m) \& comp # the component object \& args # arrayref of arguments the component was called with .Ve .Sp The plugin may NOT modify \fIargs\fR currently. .IP end_component_hook 4 .IX Item "end_component_hook" \&\f(CWend_component_hook()\fR is called after a component has completed. Its context has the following read-only methods: .Sp .Vb 6 \& request # the current request ($m) \& comp # the component object \& args # arrayref of arguments the component was called with \& wantarray # value of wantarray the component was called with \& result # arrayref of value(s) that the component is about to return \& error # reference to error, if any, that the component is about to throw .Ve .Sp \&\fIresult\fR always contains an array ref; if \fIwantarray\fR is 0, the return value is the first element of that array. The plugin may modify both \fIresult\fR and \fIerror\fR to affect how the request returns. .Sp It would be desirable for this hook to have access to the component's output as well as its return value, but this is currently impossible because output from multiple components combine into a single buffer. .SH WARNINGS .IX Header "WARNINGS" Do not keep an unweakened reference to a request or component object in your plugin object, or you will create a nasty circular reference.