← Index
NYTProf Performance Profile   « block view • line view • sub view »
For /usr/share/koha/opac/cgi-bin/opac/opac-search.pl
  Run on Tue Oct 15 11:58:52 2013
Reported on Tue Oct 15 12:01:58 2013

Filename/usr/lib/perl5/Template/Service.pm
StatementsExecuted 80 statements in 2.07ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
111598µs852µsTemplate::Service::::BEGIN@27Template::Service::BEGIN@27
111361µs700msTemplate::Service::::processTemplate::Service::process
11140µs19.7msTemplate::Service::::_initTemplate::Service::_init
11120µs25µsTemplate::Service::::BEGIN@23Template::Service::BEGIN@23
11119µs102µsTemplate::Service::::BEGIN@29Template::Service::BEGIN@29
11114µs47µsTemplate::Service::::BEGIN@28Template::Service::BEGIN@28
11113µs83µsTemplate::Service::::BEGIN@25Template::Service::BEGIN@25
11112µs32µsTemplate::Service::::BEGIN@24Template::Service::BEGIN@24
11111µs50µsTemplate::Service::::BEGIN@31Template::Service::BEGIN@31
1118µs8µsTemplate::Service::::BEGIN@26Template::Service::BEGIN@26
0000s0sTemplate::Service::::_dumpTemplate::Service::_dump
0000s0sTemplate::Service::::_recoverTemplate::Service::_recover
0000s0sTemplate::Service::::contextTemplate::Service::context
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1#============================================================= -*-Perl-*-
2#
3# Template::Service
4#
5# DESCRIPTION
6# Module implementing a template processing service which wraps a
7# template within PRE_PROCESS and POST_PROCESS templates and offers
8# ERROR recovery.
9#
10# AUTHOR
11# Andy Wardley <abw@wardley.org>
12#
13# COPYRIGHT
14# Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
15#
16# This module is free software; you can redistribute it and/or
17# modify it under the same terms as Perl itself.
18#
19#============================================================================
20
21package Template::Service;
22
23331µs230µs
# spent 25µs (20+5) within Template::Service::BEGIN@23 which was called: # once (20µs+5µs) by Template::BEGIN@30 at line 23
use strict;
# spent 25µs making 1 call to Template::Service::BEGIN@23 # spent 5µs making 1 call to strict::import
24338µs251µs
# spent 32µs (12+20) within Template::Service::BEGIN@24 which was called: # once (12µs+20µs) by Template::BEGIN@30 at line 24
use warnings;
# spent 32µs making 1 call to Template::Service::BEGIN@24 # spent 20µs making 1 call to warnings::import
25333µs2154µs
# spent 83µs (13+70) within Template::Service::BEGIN@25 which was called: # once (13µs+70µs) by Template::BEGIN@30 at line 25
use base 'Template::Base';
# spent 83µs making 1 call to Template::Service::BEGIN@25 # spent 70µs making 1 call to base::import
26326µs18µs
# spent 8µs within Template::Service::BEGIN@26 which was called: # once (8µs+0s) by Template::BEGIN@30 at line 26
use Template::Config;
# spent 8µs making 1 call to Template::Service::BEGIN@26
273128µs1852µs
# spent 852µs (598+255) within Template::Service::BEGIN@27 which was called: # once (598µs+255µs) by Template::BEGIN@30 at line 27
use Template::Exception;
# spent 852µs making 1 call to Template::Service::BEGIN@27
28335µs281µs
# spent 47µs (14+34) within Template::Service::BEGIN@28 which was called: # once (14µs+34µs) by Template::BEGIN@30 at line 28
use Template::Constants;
# spent 47µs making 1 call to Template::Service::BEGIN@28 # spent 34µs making 1 call to Exporter::import
29346µs2186µs
# spent 102µs (19+84) within Template::Service::BEGIN@29 which was called: # once (19µs+84µs) by Template::BEGIN@30 at line 29
use Scalar::Util 'blessed';
# spent 102µs making 1 call to Template::Service::BEGIN@29 # spent 84µs making 1 call to Exporter::import
30
3131.34ms290µs
# spent 50µs (11+39) within Template::Service::BEGIN@31 which was called: # once (11µs+39µs) by Template::BEGIN@30 at line 31
use constant EXCEPTION => 'Template::Exception';
# spent 50µs making 1 call to Template::Service::BEGIN@31 # spent 39µs making 1 call to constant::import
32
331700nsour $VERSION = 2.80;
341700nsour $DEBUG = 0 unless defined $DEBUG;
351800nsour $ERROR = '';
36
37
38#========================================================================
39# ----- PUBLIC METHODS -----
40#========================================================================
41
42#------------------------------------------------------------------------
43# process($template, \%params)
44#
45# Process a template within a service framework. A service may encompass
46# PRE_PROCESS and POST_PROCESS templates and an ERROR hash which names
47# templates to be substituted for the main template document in case of
48# error. Each service invocation begins by resetting the state of the
49# context object via a call to reset(). The AUTO_RESET option may be set
50# to 0 (default: 1) to bypass this step.
51#------------------------------------------------------------------------
52
53
# spent 700ms (361µs+700) within Template::Service::process which was called: # once (361µs+700ms) by Template::process at line 66 of Template.pm
sub process {
5413µs my ($self, $template, $params) = @_;
5512µs my $context = $self->{ CONTEXT };
561800ns my ($name, $output, $procout, $error);
571700ns $output = '';
58
59 $self->debug("process($template, ",
60 defined $params ? $params : '<no params>',
6112µs ')') if $self->{ DEBUG };
62
63 $context->reset()
6416µs112µs if $self->{ AUTO_RESET };
# spent 12µs making 1 call to Template::Context::reset
65
66 # pre-request compiled template from context so that we can alias it
67 # in the stash for pre-processed templates to reference
6829µs1377ms eval { $template = $context->template($template) };
# spent 377ms making 1 call to Template::Context::template
691600ns return $self->error($@)
70 if $@;
71
72 # localise the variable stash with any parameters passed
73 # and set the 'template' variable
741800ns $params ||= { };
75 # TODO: change this to C<||=> so we can use a template parameter
7614µs $params->{ template } = $template
77 unless ref $template eq 'CODE';
7815µs1205µs $context->localise($params);
# spent 205µs making 1 call to Template::Context::localise
79
80 SERVICE: {
81 # PRE_PROCESS
82210µs eval {
8314µs foreach $name (@{ $self->{ PRE_PROCESS } }) {
84 $self->debug("PRE_PROCESS: $name") if $self->{ DEBUG };
85 $output .= $context->process($name);
86 }
87 };
8812µs last SERVICE if ($error = $@);
89
90 # PROCESS
9112µs eval {
9213µs foreach $name (@{ $self->{ PROCESS } || [ $template ] }) {
9311µs $self->debug("PROCESS: $name") if $self->{ DEBUG };
94196µs1322ms $procout .= $context->process($name);
# spent 322ms making 1 call to Template::Context::process
95 }
96 };
9718µs if ($error = $@) {
98 last SERVICE
99 unless defined ($procout = $self->_recover(\$error));
100 }
101
10212µs if (defined $procout) {
103 # WRAPPER
10411µs eval {
10514µs foreach $name (reverse @{ $self->{ WRAPPER } }) {
106 $self->debug("WRAPPER: $name") if $self->{ DEBUG };
107 $procout = $context->process($name, { content => $procout });
108 }
109 };
1101900ns last SERVICE if ($error = $@);
111115µs $output .= $procout;
112 }
113
114 # POST_PROCESS
11511µs eval {
11614µs foreach $name (@{ $self->{ POST_PROCESS } }) {
117 $self->debug("POST_PROCESS: $name") if $self->{ DEBUG };
118 $output .= $context->process($name);
119 }
120 };
12111µs last SERVICE if ($error = $@);
122 }
123
124112µs124µs $context->delocalise();
# spent 24µs making 1 call to Template::Context::delocalise
125130µs12µs delete $params->{ template };
# spent 2µs making 1 call to Template::Stash::XS::DESTROY
126
1271800ns if ($error) {
128 # $error = $error->as_string if ref $error;
129 return $self->error($error);
130 }
131
1321122µs return $output;
133}
134
135
136#------------------------------------------------------------------------
137# context()
138#
139# Returns the internal CONTEXT reference.
140#------------------------------------------------------------------------
141
142sub context {
143 return $_[0]->{ CONTEXT };
144}
145
146
147#========================================================================
148# -- PRIVATE METHODS --
149#========================================================================
150
151
# spent 19.7ms (40µs+19.6) within Template::Service::_init which was called: # once (40µs+19.6ms) by Template::Base::new at line 65 of Template/Base.pm
sub _init {
15212µs my ($self, $config) = @_;
1531900ns my ($item, $data, $context, $block, $blocks);
15411µs my $delim = $config->{ DELIMITER };
15511µs $delim = ':' unless defined $delim;
156
157 # coerce PRE_PROCESS, PROCESS and POST_PROCESS to arrays if necessary,
158 # by splitting on non-word characters
15913µs foreach $item (qw( PRE_PROCESS PROCESS POST_PROCESS WRAPPER )) {
16042µs $data = $config->{ $item };
16145µs $self->{ $item } = [ ], next unless (defined $data);
162 $data = [ split($delim, $data || '') ]
163 unless ref $data eq 'ARRAY';
164 $self->{ $item } = $data;
165 }
166 # unset PROCESS option unless explicitly specified in config
167 $self->{ PROCESS } = undef
16812µs unless defined $config->{ PROCESS };
169
17012µs $self->{ ERROR } = $config->{ ERROR } || $config->{ ERRORS };
171 $self->{ AUTO_RESET } = defined $config->{ AUTO_RESET }
17212µs ? $config->{ AUTO_RESET } : 1;
17312µs $self->{ DEBUG } = ( $config->{ DEBUG } || 0 )
174 & Template::Constants::DEBUG_SERVICE;
175
176 $context = $self->{ CONTEXT } = $config->{ CONTEXT }
17719µs119.6ms || Template::Config->context($config)
# spent 19.6ms making 1 call to Template::Config::context
178 || return $self->error(Template::Config->error);
179
18015µs return $self;
181}
182
183
184#------------------------------------------------------------------------
185# _recover(\$exception)
186#
187# Examines the internal ERROR hash array to find a handler suitable
188# for the exception object passed by reference. Selecting the handler
189# is done by delegation to the exception's select_handler() method,
190# passing the set of handler keys as arguments. A 'default' handler
191# may also be provided. The handler value represents the name of a
192# template which should be processed.
193#------------------------------------------------------------------------
194
195sub _recover {
196 my ($self, $error) = @_;
197 my $context = $self->{ CONTEXT };
198 my ($hkey, $handler, $output);
199
200 # there shouldn't ever be a non-exception object received at this
201 # point... unless a module like CGI::Carp messes around with the
202 # DIE handler.
203 return undef
204 unless blessed($$error) && $$error->isa(EXCEPTION);
205
206 # a 'stop' exception is thrown by [% STOP %] - we return the output
207 # buffer stored in the exception object
208 return $$error->text()
209 if $$error->type() eq 'stop';
210
211 my $handlers = $self->{ ERROR }
212 || return undef; ## RETURN
213
214 if (ref $handlers eq 'HASH') {
215 if ($hkey = $$error->select_handler(keys %$handlers)) {
216 $handler = $handlers->{ $hkey };
217 $self->debug("using error handler for $hkey") if $self->{ DEBUG };
218 }
219 elsif ($handler = $handlers->{ default }) {
220 # use default handler
221 $self->debug("using default error handler") if $self->{ DEBUG };
222 }
223 else {
224 return undef; ## RETURN
225 }
226 }
227 else {
228 $handler = $handlers;
229 $self->debug("using default error handler") if $self->{ DEBUG };
230 }
231
232 eval { $handler = $context->template($handler) };
233 if ($@) {
234 $$error = $@;
235 return undef; ## RETURN
236 };
237
238 $context->stash->set('error', $$error);
239 eval {
240 $output .= $context->process($handler);
241 };
242 if ($@) {
243 $$error = $@;
244 return undef; ## RETURN
245 }
246
247 return $output;
248}
249
- -
252#------------------------------------------------------------------------
253# _dump()
254#
255# Debug method which return a string representing the internal object
256# state.
257#------------------------------------------------------------------------
258
259sub _dump {
260 my $self = shift;
261 my $context = $self->{ CONTEXT }->_dump();
262 $context =~ s/\n/\n /gm;
263
264 my $error = $self->{ ERROR };
265 $error = join('',
266 "{\n",
267 (map { " $_ => $error->{ $_ }\n" }
268 keys %$error),
269 "}\n")
270 if ref $error;
271
272 local $" = ', ';
273 return <<EOF;
274$self
275PRE_PROCESS => [ @{ $self->{ PRE_PROCESS } } ]
276POST_PROCESS => [ @{ $self->{ POST_PROCESS } } ]
277ERROR => $error
278CONTEXT => $context
279EOF
280}
281
282
28316µs1;
284
285__END__