Filename | /usr/lib/perl5/Template/Document.pm |
Statements | Executed 278 statements in 31.3ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
8 | 1 | 1 | 29.0ms | 29.8ms | new | Template::Document::
8 | 1 | 1 | 765µs | 765µs | CORE:match (opcode) | Template::Document::
9 | 1 | 1 | 507µs | 322ms | process (recurses: max depth 1, inclusive time 22.3ms) | Template::Document::
9 | 2 | 2 | 108µs | 156µs | AUTOLOAD | Template::Document::
9 | 1 | 1 | 48µs | 48µs | CORE:subst (opcode) | Template::Document::
1 | 1 | 1 | 16µs | 20µs | BEGIN@24 | Template::Document::
1 | 1 | 1 | 10µs | 31µs | BEGIN@27 | Template::Document::
1 | 1 | 1 | 10µs | 22µs | BEGIN@25 | Template::Document::
1 | 1 | 1 | 9µs | 9µs | BEGIN@34 | Template::Document::
1 | 1 | 1 | 8µs | 54µs | BEGIN@26 | Template::Document::
1 | 1 | 1 | 4µs | 4µs | blocks | Template::Document::
0 | 0 | 0 | 0s | 0s | _dump | Template::Document::
0 | 0 | 0 | 0s | 0s | as_perl | Template::Document::
0 | 0 | 0 | 0s | 0s | block | Template::Document::
0 | 0 | 0 | 0s | 0s | catch_warnings | Template::Document::
0 | 0 | 0 | 0s | 0s | write_perl_file | Template::Document::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | ##============================================================= -*-Perl-*- | ||||
2 | # | ||||
3 | # Template::Document | ||||
4 | # | ||||
5 | # DESCRIPTION | ||||
6 | # Module defining a class of objects which encapsulate compiled | ||||
7 | # templates, storing additional block definitions and metadata | ||||
8 | # as well as the compiled Perl sub-routine representing the main | ||||
9 | # template content. | ||||
10 | # | ||||
11 | # AUTHOR | ||||
12 | # Andy Wardley <abw@wardley.org> | ||||
13 | # | ||||
14 | # COPYRIGHT | ||||
15 | # Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved. | ||||
16 | # | ||||
17 | # This module is free software; you can redistribute it and/or | ||||
18 | # modify it under the same terms as Perl itself. | ||||
19 | # | ||||
20 | #============================================================================ | ||||
21 | |||||
22 | package Template::Document; | ||||
23 | |||||
24 | 3 | 27µs | 2 | 24µs | # spent 20µs (16+4) within Template::Document::BEGIN@24 which was called:
# once (16µs+4µs) by Template::Provider::BEGIN@46 at line 24 # spent 20µs making 1 call to Template::Document::BEGIN@24
# spent 4µs making 1 call to strict::import |
25 | 3 | 26µs | 2 | 34µs | # spent 22µs (10+12) within Template::Document::BEGIN@25 which was called:
# once (10µs+12µs) by Template::Provider::BEGIN@46 at line 25 # spent 22µs making 1 call to Template::Document::BEGIN@25
# spent 12µs making 1 call to warnings::import |
26 | 3 | 33µs | 2 | 101µs | # spent 54µs (8+46) within Template::Document::BEGIN@26 which was called:
# once (8µs+46µs) by Template::Provider::BEGIN@46 at line 26 # spent 54µs making 1 call to Template::Document::BEGIN@26
# spent 46µs making 1 call to base::import |
27 | 3 | 135µs | 2 | 52µs | # spent 31µs (10+21) within Template::Document::BEGIN@27 which was called:
# once (10µs+21µs) by Template::Provider::BEGIN@46 at line 27 # spent 31µs making 1 call to Template::Document::BEGIN@27
# spent 21µs making 1 call to Exporter::import |
28 | |||||
29 | 1 | 500ns | our $VERSION = 2.79; | ||
30 | 1 | 500ns | our $DEBUG = 0 unless defined $DEBUG; | ||
31 | 1 | 600ns | our $ERROR = ''; | ||
32 | 1 | 300ns | our ($COMPERR, $AUTOLOAD, $UNICODE); | ||
33 | |||||
34 | # spent 9µs within Template::Document::BEGIN@34 which was called:
# once (9µs+0s) by Template::Provider::BEGIN@46 at line 47 | ||||
35 | # UNICODE is supported in versions of Perl from 5.008 onwards | ||||
36 | 1 | 7µs | if ($UNICODE = $] > 5.007 ? 1 : 0) { | ||
37 | 1 | 3µs | if ($] > 5.008) { | ||
38 | # utf8::is_utf8() available from Perl 5.8.1 onwards | ||||
39 | *is_utf8 = \&utf8::is_utf8; | ||||
40 | } | ||||
41 | elsif ($] == 5.008) { | ||||
42 | # use Encode::is_utf8() for Perl 5.8.0 | ||||
43 | require Encode; | ||||
44 | *is_utf8 = \&Encode::is_utf8; | ||||
45 | } | ||||
46 | } | ||||
47 | 1 | 976µs | 1 | 9µs | } # spent 9µs making 1 call to Template::Document::BEGIN@34 |
48 | |||||
49 | |||||
50 | #======================================================================== | ||||
51 | # ----- PUBLIC METHODS ----- | ||||
52 | #======================================================================== | ||||
53 | |||||
54 | #------------------------------------------------------------------------ | ||||
55 | # new(\%document) | ||||
56 | # | ||||
57 | # Creates a new self-contained Template::Document object which | ||||
58 | # encapsulates a compiled Perl sub-routine, $block, any additional | ||||
59 | # BLOCKs defined within the document ($defblocks, also Perl sub-routines) | ||||
60 | # and additional $metadata about the document. | ||||
61 | #------------------------------------------------------------------------ | ||||
62 | |||||
63 | # spent 29.8ms (29.0+765µs) within Template::Document::new which was called 8 times, avg 3.72ms/call:
# 8 times (29.0ms+765µs) by Template::Provider::_compile at line 894 of Template/Provider.pm, avg 3.72ms/call | ||||
64 | 8 | 15µs | my ($class, $doc) = @_; | ||
65 | 8 | 62µs | my ($block, $defblocks, $metadata) = @$doc{ qw( BLOCK DEFBLOCKS METADATA ) }; | ||
66 | 8 | 5µs | $defblocks ||= { }; | ||
67 | 8 | 4µs | $metadata ||= { }; | ||
68 | |||||
69 | # evaluate Perl code in $block to create sub-routine reference if necessary | ||||
70 | 8 | 9µs | unless (ref $block) { | ||
71 | 8 | 123µs | local $SIG{__WARN__} = \&catch_warnings; | ||
72 | 8 | 11µs | $COMPERR = ''; | ||
73 | |||||
74 | # DON'T LOOK NOW! - blindly untainting can make you go blind! | ||||
75 | 8 | 832µs | 8 | 765µs | $block =~ /(.*)/s; # spent 765µs making 8 calls to Template::Document::CORE:match, avg 96µs/call |
76 | 8 | 126µs | $block = $1; | ||
77 | |||||
78 | 8 | 28.0ms | $block = eval $block; # spent 15µs executing statements in string eval
# spent 11µs executing statements in string eval
# spent 6µs executing statements in string eval
# spent 6µs executing statements in string eval
# spent 6µs executing statements in string eval
# spent 6µs executing statements in string eval
# spent 5µs executing statements in string eval
# spent 4µs executing statements in string eval | ||
79 | 8 | 41µs | return $class->error($@) | ||
80 | unless defined $block; | ||||
81 | } | ||||
82 | |||||
83 | # same for any additional BLOCK definitions | ||||
84 | @$defblocks{ keys %$defblocks } = | ||||
85 | # MORE BLIND UNTAINTING - turn away if you're squeamish | ||||
86 | map { | ||||
87 | 8 | 42µs | ref($_) | ||
88 | ? $_ | ||||
89 | : ( /(.*)/s && eval($1) or return $class->error($@) ) | ||||
90 | } values %$defblocks; | ||||
91 | |||||
92 | 8 | 175µs | bless { | ||
93 | %$metadata, | ||||
94 | _BLOCK => $block, | ||||
95 | _DEFBLOCKS => $defblocks, | ||||
96 | _HOT => 0, | ||||
97 | }, $class; | ||||
98 | } | ||||
99 | |||||
100 | |||||
101 | #------------------------------------------------------------------------ | ||||
102 | # block() | ||||
103 | # | ||||
104 | # Returns a reference to the internal sub-routine reference, _BLOCK, | ||||
105 | # that constitutes the main document template. | ||||
106 | #------------------------------------------------------------------------ | ||||
107 | |||||
108 | sub block { | ||||
109 | return $_[0]->{ _BLOCK }; | ||||
110 | } | ||||
111 | |||||
112 | |||||
113 | #------------------------------------------------------------------------ | ||||
114 | # blocks() | ||||
115 | # | ||||
116 | # Returns a reference to a hash array containing any BLOCK definitions | ||||
117 | # from the template. The hash keys are the BLOCK nameand the values | ||||
118 | # are references to Template::Document objects. Returns 0 (# an empty hash) | ||||
119 | # if no blocks are defined. | ||||
120 | #------------------------------------------------------------------------ | ||||
121 | |||||
122 | # spent 4µs within Template::Document::blocks which was called:
# once (4µs+0s) by Template::Context::process at line 339 of Template/Context.pm | ||||
123 | 1 | 6µs | return $_[0]->{ _DEFBLOCKS }; | ||
124 | } | ||||
125 | |||||
126 | |||||
127 | #------------------------------------------------------------------------ | ||||
128 | # process($context) | ||||
129 | # | ||||
130 | # Process the document in a particular context. Checks for recursion, | ||||
131 | # registers the document with the context via visit(), processes itself, | ||||
132 | # and then unwinds with a large gin and tonic. | ||||
133 | #------------------------------------------------------------------------ | ||||
134 | |||||
135 | # spent 322ms (507µs+321) within Template::Document::process which was called 9 times, avg 35.8ms/call:
# 9 times (507µs+321ms) by Template::Context::process at line 347 of Template/Context.pm, avg 35.8ms/call | ||||
136 | 9 | 12µs | my ($self, $context) = @_; | ||
137 | 9 | 11µs | my $defblocks = $self->{ _DEFBLOCKS }; | ||
138 | 9 | 4µs | my $output; | ||
139 | |||||
140 | |||||
141 | # check we're not already visiting this template | ||||
142 | return $context->throw(Template::Constants::ERROR_FILE, | ||||
143 | "recursion into '$self->{ name }'") | ||||
144 | 9 | 12µs | if $self->{ _HOT } && ! $context->{ RECURSION }; ## RETURN ## | ||
145 | |||||
146 | 9 | 37µs | 9 | 55µs | $context->visit($self, $defblocks); # spent 55µs making 9 calls to Template::Context::visit, avg 6µs/call |
147 | |||||
148 | 9 | 8µs | $self->{ _HOT } = 1; | ||
149 | 9 | 55µs | eval { | ||
150 | 9 | 11µs | my $block = $self->{ _BLOCK }; | ||
151 | 9 | 43µs | 9 | 344ms | $output = &$block($context); # spent 322ms making 1 call to Template::Document::__ANON__[/usr/share/koha/opac/htdocs/opac-tmpl/prog/en-NZ/modules/opac-results.tt:541]
# spent 9.90ms making 1 call to Template::Document::__ANON__[/usr/share/koha/opac/htdocs/opac-tmpl/prog/en-NZ/includes/opac-facets.inc:32]
# spent 7.46ms making 2 calls to Template::Document::__ANON__[/usr/share/koha/opac/htdocs/opac-tmpl/prog/en-NZ/includes/page-numbers.inc:31], avg 3.73ms/call
# spent 3.38ms making 1 call to Template::Document::__ANON__[/usr/share/koha/opac/htdocs/opac-tmpl/prog/en-NZ/includes/masthead.inc:111]
# spent 574µs making 1 call to Template::Document::__ANON__[/usr/share/koha/opac/htdocs/opac-tmpl/prog/en-NZ/includes/doc-head-close.inc:121]
# spent 315µs making 1 call to Template::Document::__ANON__[/usr/share/koha/opac/htdocs/opac-tmpl/prog/en-NZ/includes/opac-bottom.inc:74]
# spent 148µs making 1 call to Template::Document::__ANON__[/usr/share/koha/opac/htdocs/opac-tmpl/prog/en-NZ/includes/resort_form.inc:48]
# spent 86µs making 1 call to Template::Document::__ANON__[/usr/share/koha/opac/htdocs/opac-tmpl/prog/en-NZ/includes/doc-head-open.inc:14] |
152 | }; | ||||
153 | 9 | 26µs | $self->{ _HOT } = 0; | ||
154 | |||||
155 | 9 | 46µs | 9 | 57µs | $context->leave(); # spent 57µs making 9 calls to Template::Context::leave, avg 6µs/call |
156 | |||||
157 | 9 | 4µs | die $context->catch($@) | ||
158 | if $@; | ||||
159 | |||||
160 | 9 | 183µs | return $output; | ||
161 | } | ||||
162 | |||||
163 | |||||
164 | #------------------------------------------------------------------------ | ||||
165 | # AUTOLOAD | ||||
166 | # | ||||
167 | # Provides pseudo-methods for read-only access to various internal | ||||
168 | # members. | ||||
169 | #------------------------------------------------------------------------ | ||||
170 | |||||
171 | # spent 156µs (108+48) within Template::Document::AUTOLOAD which was called 9 times, avg 17µs/call:
# 8 times (95µs+42µs) by main::NULL at line 0 of /usr/share/koha/opac/cgi-bin/opac/opac-search.pl, avg 17µs/call
# once (13µs+5µs) by Template::Stash::XS::get at line 22 of /usr/share/koha/opac/htdocs/opac-tmpl/prog/en-NZ/includes/opac-bottom.inc | ||||
172 | 9 | 11µs | my $self = shift; | ||
173 | 9 | 5µs | my $method = $AUTOLOAD; | ||
174 | |||||
175 | 9 | 96µs | 9 | 48µs | $method =~ s/.*:://; # spent 48µs making 9 calls to Template::Document::CORE:subst, avg 5µs/call |
176 | 9 | 77µs | return if $method eq 'DESTROY'; | ||
177 | # my ($pkg, $file, $line) = caller(); | ||||
178 | # print STDERR "called $self->AUTOLOAD($method) from $file line $line\n"; | ||||
179 | 1 | 4µs | return $self->{ $method }; | ||
180 | } | ||||
181 | |||||
182 | |||||
183 | #======================================================================== | ||||
184 | # ----- PRIVATE METHODS ----- | ||||
185 | #======================================================================== | ||||
186 | |||||
187 | |||||
188 | #------------------------------------------------------------------------ | ||||
189 | # _dump() | ||||
190 | # | ||||
191 | # Debug method which returns a string representing the internal state | ||||
192 | # of the object. | ||||
193 | #------------------------------------------------------------------------ | ||||
194 | |||||
195 | sub _dump { | ||||
196 | my $self = shift; | ||||
197 | my $dblks; | ||||
198 | my $output = "$self : $self->{ name }\n"; | ||||
199 | |||||
200 | $output .= "BLOCK: $self->{ _BLOCK }\nDEFBLOCKS:\n"; | ||||
201 | |||||
202 | if ($dblks = $self->{ _DEFBLOCKS }) { | ||||
203 | foreach my $b (keys %$dblks) { | ||||
204 | $output .= " $b: $dblks->{ $b }\n"; | ||||
205 | } | ||||
206 | } | ||||
207 | |||||
208 | return $output; | ||||
209 | } | ||||
210 | |||||
211 | |||||
212 | #======================================================================== | ||||
213 | # ----- CLASS METHODS ----- | ||||
214 | #======================================================================== | ||||
215 | |||||
216 | #------------------------------------------------------------------------ | ||||
217 | # as_perl($content) | ||||
218 | # | ||||
219 | # This method expects a reference to a hash passed as the first argument | ||||
220 | # containing 3 items: | ||||
221 | # METADATA # a hash of template metadata | ||||
222 | # BLOCK # string containing Perl sub definition for main block | ||||
223 | # DEFBLOCKS # hash containing further subs for addional BLOCK defs | ||||
224 | # It returns a string containing Perl code which, when evaluated and | ||||
225 | # executed, will instantiate a new Template::Document object with the | ||||
226 | # above data. On error, it returns undef with an appropriate error | ||||
227 | # message set in $ERROR. | ||||
228 | #------------------------------------------------------------------------ | ||||
229 | |||||
230 | sub as_perl { | ||||
231 | my ($class, $content) = @_; | ||||
232 | my ($block, $defblocks, $metadata) = @$content{ qw( BLOCK DEFBLOCKS METADATA ) }; | ||||
233 | |||||
234 | $block =~ s/\n(?!#line)/\n /g; | ||||
235 | $block =~ s/\s+$//; | ||||
236 | |||||
237 | $defblocks = join('', map { | ||||
238 | my $code = $defblocks->{ $_ }; | ||||
239 | $code =~ s/\n(?!#line)/\n /g; | ||||
240 | $code =~ s/\s*$//; | ||||
241 | " '$_' => $code,\n"; | ||||
242 | } keys %$defblocks); | ||||
243 | $defblocks =~ s/\s+$//; | ||||
244 | |||||
245 | $metadata = join('', map { | ||||
246 | my $x = $metadata->{ $_ }; | ||||
247 | $x =~ s/(['\\])/\\$1/g; | ||||
248 | " '$_' => '$x',\n"; | ||||
249 | } keys %$metadata); | ||||
250 | $metadata =~ s/\s+$//; | ||||
251 | |||||
252 | return <<EOF | ||||
253 | #------------------------------------------------------------------------ | ||||
254 | # Compiled template generated by the Template Toolkit version $Template::VERSION | ||||
255 | #------------------------------------------------------------------------ | ||||
256 | |||||
257 | $class->new({ | ||||
258 | METADATA => { | ||||
259 | $metadata | ||||
260 | }, | ||||
261 | BLOCK => $block, | ||||
262 | DEFBLOCKS => { | ||||
263 | $defblocks | ||||
264 | }, | ||||
265 | }); | ||||
266 | EOF | ||||
267 | } | ||||
268 | |||||
269 | |||||
270 | #------------------------------------------------------------------------ | ||||
271 | # write_perl_file($filename, \%content) | ||||
272 | # | ||||
273 | # This method calls as_perl() to generate the Perl code to represent a | ||||
274 | # compiled template with the content passed as the second argument. | ||||
275 | # It then writes this to the file denoted by the first argument. | ||||
276 | # | ||||
277 | # Returns 1 on success. On error, sets the $ERROR package variable | ||||
278 | # to contain an error message and returns undef. | ||||
279 | #------------------------------------------------------------------------ | ||||
280 | |||||
281 | sub write_perl_file { | ||||
282 | my ($class, $file, $content) = @_; | ||||
283 | my ($fh, $tmpfile); | ||||
284 | |||||
285 | return $class->error("invalid filename: $file") | ||||
286 | unless $file =~ /^(.+)$/s; | ||||
287 | |||||
288 | eval { | ||||
289 | require File::Temp; | ||||
290 | require File::Basename; | ||||
291 | ($fh, $tmpfile) = File::Temp::tempfile( | ||||
292 | DIR => File::Basename::dirname($file) | ||||
293 | ); | ||||
294 | my $perlcode = $class->as_perl($content) || die $!; | ||||
295 | |||||
296 | if ($UNICODE && is_utf8($perlcode)) { | ||||
297 | $perlcode = "use utf8;\n\n$perlcode"; | ||||
298 | binmode $fh, ":utf8"; | ||||
299 | } | ||||
300 | print $fh $perlcode; | ||||
301 | close($fh); | ||||
302 | }; | ||||
303 | return $class->error($@) if $@; | ||||
304 | return rename($tmpfile, $file) | ||||
305 | || $class->error($!); | ||||
306 | } | ||||
307 | |||||
308 | |||||
309 | #------------------------------------------------------------------------ | ||||
310 | # catch_warnings($msg) | ||||
311 | # | ||||
312 | # Installed as | ||||
313 | #------------------------------------------------------------------------ | ||||
314 | |||||
315 | sub catch_warnings { | ||||
316 | $COMPERR .= join('', @_); | ||||
317 | } | ||||
318 | |||||
319 | |||||
320 | 1 | 4µs | 1; | ||
321 | |||||
322 | __END__ | ||||
# spent 765µs within Template::Document::CORE:match which was called 8 times, avg 96µs/call:
# 8 times (765µs+0s) by Template::Document::new at line 75, avg 96µs/call | |||||
# spent 48µs within Template::Document::CORE:subst which was called 9 times, avg 5µs/call:
# 9 times (48µs+0s) by Template::Document::AUTOLOAD at line 175, avg 5µs/call |