Filename | /usr/lib/x86_64-linux-gnu/perl5/5.28/Template/Service.pm |
Statements | Executed 19924392 statements in 37.0s |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
383161 | 1 | 1 | 32.0s | 2134s | process (recurses: max depth 40, inclusive time 38558s) | Template::Service::
383161 | 1 | 1 | 10.5s | 135s | _init | Template::Service::
1 | 1 | 1 | 804µs | 1.06ms | BEGIN@27 | Template::Service::
1 | 1 | 1 | 22µs | 22µs | BEGIN@26 | Template::Service::
1 | 1 | 1 | 17µs | 21µs | BEGIN@23 | Template::Service::
1 | 1 | 1 | 10µs | 38µs | BEGIN@28 | Template::Service::
1 | 1 | 1 | 9µs | 88µs | BEGIN@25 | Template::Service::
1 | 1 | 1 | 8µs | 40µs | BEGIN@24 | Template::Service::
1 | 1 | 1 | 8µs | 35µs | BEGIN@29 | Template::Service::
1 | 1 | 1 | 5µs | 34µs | BEGIN@31 | Template::Service::
2 | 2 | 1 | 2µs | 2µs | __ANON__ (xsub) | Template::Service::
0 | 0 | 0 | 0s | 0s | _dump | Template::Service::
0 | 0 | 0 | 0s | 0s | _recover | Template::Service::
0 | 0 | 0 | 0s | 0s | context | Template::Service::
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 | |||||
21 | package Template::Service; | ||||
22 | |||||
23 | 2 | 25µs | 2 | 24µs | # spent 21µs (17+4) within Template::Service::BEGIN@23 which was called:
# once (17µs+4µs) by Template::BEGIN@30 at line 23 # spent 21µs making 1 call to Template::Service::BEGIN@23
# spent 4µs making 1 call to strict::import |
24 | 2 | 25µs | 2 | 71µs | # spent 40µs (8+31) within Template::Service::BEGIN@24 which was called:
# once (8µs+31µs) by Template::BEGIN@30 at line 24 # spent 40µs making 1 call to Template::Service::BEGIN@24
# spent 31µs making 1 call to warnings::import |
25 | 2 | 26µs | 2 | 168µs | # spent 88µs (9+80) within Template::Service::BEGIN@25 which was called:
# once (9µs+80µs) by Template::BEGIN@30 at line 25 # spent 88µs making 1 call to Template::Service::BEGIN@25
# spent 80µs making 1 call to base::import |
26 | 2 | 38µs | 2 | 23µs | # spent 22µs (22+500ns) within Template::Service::BEGIN@26 which was called:
# once (22µs+500ns) by Template::BEGIN@30 at line 26 # spent 22µs making 1 call to Template::Service::BEGIN@26
# spent 500ns making 1 call to Template::Service::__ANON__ |
27 | 2 | 375µs | 2 | 1.07ms | # spent 1.06ms (804µs+260µs) within Template::Service::BEGIN@27 which was called:
# once (804µs+260µs) by Template::BEGIN@30 at line 27 # spent 1.06ms making 1 call to Template::Service::BEGIN@27
# spent 1µs making 1 call to Template::Service::__ANON__ |
28 | 2 | 25µs | 2 | 66µs | # spent 38µs (10+28) within Template::Service::BEGIN@28 which was called:
# once (10µs+28µs) by Template::BEGIN@30 at line 28 # spent 38µs making 1 call to Template::Service::BEGIN@28
# spent 28µs making 1 call to Exporter::import |
29 | 2 | 25µs | 2 | 63µs | # spent 35µs (8+28) within Template::Service::BEGIN@29 which was called:
# once (8µs+28µs) by Template::BEGIN@30 at line 29 # spent 35µs making 1 call to Template::Service::BEGIN@29
# spent 28µs making 1 call to Exporter::import |
30 | |||||
31 | 2 | 1.00ms | 2 | 62µs | # spent 34µs (5+28) within Template::Service::BEGIN@31 which was called:
# once (5µs+28µs) by Template::BEGIN@30 at line 31 # spent 34µs making 1 call to Template::Service::BEGIN@31
# spent 28µs making 1 call to constant::import |
32 | |||||
33 | 1 | 300ns | our $VERSION = 2.80; | ||
34 | 1 | 300ns | our $DEBUG = 0 unless defined $DEBUG; | ||
35 | 1 | 300ns | our $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 2134s (32.0+2102) within Template::Service::process which was called 383161 times, avg 5.57ms/call:
# 383161 times (32.0s+2102s) by Template::process at line 66 of Template.pm, avg 5.57ms/call | ||||
54 | 383161 | 213ms | my ($self, $template, $params) = @_; | ||
55 | 383161 | 362ms | my $context = $self->{ CONTEXT }; | ||
56 | 383161 | 188ms | my ($name, $output, $procout, $error); | ||
57 | 383161 | 340ms | $output = ''; | ||
58 | |||||
59 | $self->debug("process($template, ", | ||||
60 | defined $params ? $params : '<no params>', | ||||
61 | 383161 | 234ms | ')') if $self->{ DEBUG }; | ||
62 | |||||
63 | $context->reset() | ||||
64 | 383161 | 1.35s | 383161 | 1.71s | if $self->{ AUTO_RESET }; # spent 1.71s making 383161 calls to Template::Context::reset, avg 4µs/call |
65 | |||||
66 | # pre-request compiled template from context so that we can alias it | ||||
67 | # in the stash for pre-processed templates to reference | ||||
68 | 766322 | 1.25s | 383161 | 414s | eval { $template = $context->template($template) }; # spent 414s making 383161 calls to Template::Context::template, avg 1.08ms/call |
69 | 383161 | 175ms | return $self->error($@) | ||
70 | if $@; | ||||
71 | |||||
72 | # localise the variable stash with any parameters passed | ||||
73 | # and set the 'template' variable | ||||
74 | 383161 | 225ms | $params ||= { }; | ||
75 | # TODO: change this to C<||=> so we can use a template parameter | ||||
76 | 383161 | 904ms | $params->{ template } = $template | ||
77 | unless ref $template eq 'CODE'; | ||||
78 | 383161 | 1.38s | 383161 | 12.2s | $context->localise($params); # spent 12.2s making 383161 calls to Template::Context::localise, avg 32µs/call |
79 | |||||
80 | SERVICE: { | ||||
81 | # PRE_PROCESS | ||||
82 | 766322 | 431ms | eval { | ||
83 | 383161 | 532ms | foreach $name (@{ $self->{ PRE_PROCESS } }) { | ||
84 | $self->debug("PRE_PROCESS: $name") if $self->{ DEBUG }; | ||||
85 | $output .= $context->process($name); | ||||
86 | } | ||||
87 | }; | ||||
88 | 383161 | 334ms | last SERVICE if ($error = $@); | ||
89 | |||||
90 | # PROCESS | ||||
91 | 383161 | 173ms | eval { | ||
92 | 383161 | 730ms | foreach $name (@{ $self->{ PROCESS } || [ $template ] }) { | ||
93 | 383161 | 288ms | $self->debug("PROCESS: $name") if $self->{ DEBUG }; | ||
94 | 383161 | 1.76s | 383161 | 2133s | $procout .= $context->process($name); # spent 40220s making 383161 calls to Template::Context::process, avg 105ms/call, recursion: max depth 41, sum of overlapping time 38087s |
95 | } | ||||
96 | }; | ||||
97 | 383161 | 247ms | if ($error = $@) { | ||
98 | last SERVICE | ||||
99 | unless defined ($procout = $self->_recover(\$error)); | ||||
100 | } | ||||
101 | |||||
102 | 383161 | 281ms | if (defined $procout) { | ||
103 | # WRAPPER | ||||
104 | 383161 | 146ms | eval { | ||
105 | 383161 | 604ms | foreach $name (reverse @{ $self->{ WRAPPER } }) { | ||
106 | $self->debug("WRAPPER: $name") if $self->{ DEBUG }; | ||||
107 | $procout = $context->process($name, { content => $procout }); | ||||
108 | } | ||||
109 | }; | ||||
110 | 383161 | 154ms | last SERVICE if ($error = $@); | ||
111 | 383161 | 220ms | $output .= $procout; | ||
112 | } | ||||
113 | |||||
114 | # POST_PROCESS | ||||
115 | 383161 | 146ms | eval { | ||
116 | 383161 | 636ms | foreach $name (@{ $self->{ POST_PROCESS } }) { | ||
117 | $self->debug("POST_PROCESS: $name") if $self->{ DEBUG }; | ||||
118 | $output .= $context->process($name); | ||||
119 | } | ||||
120 | }; | ||||
121 | 383161 | 109ms | last SERVICE if ($error = $@); | ||
122 | } | ||||
123 | |||||
124 | 383161 | 742ms | 383161 | 7.88s | $context->delocalise(); # spent 7.88s making 383161 calls to Template::Context::delocalise, avg 21µs/call |
125 | 383161 | 381ms | delete $params->{ template }; | ||
126 | |||||
127 | 383161 | 199ms | if ($error) { | ||
128 | # $error = $error->as_string if ref $error; | ||||
129 | return $self->error($error); | ||||
130 | } | ||||
131 | |||||
132 | 383161 | 11.8s | 383161 | 4.18s | return $output; # spent 4.18s making 383161 calls to Template::Document::AUTOLOAD, avg 11µs/call |
133 | } | ||||
134 | |||||
135 | |||||
136 | #------------------------------------------------------------------------ | ||||
137 | # context() | ||||
138 | # | ||||
139 | # Returns the internal CONTEXT reference. | ||||
140 | #------------------------------------------------------------------------ | ||||
141 | |||||
142 | sub context { | ||||
143 | return $_[0]->{ CONTEXT }; | ||||
144 | } | ||||
145 | |||||
146 | |||||
147 | #======================================================================== | ||||
148 | # -- PRIVATE METHODS -- | ||||
149 | #======================================================================== | ||||
150 | |||||
151 | # spent 135s (10.5+125) within Template::Service::_init which was called 383161 times, avg 353µs/call:
# 383161 times (10.5s+125s) by Template::Base::new at line 65 of Template/Base.pm, avg 353µs/call | ||||
152 | 383161 | 248ms | my ($self, $config) = @_; | ||
153 | 383161 | 240ms | my ($item, $data, $context, $block, $blocks); | ||
154 | 383161 | 359ms | my $delim = $config->{ DELIMITER }; | ||
155 | 383161 | 442ms | $delim = ':' unless defined $delim; | ||
156 | |||||
157 | # coerce PRE_PROCESS, PROCESS and POST_PROCESS to arrays if necessary, | ||||
158 | # by splitting on non-word characters | ||||
159 | 383161 | 943ms | foreach $item (qw( PRE_PROCESS PROCESS POST_PROCESS WRAPPER )) { | ||
160 | 1532644 | 620ms | $data = $config->{ $item }; | ||
161 | 1532644 | 2.04s | $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 | ||||
168 | 383161 | 589ms | unless defined $config->{ PROCESS }; | ||
169 | |||||
170 | 383161 | 722ms | $self->{ ERROR } = $config->{ ERROR } || $config->{ ERRORS }; | ||
171 | $self->{ AUTO_RESET } = defined $config->{ AUTO_RESET } | ||||
172 | 383161 | 518ms | ? $config->{ AUTO_RESET } : 1; | ||
173 | 383161 | 471ms | $self->{ DEBUG } = ( $config->{ DEBUG } || 0 ) | ||
174 | & Template::Constants::DEBUG_SERVICE; | ||||
175 | |||||
176 | $context = $self->{ CONTEXT } = $config->{ CONTEXT } | ||||
177 | 383161 | 1.50s | 383161 | 125s | || Template::Config->context($config) # spent 125s making 383161 calls to Template::Config::context, avg 326µs/call |
178 | || return $self->error(Template::Config->error); | ||||
179 | |||||
180 | 383161 | 1.78s | 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 | |||||
195 | sub _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 | |||||
259 | sub _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 | ||||
275 | PRE_PROCESS => [ @{ $self->{ PRE_PROCESS } } ] | ||||
276 | POST_PROCESS => [ @{ $self->{ POST_PROCESS } } ] | ||||
277 | ERROR => $error | ||||
278 | CONTEXT => $context | ||||
279 | EOF | ||||
280 | } | ||||
281 | |||||
282 | |||||
283 | 1 | 4µs | 1; | ||
284 | |||||
285 | __END__ | ||||
sub Template::Service::__ANON__; # xsub |