lib/Template/Context.pm

filter: Statementstmt 98.2% 283 / 288 Branchbran 78.4% 149 / 190 Conditioncond 70.4% 74 / 105 MC/DCmcdc 43.1% 25 / 58 Subroutinesub 100% 36 / 36 Podpod 100% 21 / 21 totaltotal 84.2% 588 / 698 CC 145 SCAR 28.6
CC
145
Cov
87%
template
33.7
process
27.5
_init
27.5
CRAP 17.5
« lib/Template/Parser.pm ↑ Summary blib/lib/Template/VMethods.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Context
4 #
5 # DESCRIPTION
6 # Module defining a context in which a template document is processed.
7 # This is the runtime processing interface through which templates
8 # can access the functionality of the Template Toolkit.
9 #
10 # AUTHOR
11 # Andy Wardley <abw@wardley.org>
12 #
13 # COPYRIGHT
14 # Copyright (C) 1996-2022 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::Context;
22  
23 102 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 102 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 102 ▶use base 'Template::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 102 ▶use Template::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 102 ▶use Template::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 102 ▶use Template::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 102 ▶use Template::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 102 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 102 ▶use constant DOCUMENT => 'Template::Document';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 102 ▶use constant EXCEPTION => 'Template::Exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 102 ▶use constant BADGER_EXCEPTION => 'Badger::Exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 102 ▶use constant MSWin32 => $^O eq 'MSWin32';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 102 our $VERSION = '3.100';
39 102 ▶our $DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::Context::DEBUG 102 0 102
40 102 our $DEBUG_FORMAT = "\n## \$file line \$line : [% \$text %] ##\n";
41 102 our $VIEW_CLASS = 'Template::View';
42 102 our $AUTOLOAD;
43  
44 #========================================================================
45 # ----- PUBLIC METHODS -----
46 #========================================================================
47  
48 #------------------------------------------------------------------------
49 # template($name)
50 #
51 # General purpose method to fetch a template and return it in compiled
52 # form. In the usual case, the $name parameter will be a simple string
53 # containing the name of a template (e.g. 'header'). It may also be
54 # a reference to Template::Document object (or sub-class) or a Perl
55 # sub-routine. These are considered to be compiled templates and are
56 # returned intact. Finally, it may be a reference to any other kind
57 # of valid input source accepted by Template::Provider (e.g. scalar
58 # ref, glob, IO handle, etc).
59 #
60 # Templates may be cached at one of 3 different levels. The internal
61 # BLOCKS member is a local cache which holds references to all
62 # template blocks used or imported via PROCESS since the context's
63 # reset() method was last called. This is checked first and if the
64 # template is not found, the method then walks down the BLOCKSTACK
65 # list. This contains references to the block definition tables in
66 # any enclosing Template::Documents that we're visiting (e.g. we've
67 # been called via an INCLUDE and we want to access a BLOCK defined in
68 # the template that INCLUDE'd us). If nothing is defined, then we
69 # iterate through the LOAD_TEMPLATES providers list as a 'chain of
70 # responsibility' (see Design Patterns) asking each object to fetch()
71 # the template if it can.
72 #
73 # Returns the compiled template. On error, undef is returned and
74 # the internal ERROR value (read via error()) is set to contain an
75 # error message of the form "$name: $error".
76 #------------------------------------------------------------------------
77  
78 sub template {
79 3205 ▶ my ($self, $name) = @_;
Subroutine template: called CC=30 SCAR=35.2
Pod documented
80 3205 my ($prefix, $blocks, $defblocks, $provider, $template, $error);
81 3205 my ($shortname, $blockname, $providers);
82  
83 3205 ▶ $self->debug("template($name)") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 3205 3205
84  
85 # references to Template::Document (or sub-class) objects, or
86 # CODE references are assumed to be pre-compiled templates and are
87 # returned intact
88 3205 ▶ return $name
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($name) and $name->isa("Template::Document") or ref $name eq "CODE" 1434 1771 3205
Condition83%5/6
builtin::blessed($name) and $name->isa("Template::Document") or ref $name eq "CODE"
l!l&&r!l&&!r
1348861771
builtin::blessed($name) and $name->isa("Template::Document")
!ll&&!rl&&r
185701348
MC/DC66%2/3
builtin::blessed($name) and $name->isa("Template::Document") or ref $name eq "CODE"
builtin::blessed($name) $name->isa("Template::Document") ref $name eq "CODE"
Truth table60%3/5
A and B or C
A= builtin::blessed($name)B= $name->isa("Template::Document")C= ref $name eq "CODE"
ABCresult
11X1
0X11
1011
0X00
1000
89 if (blessed($name) && $name->isa(DOCUMENT))
90 || ref($name) eq 'CODE';
91  
92 1771 $shortname = $name;
93  
94 1771 ▶ unless (ref $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $name) 448 1323 1771
95  
96 448 ▶ $self->debug("looking for block [$name]") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 448 448
97  
98 # we first look in the BLOCKS hash for a BLOCK that may have
99 # been imported from a template (via PROCESS)
100 return $template
101 448 ▶ if ($template = $self->{ BLOCKS }->{ $name });
Branch: 1 branch
BranchTrueFalseTotal
if $template = $self->{"BLOCKS"}{$name} 195 253 448
102  
103 # then we iterate through the BLKSTACK list to see if any of the
104 # Template::Documents we're visiting define this BLOCK
105 253 foreach $blocks (@{ $self->{ BLKSTACK } }) {
106 return $template
107 145 ▶ if $blocks && ($template = $blocks->{ $name });
Branch: 1 branch
BranchTrueFalseTotal
if $blocks and $template = $blocks->{$name} 19 126 145
Condition66%2/3
$blocks and $template = $blocks->{$name}
!ll&&!rl&&r
012619
MC/DC50%1/2
$blocks and $template = $blocks->{$name}
$blocks $template = $blocks->{$name}
Truth table66%2/3
A and B
A= $blocksB= $template = $blocks->{$name}
ABresult
0X0
100
111
108 }
109  
110 # now it's time to ask the providers, so we look to see if any
111 # prefix is specified to indicate the desired provider set.
112 234 if (MSWin32) {
113 # let C:/foo through
114 $prefix = $1 if $shortname =~ s/^(\w{2,})://o;
115 }
116 else {
117 234 ▶ $prefix = $1 if $shortname =~ s/^(\w+)://;
Branch: 1 branch
BranchTrueFalseTotal
if $shortname =~ s/^(\w+):// 3 231 234
118 }
119  
120 234 ▶ if (defined $prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix) 3 231 234
121 3 ▶ $providers = $self->{ PREFIX_MAP }->{ $prefix }
Condition50%1/2
$self->{'PREFIX_MAP'}{$prefix} || (return $self->throw('file', "no providers for template prefix '${prefix}'"))
l!l
30
MC/DC0%0/1
$self->{'PREFIX_MAP'}{$prefix} || (return $self->throw('file', "no providers for template prefix '${prefix}'"))
$self->{'PREFIX_MAP'}{$prefix}
Truth table50%1/2
A
A= $self->{'PREFIX_MAP'}{$prefix}
Aresult
11
00
122 || return $self->throw( Template::Constants::ERROR_FILE,
123 "no providers for template prefix '$prefix'");
124 }
125 }
126 $providers = $self->{ PREFIX_MAP }->{ default }
127 || $self->{ LOAD_TEMPLATES }
128 1557 ▶ unless $providers;
Branch: 1 branch
BranchTrueFalseTotal
unless $providers 1554 3 1557
Condition33%1/3
$self->{'PREFIX_MAP'}{'default'} || $self->{'LOAD_TEMPLATES'}
l!l&&r!l&&!r
015540
MC/DC0%0/2
$self->{'PREFIX_MAP'}{'default'} || $self->{'LOAD_TEMPLATES'}
$self->{'PREFIX_MAP'}{'default'} $self->{'LOAD_TEMPLATES'}
Truth table33%1/3
A || B
A= $self->{'PREFIX_MAP'}{'default'}B= $self->{'LOAD_TEMPLATES'}
ABresult
1X1
011
000
129  
130  
131 # Finally we try the regular template providers which will
132 # handle references to files, text, etc., as well as templates
133 # reference by name. If
134  
135 1557 $blockname = '';
136 1557 ▶ while ($shortname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shortname) 1561 0 1561
137 $self->debug("asking providers for [$shortname] [$blockname]")
138 1561 ▶ if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 1561 1561
139  
140 1561 foreach my $provider (@$providers) {
141 1561 ($template, $error) = $provider->fetch($shortname, $prefix);
142 1561 ▶ if ($error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error) { } 49 1512 1561
elsif (length $blockname) { } 4 1508 1512
143 49 ▶ if ($error == Template::Constants::STATUS_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error == 255) 9 40 49
144 # $template contains exception object
145 9 ▶ if (blessed($template) && $template->isa(EXCEPTION)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($template) and $template->isa('Template::Exception') and $template->type eq 'file') { } 0 9 9
Condition66%4/6
builtin::blessed($template) and $template->isa("Template::Exception") and $template->type eq "file"
!ll&&!rl&&r
630
builtin::blessed($template) and $template->isa("Template::Exception")
!ll&&!rl&&r
603
MC/DC0%0/3
builtin::blessed($template) and $template->isa("Template::Exception") and $template->type eq "file"
builtin::blessed($template) $template->isa("Template::Exception") $template->type eq "file"
Truth table50%2/4
A and B and C
A= builtin::blessed($template)B= $template->isa("Template::Exception")C= $template->type eq "file"
ABCresult
0XX0
10X0
1100
1111
146 && $template->type eq Template::Constants::ERROR_FILE) {
147 0 $self->throw($template);
148 }
149 else {
150 9 $self->throw( Template::Constants::ERROR_FILE, $template );
151 }
152 }
153 # DECLINE is ok, carry on
154 }
155 elsif (length $blockname) {
156 return $template
157 4 ▶ if $template = $template->blocks->{ $blockname };
Branch: 1 branch
BranchTrueFalseTotal
if $template = $template->blocks->{$blockname} 3 1 4
158 }
159 else {
160 1508 return $template;
161 }
162 }
163  
164 41 ▶ last if ref $shortname || ! $self->{ EXPOSE_BLOCKS };
Branch: 1 branch
BranchTrueFalseTotal
if ref $shortname or not $self->{"EXPOSE_BLOCKS"} 36 5 41
Condition66%2/3
ref $shortname or not $self->{"EXPOSE_BLOCKS"}
l!l&&r!l&&!r
0365
MC/DC50%1/2
ref $shortname or not $self->{"EXPOSE_BLOCKS"}
ref $shortname not $self->{"EXPOSE_BLOCKS"}
Truth table66%2/3
A or not(B)
A= ref $shortnameB= not $self->{"EXPOSE_BLOCKS"}
ABresult
1X1
011
000
165 5 ▶ $shortname =~ s{/([^/]+)$}{} || last;
Branch: 1 branch
BranchTrueFalseTotal
$shortname =~ s[/([^/]+)$][] or last 1 4 5
166 4 ▶ $blockname = length $blockname ? "$1/$blockname" : $1;
Branch: 1 branch
BranchTrueFalseTotal
length $blockname ? : 0 4 4
167 }
168  
169 37 $self->throw(Template::Constants::ERROR_FILE, "$name: not found");
170 }
171  
172  
173 #------------------------------------------------------------------------
174 # plugin($name, \@args)
175 #
176 # Calls on each of the LOAD_PLUGINS providers in turn to fetch() (i.e. load
177 # and instantiate) a plugin of the specified name. Additional parameters
178 # passed are propagated to the new() constructor for the plugin.
179 # Returns a reference to a new plugin object or other reference. On
180 # error, undef is returned and the appropriate error message is set for
181 # subsequent retrieval via error().
182 #------------------------------------------------------------------------
183  
184 sub plugin {
185 238 ▶ my ($self, $name, $args) = @_;
Subroutine plugin: called CC=7 SCAR=19.7
Pod documented
186 238 my ($provider, $plugin, $error);
187  
188 $self->debug("plugin($name, ", defined $args ? @$args : '[ ]', ')')
189 238 ▶ if $self->{ DEBUG };
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 238 238
defined $args ? : 0 0 0
190  
191 # request the named plugin from each of the LOAD_PLUGINS providers in turn
192 238 foreach my $provider (@{ $self->{ LOAD_PLUGINS } }) {
193 238 ($plugin, $error) = $provider->fetch($name, $args, $self);
194 238 ▶ return $plugin unless $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 229 9 238
195 9 ▶ if ($error == Template::Constants::STATUS_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error == 255) 5 4 9
196 5 ▶ $self->throw($plugin) if ref $plugin;
Branch: 1 branch
BranchTrueFalseTotal
if ref $plugin 3 2 5
197 2 $self->throw(Template::Constants::ERROR_PLUGIN, $plugin);
198 }
199 }
200  
201 4 $self->throw(Template::Constants::ERROR_PLUGIN, "$name: plugin not found");
202 }
203  
204  
205 #------------------------------------------------------------------------
206 # filter($name, \@args, $alias)
207 #
208 # Similar to plugin() above, but querying the LOAD_FILTERS providers to
209 # return filter instances. An alias may be provided which is used to
210 # save the returned filter in a local cache.
211 #------------------------------------------------------------------------
212  
213 sub filter {
214 196 ▶ my ($self, $name, $args, $alias) = @_;
Subroutine filter: called CC=13 SCAR=25.9
Pod documented
215 196 my ($provider, $filter, $error);
216  
217 $self->debug("filter($name, ",
218 defined $args ? @$args : '[ ]',
219 defined $alias ? $alias : '<no alias>', ')')
220 196 ▶ if $self->{ DEBUG };
Branch: 3 branches
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 196 196
defined $args ? : 0 0 0
defined $alias ? : 0 0 0
221  
222 # use any cached version of the filter if no params provided
223 return $filter
224 if ! $args && ! ref $name
225 196 ▶ && ($filter = $self->{ FILTER_CACHE }->{ $name });
Branch: 1 branch
BranchTrueFalseTotal
if not $args || ref $name and $filter = $self->{"FILTER_CACHE"}{$name} 12 184 196
Condition100%6/6
not $args || ref $name and $filter = $self->{"FILTER_CACHE"}{$name}
!ll&&!rl&&r
998512
$args || ref $name
l!l&&r!l&&!r
95497
MC/DC100%3/3
not $args || ref $name and $filter = $self->{"FILTER_CACHE"}{$name}
$args ref $name $filter = $self->{"FILTER_CACHE"}{$name}
Truth table100%4/4
not(A || B) and C
A= $argsB= ref $nameC= $filter = $self->{"FILTER_CACHE"}{$name}
ABCresult
1XX0
01X0
0000
0011
226  
227 # request the named filter from each of the FILTERS providers in turn
228 184 foreach my $provider (@{ $self->{ LOAD_FILTERS } }) {
229 184 ($filter, $error) = $provider->fetch($name, $args, $self);
230 184 ▶ last unless $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 174 10 184
231 10 ▶ if ($error == Template::Constants::STATUS_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error == 255) 9 1 10
232 9 ▶ $self->throw($filter) if ref $filter;
Branch: 1 branch
BranchTrueFalseTotal
if ref $filter 4 5 9
233 5 $self->throw(Template::Constants::ERROR_FILTER, $filter);
234 }
235 # return $self->error($filter)
236 # if $error == &Template::Constants::STATUS_ERROR;
237 }
238  
239 175 ▶ return $self->error("$name: filter not found")
Branch: 1 branch
BranchTrueFalseTotal
unless $filter 1 174 175
240 unless $filter;
241  
242 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
243 # commented out by abw on 19 Nov 2001 to fix problem with xmlstyle
244 # plugin which may re-define a filter by calling define_filter()
245 # multiple times. With the automatic aliasing/caching below, any
246 # new filter definition isn't seen. Don't think this will cause
247 # any problems as filters explicitly supplied with aliases will
248 # still work as expected.
249 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
250 # alias defaults to name if undefined
251 # $alias = $name
252 # unless defined($alias) or ref($name) or $args;
253  
254 # cache FILTER if alias is valid
255 174 ▶ $self->{ FILTER_CACHE }->{ $alias } = $filter
Branch: 1 branch
BranchTrueFalseTotal
if $alias 9 165 174
256 if $alias;
257  
258 174 return $filter;
259 }
260  
261  
262 #------------------------------------------------------------------------
263 # view(\%config)
264 #
265 # Create a new Template::View bound to this context.
266 #------------------------------------------------------------------------
267  
268 sub view {
269 35 ▶ my $self = shift;
Subroutine view: called CC=2 SCAR=7.0
Pod documented
270 35 require Template::View;
271 35 ▶ return $VIEW_CLASS->new($self, @_)
Condition66%2/3
$Template::Context::VIEW_CLASS->new($self, @_) || $self->throw(&Template::Constants::ERROR_VIEW, $Template::Context::VIEW_CLASS->error)
l!l&&r!l&&!r
3401
MC/DC0%0/2
$Template::Context::VIEW_CLASS->new($self, @_) || $self->throw(&Template::Constants::ERROR_VIEW, $Template::Context::VIEW_CLASS->error)
$Template::Context::VIEW_CLASS->new($self, @_) $self->throw(&Template::Constants::ERROR_VIEW, $Template::Context::VIEW_CLASS->error)
Truth table33%1/3
A || B
A= $Template::Context::VIEW_CLASS->new($self, @_)B= $self->throw(&Template::Constants::ERROR_VIEW, $Template::Context::VIEW_CLASS->error)
ABresult
1X1
011
000
272 || $self->throw(
273 &Template::Constants::ERROR_VIEW,
274 $VIEW_CLASS->error
275 );
276 }
277  
278  
279 #------------------------------------------------------------------------
280 # process($template, \%params) [% PROCESS template var=val ... %]
281 # process($template, \%params, $local) [% INCLUDE template var=val ... %]
282 #
283 # Processes the template named or referenced by the first parameter.
284 # The optional second parameter may reference a hash array of variable
285 # definitions. These are set before the template is processed by
286 # calling update() on the stash. Note that, unless the third parameter
287 # is true, the context is not localised and these, and any other
288 # variables set in the template will retain their new values after this
289 # method returns. The third parameter is in place so that this method
290 # can handle INCLUDE calls: the stash will be localized.
291 #
292 # Returns the output of processing the template. Errors are thrown
293 # as Template::Exception objects via die().
294 #------------------------------------------------------------------------
295  
296 sub process {
297 1731 ▶ my ($self, $template, $params, $localize) = @_;
Subroutine process: called CC=26 SCAR=33.1
Pod documented
298 1731 my ($trim, $blocks) = @$self{ qw( TRIM BLOCKS ) };
299 1731 my (@compiled, $name, $compiled);
300 1731 my ($stash, $component, $tblocks, $error, $tmpout);
301 1731 my $output = '';
302  
303 1731 ▶ $template = [ $template ] unless ref $template eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $template eq "ARRAY" 1729 2 1731
304  
305 $self->debug("process([ ", join(', '), @$template, ' ], ',
306 defined $params ? $params : '<no params>', ', ',
307 $localize ? '<localized>' : '<unlocalized>', ')')
308 1731 ▶ if $self->{ DEBUG };
Branch: 3 branches
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 1731 1731
defined $params ? : 0 0 0
$localize ? : 0 0 0
309  
310 # fetch compiled template for each name specified
311 1731 foreach $name (@$template) {
312 1733 push(@compiled, $self->template($name));
313 }
314  
315 1709 ▶ if ($localize) {
Branch: 1 branch
BranchTrueFalseTotal
if ($localize) { } 268 1441 1709
316 # localise the variable stash with any parameters passed
317 268 $stash = $self->{ STASH } = $self->{ STASH }->clone($params);
318 } else {
319 # update stash with any new parameters passed
320 1441 $self->{ STASH }->update($params);
321 1441 $stash = $self->{ STASH };
322 }
323  
324 1709 eval {
325 # save current component
326 1709 eval { $component = $stash->get('component') };
327  
328 1709 foreach $name (@$template) {
329 1711 $compiled = shift @compiled;
330 1711 ▶ my $element = ref $compiled eq 'CODE'
Branch: 2 branches
BranchTrueFalseTotal
ref $compiled eq 'CODE' ? : 221 1490 1711
ref $name ? : 85 136 221
331 ? { (name => (ref $name ? '' : $name), modtime => time()) }
332 : $compiled;
333  
334 1711 ▶ if (blessed($component) && $component->isa(DOCUMENT)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($component) and $component->isa("Template::Document")) 294 1417 1711
Condition66%2/3
builtin::blessed($component) and $component->isa("Template::Document")
!ll&&!rl&&r
14170294
MC/DC50%1/2
builtin::blessed($component) and $component->isa("Template::Document")
builtin::blessed($component) $component->isa("Template::Document")
Truth table66%2/3
A and B
A= builtin::blessed($component)B= $component->isa("Template::Document")
ABresult
0X0
100
111
335 294 $element->{ caller } = $component->{ name };
336 294 ▶ $element->{ callers } = $component->{ callers } || [];
Condition100%2/2
$component->{'callers'} || []
l!l
17277
MC/DC100%1/1
$component->{'callers'} || []
$component->{'callers'}
Truth table100%2/2
A
A= $component->{'callers'}
Aresult
11
00
337 294 push(@{$element->{ callers }}, $element->{ caller });
338 }
339  
340 1711 $stash->set('component', $element);
341  
342 1711 ▶ unless ($localize) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($localize) 1442 269 1711
343 # merge any local blocks defined in the Template::Document
344 # into our local BLOCKS cache
345 1442 ▶ @$blocks{ keys %$tblocks } = values %$tblocks
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($compiled) and $compiled->isa("Template::Document") and $tblocks = $compiled->blocks 1422 20 1442
Condition66%4/6
builtin::blessed($compiled) and $compiled->isa("Template::Document") and $tblocks = $compiled->blocks
!ll&&!rl&&r
2001422
builtin::blessed($compiled) and $compiled->isa("Template::Document")
!ll&&!rl&&r
2001422
MC/DC33%1/3
builtin::blessed($compiled) and $compiled->isa("Template::Document") and $tblocks = $compiled->blocks
builtin::blessed($compiled) $compiled->isa("Template::Document") $tblocks = $compiled->blocks
Truth table50%2/4
A and B and C
A= builtin::blessed($compiled)B= $compiled->isa("Template::Document")C= $tblocks = $compiled->blocks
ABCresult
0XX0
10X0
1100
1111
346 if (blessed($compiled) && $compiled->isa(DOCUMENT))
347 && ($tblocks = $compiled->blocks);
348 }
349  
350 1711 ▶ if (ref $compiled eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $compiled eq 'CODE') { } 221 1490 1711
elsif (ref $compiled) { } 1490 0 1490
351 221 $tmpout = &$compiled($self);
352 }
353 elsif (ref $compiled) {
354 1490 $tmpout = $compiled->process($self);
355 }
356 else {
357 0 $self->throw(
358 'file',
359 "invalid template reference: $compiled"
360 );
361 }
362  
363 1684 ▶ if ($trim) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trim) 122 1562 1684
364 122 for ($tmpout) {
365 122 s/^\s+//;
366 122 s/\s+$//;
367 }
368 }
369 1684 $output .= $tmpout;
370  
371 # pop last item from callers.
372 # NOTE - this will not be called if template throws an
373 # error. The whole issue of caller and callers should be
374 # revisited to try and avoid putting this info directly into
375 # the component data structure. Perhaps use a local element
376 # instead?
377  
378 1684 ▶ pop(@{$element->{ callers }})
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($component) and $component->isa("Template::Document") 287 1397 1684
Condition66%2/3
builtin::blessed($component) and $component->isa("Template::Document")
!ll&&!rl&&r
13970287
MC/DC50%1/2
builtin::blessed($component) and $component->isa("Template::Document")
builtin::blessed($component) $component->isa("Template::Document")
Truth table66%2/3
A and B
A= builtin::blessed($component)B= $component->isa("Template::Document")
ABresult
0X0
100
111
379 if (blessed($component) && $component->isa(DOCUMENT));
380 }
381 1682 $stash->set('component', $component);
382 };
383 1709 $error = $@;
384  
385 1709 ▶ if ($localize) {
Branch: 1 branch
BranchTrueFalseTotal
if ($localize) 268 1441 1709
386 # ensure stash is delocalised before dying
387 268 $self->{ STASH } = $self->{ STASH }->declone();
388 }
389  
390 $self->throw(
391 1709 ▶ ref $error
Branch: 2 branches
BranchTrueFalseTotal
if $error 27 1682 1709
ref $error ? : 27 0 27
392 ? $error
393 : (Template::Constants::ERROR_FILE, $error)
394 ) if $error;
395  
396 1682 return $output;
397 }
398  
399  
400 #------------------------------------------------------------------------
401 # include($template, \%params) [% INCLUDE template var = val, ... %]
402 #
403 # Similar to process() above but processing the template in a local
404 # context. Any variables passed by reference to a hash as the second
405 # parameter will be set before the template is processed and then
406 # revert to their original values before the method returns. Similarly,
407 # any changes made to non-global variables within the template will
408 # persist only until the template is processed.
409 #
410 # Returns the output of processing the template. Errors are thrown
411 # as Template::Exception objects via die().
412 #------------------------------------------------------------------------
413  
414 sub include {
415 286 ▶ my ($self, $template, $params) = @_;
Subroutine include: called CC=1 SCAR=0.0
Pod documented
416 286 return $self->process($template, $params, 'localize me!');
417 }
418  
419 #------------------------------------------------------------------------
420 # insert($file)
421 #
422 # Insert the contents of a file without parsing.
423 #------------------------------------------------------------------------
424  
425 sub insert {
426 15 ▶ my ($self, $file) = @_;
Subroutine insert: called CC=12 SCAR=25.1
Pod documented
427 15 my ($prefix, $providers, $text, $error);
428 15 my $output = '';
429  
430 15 ▶ my $files = ref $file eq 'ARRAY' ? $file : [ $file ];
Branch: 1 branch
BranchTrueFalseTotal
ref $file eq 'ARRAY' ? : 2 13 15
431  
432 $self->debug("insert([ ", join(', '), @$files, " ])")
433 15 ▶ if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 15 15
434  
435  
436 15 FILE: foreach $file (@$files) {
437 17 my $name = $file;
438  
439 17 if (MSWin32) {
440 # let C:/foo through
441 $prefix = $1 if $name =~ s/^(\w{2,})://o;
442 }
443 else {
444 17 ▶ $prefix = $1 if $name =~ s/^(\w+)://;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ s/^(\w+):// 1 16 17
445 }
446  
447 17 ▶ if (defined $prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix) { } 1 16 17
448 1 ▶ $providers = $self->{ PREFIX_MAP }->{ $prefix }
Condition50%1/2
$self->{'PREFIX_MAP'}{$prefix} || (return $self->throw('file', "no providers for file prefix '${prefix}'"))
l!l
10
MC/DC0%0/1
$self->{'PREFIX_MAP'}{$prefix} || (return $self->throw('file', "no providers for file prefix '${prefix}'"))
$self->{'PREFIX_MAP'}{$prefix}
Truth table50%1/2
A
A= $self->{'PREFIX_MAP'}{$prefix}
Aresult
11
00
449 || return $self->throw(
450 Template::Constants::ERROR_FILE,
451 "no providers for file prefix '$prefix'"
452 );
453 }
454 else {
455 $providers = $self->{ PREFIX_MAP }->{ default }
456 16 ▶ || $self->{ LOAD_TEMPLATES };
Condition33%1/3
$self->{'PREFIX_MAP'}{'default'} || $self->{'LOAD_TEMPLATES'}
l!l&&r!l&&!r
0160
MC/DC0%0/2
$self->{'PREFIX_MAP'}{'default'} || $self->{'LOAD_TEMPLATES'}
$self->{'PREFIX_MAP'}{'default'} $self->{'LOAD_TEMPLATES'}
Truth table33%1/3
A || B
A= $self->{'PREFIX_MAP'}{'default'}B= $self->{'LOAD_TEMPLATES'}
ABresult
1X1
011
000
457 }
458  
459 17 foreach my $provider (@$providers) {
460 17 ($text, $error) = $provider->load($name, $prefix);
461 17 ▶ next FILE unless $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 12 5 17
462 5 ▶ if ($error == Template::Constants::STATUS_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error == 255) 2 3 5
463 2 ▶ $self->throw($text) if ref $text;
Branch: 1 branch
BranchTrueFalseTotal
if ref $text 0 2 2
464 2 $self->throw(Template::Constants::ERROR_FILE, $text);
465 }
466 }
467 3 $self->throw(Template::Constants::ERROR_FILE, "$file: not found");
468 }
469 continue {
470 12 $output .= $text;
471 }
472 10 return $output;
473 }
474  
475  
476 #------------------------------------------------------------------------
477 # throw($type, $info, \$output) [% THROW errtype "Error info" %]
478 #
479 # Throws a Template::Exception object by calling die(). This method
480 # may be passed a reference to an existing Template::Exception object;
481 # a single value containing an error message which is used to
482 # instantiate a Template::Exception of type 'undef'; or a pair of
483 # values representing the exception type and info from which a
484 # Template::Exception object is instantiated. e.g.
485 #
486 # $context->throw($exception);
487 # $context->throw("I'm sorry Dave, I can't do that");
488 # $context->throw('denied', "I'm sorry Dave, I can't do that");
489 #
490 # An optional third parameter can be supplied in the last case which
491 # is a reference to the current output buffer containing the results
492 # of processing the template up to the point at which the exception
493 # was thrown. The RETURN and STOP directives, for example, use this
494 # to propagate output back to the user, but it can safely be ignored
495 # in most cases.
496 #
497 # This method rides on a one-way ticket to die() oblivion. It does not
498 # return in any real sense of the word, but should get caught by a
499 # surrounding eval { } block (e.g. a BLOCK or TRY) and handled
500 # accordingly, or returned to the caller as an uncaught exception.
501 #------------------------------------------------------------------------
502  
503 sub throw {
504 153 ▶ my ($self, $error, $info, $output) = @_;
Subroutine throw: called CC=7 SCAR=20.8
Pod documented
505 153 local $" = ', ';
506  
507 # die! die! die!
508 153 ▶ if (blessed($error) && $error->isa(EXCEPTION)) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($error) and $error->isa('Template::Exception')) { } 35 118 153
elsif (builtin::blessed($error) and $error->isa('Badger::Exception')) { } 0 118 118
elsif (defined $info) { } 113 5 118
Condition66%2/3
builtin::blessed($error) and $error->isa("Template::Exception")
!ll&&!rl&&r
118035
MC/DC50%1/2
builtin::blessed($error) and $error->isa("Template::Exception")
builtin::blessed($error) $error->isa("Template::Exception")
Truth table66%2/3
A and B
A= builtin::blessed($error)B= $error->isa("Template::Exception")
ABresult
0X0
100
111
509 35 die $error;
510 }
511 ▶ elsif (blessed($error) && $error->isa(BADGER_EXCEPTION)) {
Condition33%1/3
builtin::blessed($error) and $error->isa("Badger::Exception")
!ll&&!rl&&r
11800
MC/DC0%0/2
builtin::blessed($error) and $error->isa("Badger::Exception")
builtin::blessed($error) $error->isa("Badger::Exception")
Truth table33%1/3
A and B
A= builtin::blessed($error)B= $error->isa("Badger::Exception")
ABresult
0X0
100
111
512 # convert a Badger::Exception to a Template::Exception so that
513 # things continue to work during the transition to Badger
514 0 die EXCEPTION->new($error->type, $error->info);
515 }
516 elsif (defined $info) {
517 113 die (EXCEPTION->new($error, $info, $output));
518 }
519 else {
520 5 ▶ $error ||= '';
Condition50%1/2
$error ||= ""
l!l
50
MC/DC0%0/1
$error ||= ""
$error
Truth table50%1/2
A
A= $error
Aresult
11
00
521 5 die (EXCEPTION->new('undef', $error, $output));
522 }
523  
524 # not reached
525 }
526  
527  
528 #------------------------------------------------------------------------
529 # catch($error, \$output)
530 #
531 # Called by various directives after catching an error thrown via die()
532 # from within an eval { } block. The first parameter contains the error
533 # which may be a sanitized reference to a Template::Exception object
534 # (such as that raised by the throw() method above, a plugin object,
535 # and so on) or an error message thrown via die from somewhere in user
536 # code. The latter are coerced into 'undef' Template::Exception objects.
537 # Like throw() above, a reference to a scalar may be passed as an
538 # additional parameter to represent the current output buffer
539 # localised within the eval block. As exceptions are thrown upwards
540 # and outwards from nested blocks, the catch() method reconstructs the
541 # correct output buffer from these fragments, storing it in the
542 # exception object for passing further onwards and upwards.
543 #
544 # Returns a reference to a Template::Exception object..
545 #------------------------------------------------------------------------
546  
547 sub catch {
548 172 ▶ my ($self, $error, $output) = @_;
Subroutine catch: called CC=5 SCAR=16.1
Pod documented
549  
550 172 ▶ if ( blessed($error)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($error) and $error->isa('Template::Exception') || $error->isa('Badger::Exception')) { } 157 15 172
Condition83%5/6
builtin::blessed($error) and $error->isa("Template::Exception") || $error->isa("Badger::Exception")
!ll&&!rl&&r
141157
$error->isa("Template::Exception") or $error->isa("Badger::Exception")
l!l&&r!l&&!r
15701
MC/DC66%2/3
builtin::blessed($error) and $error->isa("Template::Exception") || $error->isa("Badger::Exception")
builtin::blessed($error) $error->isa("Template::Exception") $error->isa("Badger::Exception")
Truth table75%3/4
A and B or C
A= builtin::blessed($error)B= $error->isa("Template::Exception")C= $error->isa("Badger::Exception")
ABCresult
0XX0
1000
11X1
1011
551 && ( $error->isa(EXCEPTION) || $error->isa(BADGER_EXCEPTION) ) ) {
552 157 ▶ $error->text($output) if $output;
Branch: 1 branch
BranchTrueFalseTotal
if $output 134 23 157
553 157 return $error;
554 }
555 else {
556 15 return EXCEPTION->new('undef', $error, $output);
557 }
558 }
559  
560  
561 #------------------------------------------------------------------------
562 # localise(\%params)
563 # delocalise()
564 #
565 # The localise() method creates a local copy of the current stash,
566 # allowing the existing state of variables to be saved and later
567 # restored via delocalise().
568 #
569 # A reference to a hash array may be passed containing local variable
570 # definitions which should be added to the cloned namespace. These
571 # values persist until delocalisation.
572 #------------------------------------------------------------------------
573  
574 sub localise {
575 1369 ▶ my $self = shift;
Subroutine localise: called CC=1 SCAR=0.0
Pod documented
576 1369 $self->{ STASH } = $self->{ STASH }->clone(@_);
577 }
578  
579 sub delocalise {
580 1369 ▶ my $self = shift;
Subroutine delocalise: called CC=1 SCAR=0.0
Pod documented
581 1369 $self->{ STASH } = $self->{ STASH }->declone();
582 }
583  
584  
585 #------------------------------------------------------------------------
586 # visit($document, $blocks)
587 #
588 # Each Template::Document calls the visit() method on the context
589 # before processing itself. It passes a reference to the hash array
590 # of named BLOCKs defined within the document, allowing them to be
591 # added to the internal BLKSTACK list which is subsequently used by
592 # template() to resolve templates.
593 # from a provider.
594 #------------------------------------------------------------------------
595  
596 sub visit {
597 1496 ▶ my ($self, $document, $blocks) = @_;
Subroutine visit: called CC=1 SCAR=0.0
Pod documented
598 1496 unshift(@{ $self->{ BLKSTACK } }, $blocks)
599 }
600  
601  
602 #------------------------------------------------------------------------
603 # leave()
604 #
605 # The leave() method is called when the document has finished
606 # processing itself. This removes the entry from the BLKSTACK list
607 # that was added visit() above. For persistence of BLOCK definitions,
608 # the process() method (i.e. the PROCESS directive) does some extra
609 # magic to copy BLOCKs into a shared hash.
610 #------------------------------------------------------------------------
611  
612 sub leave {
613 1493 ▶ my $self = shift;
Subroutine leave: called CC=1 SCAR=0.0
Pod documented
614 1493 shift(@{ $self->{ BLKSTACK } });
615 }
616  
617  
618 #------------------------------------------------------------------------
619 # define_block($name, $block)
620 #
621 # Adds a new BLOCK definition to the local BLOCKS cache. $block may
622 # be specified as a reference to a sub-routine or Template::Document
623 # object or as text which is compiled into a template. Returns a true
624 # value (the $block reference or compiled block reference) if
625 # successful or undef on failure. Call error() to retrieve the
626 # relevant error message (i.e. compilation failure).
627 #------------------------------------------------------------------------
628  
629 sub define_block {
630 2 ▶ my ($self, $name, $block) = @_;
Subroutine define_block: called CC=3 SCAR=11.1
Pod documented
631 2 ▶ $block = $self->template(\$block)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $block 1 1 2
Condition50%1/2
$self->template(\$block) || (return undef)
l!l
10
MC/DC0%0/1
$self->template(\$block) || (return undef)
$self->template(\$block)
Truth table50%1/2
A
A= $self->template(\$block)
Aresult
11
00
632 || return undef
633 unless ref $block;
634 2 $self->{ BLOCKS }->{ $name } = $block;
635 }
636  
637  
638 #------------------------------------------------------------------------
639 # define_filter($name, $filter, $is_dynamic)
640 #
641 # Adds a new FILTER definition to the local FILTER_CACHE.
642 #------------------------------------------------------------------------
643  
644 sub define_filter {
645 14 ▶ my ($self, $name, $filter, $is_dynamic) = @_;
Subroutine define_filter: called CC=5 SCAR=17.8
Pod documented
646 14 my ($result, $error);
647 14 ▶ $filter = [ $filter, 1 ] if $is_dynamic;
Branch: 1 branch
BranchTrueFalseTotal
if $is_dynamic 2 12 14
648  
649 14 foreach my $provider (@{ $self->{ LOAD_FILTERS } }) {
650 14 ($result, $error) = $provider->store($name, $filter);
651 14 ▶ return 1 unless $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 14 0 14
652 0 ▶ $self->throw(&Template::Constants::ERROR_FILTER, $result)
Branch: 1 branch
BranchTrueFalseTotal
if $error == &Template::Constants::STATUS_ERROR 0 0 0
653 if $error == &Template::Constants::STATUS_ERROR;
654 }
655 $self->throw(
656 0 &Template::Constants::ERROR_FILTER,
657 "FILTER providers declined to store filter $name"
658 );
659 }
660  
661  
662 #------------------------------------------------------------------------
663 # define_vmethod($type, $name, \&sub)
664 #
665 # Passes $type, $name, and &sub on to stash->define_vmethod().
666 #------------------------------------------------------------------------
667  
668 sub define_vmethod {
669 9 ▶ my $self = shift;
Subroutine define_vmethod: called CC=1 SCAR=0.0
Pod documented
670 9 $self->stash->define_vmethod(@_);
671 }
672  
673  
674 #------------------------------------------------------------------------
675 # define_view($name, $params)
676 #
677 # Defines a new view.
678 #------------------------------------------------------------------------
679  
680 sub define_view {
681 2 ▶ my ($self, $name, $params) = @_;
Subroutine define_view: called CC=5 SCAR=16.6
Pod documented
682 2 my $base;
683  
684 2 ▶ if (defined $params->{ base }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{"base"}) 1 1 2
685 1 my $base = $self->{ STASH }->get($params->{ base });
686  
687 1 ▶ return $self->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $base 0 1 1
688 &Template::Constants::ERROR_VIEW,
689 "view base is not defined: $params->{ base }"
690 ) unless $base;
691  
692 1 ▶ return $self->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($base) and $base->isa($Template::Context::VIEW_CLASS) 0 1 1
Condition33%1/3
builtin::blessed($base) and $base->isa($Template::Context::VIEW_CLASS)
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($base) and $base->isa($Template::Context::VIEW_CLASS)
builtin::blessed($base) $base->isa($Template::Context::VIEW_CLASS)
Truth table33%1/3
A and B
A= builtin::blessed($base)B= $base->isa($Template::Context::VIEW_CLASS)
ABresult
0X0
100
111
693 &Template::Constants::ERROR_VIEW,
694 "view base is not a $VIEW_CLASS object: $params->{ base } => $base"
695 ) unless blessed($base) && $base->isa($VIEW_CLASS);
696  
697 1 $params->{ base } = $base;
698 }
699 2 my $view = $self->view($params);
700 2 $view->seal();
701 2 $self->{ STASH }->set($name, $view);
702 }
703  
704  
705 #------------------------------------------------------------------------
706 # define_views($views)
707 #
708 # Defines multiple new views.
709 #------------------------------------------------------------------------
710  
711 sub define_views {
712 1 ▶ my ($self, $views) = @_;
Subroutine define_views: called CC=3 SCAR=11.0
Pod documented
713  
714 # a list reference is better because the order is deterministic (and so
715 # allows an earlier VIEW to be the base for a later VIEW), but we'll
716 # accept a hash reference and assume that the user knows the order of
717 # processing is undefined
718 1 ▶ $views = [ %$views ]
Branch: 1 branch
BranchTrueFalseTotal
if ref $views eq "HASH" 0 1 1
719 if ref $views eq 'HASH';
720  
721 # make of copy so we don't destroy the original list reference
722 1 my @items = @$views;
723 1 my ($name, $view);
724  
725 1 ▶ while (@items) {
Branch: 1 branch
BranchTrueFalseTotal
if (@items) 2 1 3
726 2 $self->define_view(splice(@items, 0, 2));
727 }
728 }
729  
730  
731 #------------------------------------------------------------------------
732 # reset()
733 #
734 # Reset the state of the internal BLOCKS hash to clear any BLOCK
735 # definitions imported via the PROCESS directive. Any original
736 # BLOCKS definitions passed to the constructor will be restored.
737 #------------------------------------------------------------------------
738  
739 sub reset {
740 1325 ▶ my ($self, $blocks) = @_;
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
741 1325 $self->{ BLKSTACK } = [ ];
742 1325 $self->{ BLOCKS } = { %{ $self->{ INIT_BLOCKS } } };
743 }
744  
745  
746 #------------------------------------------------------------------------
747 # stash()
748 #
749 # Simple accessor methods to return the STASH values. This is likely
750 # to be called quite often so we provide a direct method rather than
751 # relying on the slower AUTOLOAD.
752 #------------------------------------------------------------------------
753  
754 sub stash {
755 1722 ▶ return $_[0]->{ STASH };
Subroutine stash: called CC=1 SCAR=0.0
Pod documented
756 }
757  
758  
759 #------------------------------------------------------------------------
760 # debugging($command, @args, \%params)
761 #
762 # Method for controlling the debugging status of the context. The first
763 # argument can be 'on' or 'off' to enable/disable debugging, 'format'
764 # to define the format of the debug message, or 'msg' to generate a
765 # debugging message reporting the file, line, message text, etc.,
766 # according to the current debug format.
767 #------------------------------------------------------------------------
768  
769 sub debugging {
770 20 ▶ my $self = shift;
Subroutine debugging: called CC=12 SCAR=24.9
Pod documented
771 20 ▶ my $hash = ref $_[-1] eq 'HASH' ? pop : { };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 9 11 20
772 20 my @args = @_;
773  
774 20 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 20 0 20
775 20 ▶ if ($args[0] eq '1' || lc($args[0]) eq 'on' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args[0] eq '1' or lc $args[0] eq 'on') { } 5 15 20
elsif ($args[0] eq '0' or lc $args[0] eq 'off') { } 5 10 15
Condition100%3/3
$args[0] eq 1 or lc $args[0] eq "on"
l!l&&r!l&&!r
1415
MC/DC100%2/2
$args[0] eq 1 or lc $args[0] eq "on"
$args[0] eq 1 lc $args[0] eq "on"
Truth table100%3/3
A or B
A= $args[0] eq 1B= lc $args[0] eq "on"
ABresult
1X1
011
000
776 5 $self->{ DEBUG_DIRS } = 1;
777 5 shift(@args);
778 }
779 ▶ elsif ($args[0] eq '0' || lc($args[0]) eq 'off') {
Condition100%3/3
$args[0] eq 0 or lc $args[0] eq "off"
l!l&&r!l&&!r
1410
MC/DC100%2/2
$args[0] eq 0 or lc $args[0] eq "off"
$args[0] eq 0 lc $args[0] eq "off"
Truth table100%3/3
A or B
A= $args[0] eq 0B= lc $args[0] eq "off"
ABresult
1X1
011
000
780 5 $self->{ DEBUG_DIRS } = 0;
781 5 shift(@args);
782 }
783 }
784  
785 20 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 10 10 20
786 10 ▶ if (lc($args[0]) eq 'msg') {
Branch: 2 branches
BranchTrueFalseTotal
if (lc $args[0] eq 'msg') { } 9 1 10
elsif (lc $args[0] eq 'format') { } 1 0 1
787 9 ▶ return unless $self->{ DEBUG_DIRS };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"DEBUG_DIRS"} 0 9 9
788 9 my $format = $self->{ DEBUG_FORMAT };
789 9 ▶ $format = $DEBUG_FORMAT unless defined $format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 0 9 9
790 9 $format =~ s/\$(\w+)/$hash->{ $1 }/ge;
791 9 return $format;
792 }
793 elsif ( lc($args[0]) eq 'format' ) {
794 1 $self->{ DEBUG_FORMAT } = $args[1];
795 }
796 # else ignore
797 }
798  
799 11 return '';
800 }
801  
802  
803 #------------------------------------------------------------------------
804 # AUTOLOAD
805 #
806 # Provides pseudo-methods for read-only access to various internal
807 # members. For example, templates(), plugins(), filters(),
808 # eval_perl(), load_perl(), etc. These aren't called very often, or
809 # may never be called at all.
810 #------------------------------------------------------------------------
811  
812 sub AUTOLOAD {
813 26 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=3 SCAR=11.2
814 26 my $method = $AUTOLOAD;
815 26 my $result;
816  
817 26 $method =~ s/.*:://;
818 26 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 0 26 26
819  
820 warn "no such context method/member: $method\n"
821 26 ▶ unless defined ($result = $self->{ uc $method });
Branch: 1 branch
BranchTrueFalseTotal
unless defined($result = $$self{uc $method}) 0 26 26
822  
823 26 return $result;
824 }
825  
826  
827 #------------------------------------------------------------------------
828 # DESTROY
829 #
830 # Stash may contain references back to the Context via macro closures,
831 # etc. This breaks the circular references.
832 #------------------------------------------------------------------------
833  
834 sub DESTROY {
835 244 ▶ my $self = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
836 244 undef $self->{ STASH };
837 }
838  
839  
840  
841 #========================================================================
842 # -- PRIVATE METHODS --
843 #========================================================================
844  
845 #------------------------------------------------------------------------
846 # _init(\%config)
847 #
848 # Initialisation method called by Template::Base::new()
849 #------------------------------------------------------------------------
850  
851 sub _init {
852 248 ▶ my ($self, $config) = @_;
Subroutine _init: called CC=26 SCAR=33.1
853 248 my ($name, $item, $method, $block, $blocks);
854 248 my @itemlut = (
855 LOAD_TEMPLATES => 'provider',
856 LOAD_PLUGINS => 'plugins',
857 LOAD_FILTERS => 'filters'
858 );
859  
860 # LOAD_TEMPLATE, LOAD_PLUGINS, LOAD_FILTERS - lists of providers
861 248 ▶ while (($name, $method) = splice(@itemlut, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($name, $method) = splice(@itemlut, 0, 2)) 744 248 992
862 744 ▶ $item = $config->{ $name }
Condition60%3/5
$config->{$name} || 'Template::Config'->$method($config) || (return $self->error($Template::Config::ERROR))
l!l
7440
$config->{$name} or "Template::Config"->$method($config)
l!l&&r!l&&!r
77370
MC/DC0%0/2
$config->{$name} || 'Template::Config'->$method($config) || (return $self->error($Template::Config::ERROR))
$config->{$name} "Template::Config"->$method($config)
Truth table66%2/3
A or B
A= $config->{$name}B= "Template::Config"->$method($config)
ABresult
1X1
011
000
863 || Template::Config->$method($config)
864 || return $self->error($Template::Config::ERROR);
865 744 ▶ $self->{ $name } = ref $item eq 'ARRAY' ? $item : [ $item ];
Branch: 1 branch
BranchTrueFalseTotal
ref $item eq 'ARRAY' ? : 7 737 744
866 }
867  
868 248 my $providers = $self->{ LOAD_TEMPLATES };
869 248 ▶ my $prefix_map = $self->{ PREFIX_MAP } = $config->{ PREFIX_MAP } || { };
Condition100%2/2
$config->{'PREFIX_MAP'} || {}
l!l
1247
MC/DC100%1/1
$config->{'PREFIX_MAP'} || {}
$config->{'PREFIX_MAP'}
Truth table100%2/2
A
A= $config->{'PREFIX_MAP'}
Aresult
11
00
870 248 ▶ while (my ($key, $val) = each %$prefix_map) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$prefix_map) 3 248 251
871 $prefix_map->{ $key } = [
872 ref $val
873 ? $val
874 3 ▶ : map { $providers->[$_] } split(/\D+/, $val)
Branch: 2 branches
BranchTrueFalseTotal
unless ref $val eq "ARRAY" 3 0 3
ref $val ? : 0 3 3
875 ]
876 unless ref $val eq 'ARRAY';
877 }
878  
879 # STASH
880 248 ▶ $self->{ STASH } = $config->{ STASH } || do {
Condition66%2/3
$config->{'STASH'} || do { my $predefs = $config->{'VARIABLES'} || $config->{'PRE_DEFINE'} || {}; $predefs->{'_DEBUG'} = ($config->{'DEBUG'} || 0) & &Template::Constants::DEBUG_UNDEF ? 1 : 0 unless defined $predefs->{'_DEBUG'}; $predefs->{'_STRICT'} = $config->{'STRICT'}; 'Template::Config'->stash($predefs) or return $self->error($Template::Config::ERROR) }
l!l&&r!l&&!r
62420
MC/DC0%0/2
$config->{'STASH'} || do { my $predefs = $config->{'VARIABLES'} || $config->{'PRE_DEFINE'} || {}; $predefs->{'_DEBUG'} = ($config->{'DEBUG'} || 0) & &Template::Constants::DEBUG_UNDEF ? 1 : 0 unless defined $predefs->{'_DEBUG'}; $predefs->{'_STRICT'} = $config->{'STRICT'}; 'Template::Config'->stash($predefs) or return $self->error($Template::Config::ERROR) }
$config->{'STASH'} do { my $predefs = $config->{'VARIABLES'} || $config->{'PRE_DEFINE'} || {}; $predefs->{'_DEBUG'} = ($config->{'DEBUG'} || 0) & &Template::Constants::DEBUG_UNDEF ? 1 : 0 unless defined $predefs->{'_DEBUG'}; $predefs->{'_STRICT'} = $config->{'STRICT'}; 'Template::Config'->stash($predefs) or return $self->error($Template::Config::ERROR) }
Truth table66%2/3
A || B
A= $config->{'STASH'}B= do { my $predefs = $config->{'VARIABLES'} || $config->{'PRE_DEFINE'} || {}; $predefs->{'_DEBUG'} = ($config->{'DEBUG'} || 0) & &Template::Constants::DEBUG_UNDEF ? 1 : 0 unless defined $predefs->{'_DEBUG'}; $predefs->{'_STRICT'} = $config->{'STRICT'}; 'Template::Config'->stash($predefs) or return $self->error($Template::Config::ERROR) }
ABresult
1X1
011
000
881 my $predefs = $config->{ VARIABLES }
882 || $config->{ PRE_DEFINE }
883 242 ▶ || { };
Condition40%2/5
$config->{'VARIABLES'} || $config->{'PRE_DEFINE'} || {}
l!l
0242
$config->{"VARIABLES"} or $config->{"PRE_DEFINE"}
l!l&&r!l&&!r
00242
MC/DC0%0/2
$config->{'VARIABLES'} || $config->{'PRE_DEFINE'} || {}
$config->{"VARIABLES"} $config->{"PRE_DEFINE"}
Truth table33%1/3
A or B
A= $config->{"VARIABLES"}B= $config->{"PRE_DEFINE"}
ABresult
1X1
011
000
884  
885 # hack to get stash to know about debug mode
886 $predefs->{ _DEBUG } = (
887 ($config->{ DEBUG } || 0) & &Template::Constants::DEBUG_UNDEF
888 ) ? 1 : 0
889 242 ▶ unless defined $predefs->{ _DEBUG };
Branch: 2 branches
BranchTrueFalseTotal
unless defined $predefs->{"_DEBUG"} 242 0 242
($config->{'DEBUG'} || 0) & &Template::Constants::DEBUG_UNDEF ? : 1 241 242
Condition100%2/2
$config->{'DEBUG'} || 0
l!l
8234
MC/DC100%1/1
$config->{'DEBUG'} || 0
$config->{'DEBUG'}
Truth table100%2/2
A
A= $config->{'DEBUG'}
Aresult
11
00
890 242 $predefs->{ _STRICT } = $config->{ STRICT };
891  
892 242 ▶ Template::Config->stash($predefs)
Branch: 1 branch
BranchTrueFalseTotal
"Template::Config"->stash($predefs) or return $self->error($Template::Config::ERROR) 0 242 242
893 || return $self->error($Template::Config::ERROR);
894 };
895  
896 # compile any template BLOCKS specified as text
897 248 ▶ $blocks = $config->{ BLOCKS } || { };
Condition100%2/2
$config->{'BLOCKS'} || {}
l!l
14234
MC/DC100%1/1
$config->{'BLOCKS'} || {}
$config->{'BLOCKS'}
Truth table100%2/2
A
A= $config->{'BLOCKS'}
Aresult
11
00
898 $self->{ INIT_BLOCKS } = $self->{ BLOCKS } = {
899 map {
900 248 $block = $blocks->{ $_ };
901 53 ▶ $block = $self->template(\$block)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $block 40 13 53
Condition50%1/2
$self->template(\$block) || (return undef)
l!l
400
MC/DC0%0/1
$self->template(\$block) || (return undef)
$self->template(\$block)
Truth table50%1/2
A
A= $self->template(\$block)
Aresult
11
00
902 || return undef
903 unless ref $block;
904 53 ($_ => $block);
905 }
906 keys %$blocks
907 };
908  
909 # define any VIEWS
910 $self->define_views( $config->{ VIEWS } )
911 248 ▶ if $config->{ VIEWS };
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"VIEWS"} 1 247 248
912  
913 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
914 # RECURSION - flag indicating is recursion into templates is supported
915 # EVAL_PERL - flag indicating if PERL blocks should be processed
916 # TRIM - flag to remove leading and trailing whitespace from output
917 # BLKSTACK - list of hashes of BLOCKs defined in current template(s)
918 # CONFIG - original configuration hash
919 # EXPOSE_BLOCKS - make blocks visible as pseudo-files
920 # DEBUG_FORMAT - format for generating template runtime debugging messages
921 # DEBUG - format for generating template runtime debugging messages
922  
923 248 ▶ $self->{ RECURSION } = $config->{ RECURSION } || 0;
Condition100%2/2
$config->{'RECURSION'} || 0
l!l
1247
MC/DC100%1/1
$config->{'RECURSION'} || 0
$config->{'RECURSION'}
Truth table100%2/2
A
A= $config->{'RECURSION'}
Aresult
11
00
924 248 ▶ $self->{ EVAL_PERL } = $config->{ EVAL_PERL } || 0;
Condition100%2/2
$config->{'EVAL_PERL'} || 0
l!l
9239
MC/DC100%1/1
$config->{'EVAL_PERL'} || 0
$config->{'EVAL_PERL'}
Truth table100%2/2
A
A= $config->{'EVAL_PERL'}
Aresult
11
00
925 248 ▶ $self->{ TRIM } = $config->{ TRIM } || 0;
Condition100%2/2
$config->{'TRIM'} || 0
l!l
10238
MC/DC100%1/1
$config->{'TRIM'} || 0
$config->{'TRIM'}
Truth table100%2/2
A
A= $config->{'TRIM'}
Aresult
11
00
926 248 $self->{ BLKSTACK } = [ ];
927 248 $self->{ CONFIG } = $config;
928 $self->{ EXPOSE_BLOCKS } = defined $config->{ EXPOSE_BLOCKS }
929 ? $config->{ EXPOSE_BLOCKS }
930 248 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $config->{'EXPOSE_BLOCKS'} ? : 1 247 248
931  
932 248 $self->{ DEBUG_FORMAT } = $config->{ DEBUG_FORMAT };
933 248 ▶ $self->{ DEBUG_DIRS } = ($config->{ DEBUG } || 0)
Condition100%2/2
$config->{'DEBUG'} || 0
l!l
8240
MC/DC100%1/1
$config->{'DEBUG'} || 0
$config->{'DEBUG'}
Truth table100%2/2
A
A= $config->{'DEBUG'}
Aresult
11
00
934 & Template::Constants::DEBUG_DIRS;
935 $self->{ DEBUG } = defined $config->{ DEBUG }
936 ? $config->{ DEBUG }
937 248 ▶ & ( Template::Constants::DEBUG_CONTEXT
Branch: 1 branch
BranchTrueFalseTotal
defined $config->{'DEBUG'} ? : 21 227 248
938 | Template::Constants::DEBUG_FLAGS )
939 : $DEBUG;
940  
941 248 return $self;
942 }
943  
944 102 1;
945  
946 __END__
« lib/Template/Parser.pm ↑ Summary blib/lib/Template/VMethods.pm »