lib/Template/Context.pm

filter: Statementstmt 98.2% 282 / 287 Branchbran 80.9% 144 / 178 Conditioncond 66.9% 79 / 118 MC/DCmcdc 39.0% 25 / 64 Subroutinesub 100% 36 / 36 Podpod 100% 21 / 21 totaltotal 83.3% 587 / 704 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 lib/Template/Service.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 106 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 106 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 106 ▶use base 'Template::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 106 ▶use Template::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 106 ▶use Template::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 106 ▶use Template::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 106 ▶use Template::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 106 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 106 ▶use constant DOCUMENT => 'Template::Document';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 106 ▶use constant EXCEPTION => 'Template::Exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 106 ▶use constant BADGER_EXCEPTION => 'Badger::Exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 106 ▶use constant MSWin32 => $^O eq 'MSWin32';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 106 our $VERSION = '3.106';
39 106 ▶our $DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::Context::DEBUG 106 0 106
40 106 our $DEBUG_FORMAT = "\n## \$file line \$line : [% \$text %] ##\n";
41 106 our $VIEW_CLASS = 'Template::View';
42 106 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 3325 ▶sub template {
Subroutine template: called CC=30 SCAR=35.2
Pod documented
79 3325 my ($self, $name) = @_;
80 3325 my ($prefix, $blocks, $defblocks, $provider, $template, $error);
81 3325 my ($shortname, $blockname, $providers);
82  
83 3325 ▶ $self->debug("template($name)") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 3325 3325
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 3325 ▶ return $name
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($name) and $name->isa("Template::Document") or ref $name eq "CODE" 1494 1831 3325
Condition83%5/6
builtin::blessed($name) and $name->isa("Template::Document") or ref $name eq "CODE"
l!l&&r!l&&!r
1408861831
builtin::blessed($name) and $name->isa("Template::Document")
!ll&&!rl&&r
191701408
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 1831 $shortname = $name;
93  
94 1831 ▶ unless (ref $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $name) 449 1382 1831
95  
96 449 ▶ $self->debug("looking for block [$name]") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 449 449
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 449 ▶ if ($template = $self->{ BLOCKS }->{ $name });
Branch: 1 branch
BranchTrueFalseTotal
if $template = $self->{"BLOCKS"}{$name} 195 254 449
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 254 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 235 if (MSWin32) {
113 # let C:/foo through
114 $prefix = $1 if $shortname =~ s/^(\w{2,})://o;
115 }
116 else {
117 235 ▶ $prefix = $1 if $shortname =~ s/^(\w+)://;
Branch: 1 branch
BranchTrueFalseTotal
if $shortname =~ s/^(\w+):// 3 232 235
118 }
119  
120 235 ▶ if (defined $prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix) 3 232 235
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 1617 ▶ unless $providers;
Branch: 1 branch
BranchTrueFalseTotal
unless $providers 1614 3 1617
Condition33%1/3
$self->{'PREFIX_MAP'}{'default'} || $self->{'LOAD_TEMPLATES'}
l!l&&r!l&&!r
016140
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 1617 $blockname = '';
136 1617 ▶ while ($shortname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shortname) 1621 0 1621
137 $self->debug("asking providers for [$shortname] [$blockname]")
138 1621 ▶ if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 1621 1621
139  
140 1621 foreach my $provider (@$providers) {
141 1621 ($template, $error) = $provider->fetch($shortname, $prefix);
142 1621 ▶ if ($error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error) { } 49 1572 1621
elsif (length $blockname) { } 4 1568 1572
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 1568 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 4 1 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 259 ▶sub plugin {
Subroutine plugin: called CC=7 SCAR=19.7
Pod documented
185 259 my ($self, $name, $args) = @_;
186 259 my ($provider, $plugin, $error);
187  
188 $self->debug("plugin($name, ", defined $args ? @$args : '[ ]', ')')
189 259 ▶ if $self->{ DEBUG };
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 259 259
defined $args ? : 0 0 0
190  
191 # request the named plugin from each of the LOAD_PLUGINS providers in turn
192 259 foreach my $provider (@{ $self->{ LOAD_PLUGINS } }) {
193 259 ($plugin, $error) = $provider->fetch($name, $args, $self);
194 259 ▶ return $plugin unless $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 248 11 259
195 11 ▶ if ($error == Template::Constants::STATUS_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error == 255) 7 4 11
196 7 ▶ $self->throw($plugin) if ref $plugin;
Branch: 1 branch
BranchTrueFalseTotal
if ref $plugin 3 4 7
197 4 $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 198 ▶sub filter {
Subroutine filter: called CC=13 SCAR=25.9
Pod documented
214 198 my ($self, $name, $args, $alias) = @_;
215 198 my ($provider, $filter, $error);
216  
217 $self->debug("filter($name, ",
218 defined $args ? @$args : '[ ]',
219 $alias // '<no alias>', ')')
220 198 ▶ if $self->{ DEBUG };
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 198 198
defined $args ? : 0 0 0
Condition0%0/2
$alias // "<no alias>"
l!l
00
MC/DC0%0/1
$alias // "<no alias>"
$alias
Truth table0%0/2
A
A= $alias
Aresult
11
00
221  
222 # use any cached version of the filter if no params provided
223 return $filter
224 if ! $args && ! ref $name
225 198 ▶ && ($filter = $self->{ FILTER_CACHE }->{ $name });
Branch: 1 branch
BranchTrueFalseTotal
if not $args || ref $name and $filter = $self->{"FILTER_CACHE"}{$name} 12 186 198
Condition100%6/6
not $args || ref $name and $filter = $self->{"FILTER_CACHE"}{$name}
!ll&&!rl&&r
1018512
$args || ref $name
l!l&&r!l&&!r
95697
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 186 foreach my $provider (@{ $self->{ LOAD_FILTERS } }) {
229 186 ($filter, $error) = $provider->fetch($name, $args, $self);
230 186 ▶ last unless $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 176 10 186
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 177 ▶ return $self->error("$name: filter not found")
Branch: 1 branch
BranchTrueFalseTotal
unless $filter 1 176 177
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 176 ▶ $self->{ FILTER_CACHE }->{ $alias } = $filter
Branch: 1 branch
BranchTrueFalseTotal
if $alias 9 167 176
256 if $alias;
257  
258 176 return $filter;
259 }
260  
261  
262 #------------------------------------------------------------------------
263 # view(\%config)
264 #
265 # Create a new Template::View bound to this context.
266 #------------------------------------------------------------------------
267  
268 35 ▶sub view {
Subroutine view: called CC=2 SCAR=7.0
Pod documented
269 35 my $self = shift;
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 1791 ▶sub process {
Subroutine process: called CC=26 SCAR=33.1
Pod documented
297 1791 my ($self, $template, $params, $localize) = @_;
298 1791 my ($trim, $blocks) = @$self{ qw( TRIM BLOCKS ) };
299 1791 my (@compiled, $name, $compiled);
300 1791 my ($stash, $component, $tblocks, $error, $tmpout);
301 1791 my $output = '';
302  
303 1791 ▶ $template = [ $template ] unless ref $template eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $template eq "ARRAY" 1789 2 1791
304  
305 $self->debug("process([ ", join(', ', @$template), ' ], ',
306 $params // '<no params>', ', ',
307 $localize ? '<localized>' : '<unlocalized>', ')')
308 1791 ▶ if $self->{ DEBUG };
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 1791 1791
$localize ? : 0 0 0
Condition0%0/2
$params // "<no params>"
l!l
00
MC/DC0%0/1
$params // "<no params>"
$params
Truth table0%0/2
A
A= $params
Aresult
11
00
309  
310 # fetch compiled template for each name specified
311 1791 foreach $name (@$template) {
312 1793 push(@compiled, $self->template($name));
313 }
314  
315 1769 ▶ if ($localize) {
Branch: 1 branch
BranchTrueFalseTotal
if ($localize) { } 268 1501 1769
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 1501 $self->{ STASH }->update($params);
321 1501 $stash = $self->{ STASH };
322 }
323  
324 1769 eval {
325 # save current component
326 1769 eval { $component = $stash->get('component') };
327  
328 1769 foreach $name (@$template) {
329 1771 $compiled = shift @compiled;
330 1771 ▶ my $element = ref $compiled eq 'CODE'
Branch: 2 branches
BranchTrueFalseTotal
ref $compiled eq 'CODE' ? : 221 1550 1771
ref $name ? : 85 136 221
331 ? { (name => (ref $name ? '' : $name), modtime => time()) }
332 : $compiled;
333  
334 1771 ▶ if (blessed($component) && $component->isa(DOCUMENT)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($component) and $component->isa("Template::Document")) 294 1477 1771
Condition66%2/3
builtin::blessed($component) and $component->isa("Template::Document")
!ll&&!rl&&r
14770294
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 1771 $stash->set('component', $element);
341  
342 1771 ▶ unless ($localize) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($localize) 1502 269 1771
343 # merge any local blocks defined in the Template::Document
344 # into our local BLOCKS cache
345 1502 ▶ @$blocks{ keys %$tblocks } = values %$tblocks
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($compiled) and $compiled->isa("Template::Document") and $tblocks = $compiled->blocks 1482 20 1502
Condition66%4/6
builtin::blessed($compiled) and $compiled->isa("Template::Document") and $tblocks = $compiled->blocks
!ll&&!rl&&r
2001482
builtin::blessed($compiled) and $compiled->isa("Template::Document")
!ll&&!rl&&r
2001482
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 1771 ▶ if (ref $compiled eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $compiled eq 'CODE') { } 221 1550 1771
elsif (ref $compiled) { } 1550 0 1550
351 221 $tmpout = &$compiled($self);
352 }
353 elsif (ref $compiled) {
354 1550 $tmpout = $compiled->process($self);
355 }
356 else {
357 0 $self->throw(
358 'file',
359 "invalid template reference: $compiled"
360 );
361 }
362  
363 1742 ▶ if ($trim) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trim) 122 1620 1742
364 122 for ($tmpout) {
365 122 s/^\s+//;
366 122 s/\s+$//;
367 }
368 }
369 1742 $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 1742 ▶ pop(@{$element->{ callers }})
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($component) and $component->isa("Template::Document") 287 1455 1742
Condition66%2/3
builtin::blessed($component) and $component->isa("Template::Document")
!ll&&!rl&&r
14550287
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 1740 $stash->set('component', $component);
382 };
383 1769 $error = $@;
384  
385 1769 ▶ if ($localize) {
Branch: 1 branch
BranchTrueFalseTotal
if ($localize) 268 1501 1769
386 # ensure stash is delocalised before dying
387 268 $self->{ STASH } = $self->{ STASH }->declone();
388 }
389  
390 $self->throw(
391 1769 ▶ ref $error
Branch: 2 branches
BranchTrueFalseTotal
if $error 29 1740 1769
ref $error ? : 29 0 29
392 ? $error
393 : (Template::Constants::ERROR_FILE, $error)
394 ) if $error;
395  
396 1740 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 286 ▶sub include {
Subroutine include: called CC=1 SCAR=0.0
Pod documented
415 286 my ($self, $template, $params) = @_;
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 15 ▶sub insert {
Subroutine insert: called CC=12 SCAR=25.1
Pod documented
426 15 my ($self, $file) = @_;
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 157 ▶sub throw {
Subroutine throw: called CC=7 SCAR=20.8
Pod documented
504 157 my ($self, $error, $info, $output) = @_;
505 157 local $" = ', ';
506  
507 # die! die! die!
508 157 ▶ if (blessed($error) && $error->isa(EXCEPTION)) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($error) and $error->isa('Template::Exception')) { } 37 120 157
elsif (builtin::blessed($error) and $error->isa('Badger::Exception')) { } 0 120 120
elsif (defined $info) { } 115 5 120
Condition66%2/3
builtin::blessed($error) and $error->isa("Template::Exception")
!ll&&!rl&&r
120037
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 37 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
12000
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 115 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 177 ▶sub catch {
Subroutine catch: called CC=5 SCAR=16.1
Pod documented
548 177 my ($self, $error, $output) = @_;
549  
550 177 ▶ if ( blessed($error)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($error) and $error->isa('Template::Exception') || $error->isa('Badger::Exception')) { } 162 15 177
Condition83%5/6
builtin::blessed($error) and $error->isa("Template::Exception") || $error->isa("Badger::Exception")
!ll&&!rl&&r
141162
$error->isa("Template::Exception") or $error->isa("Badger::Exception")
l!l&&r!l&&!r
16201
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 162 ▶ $error->text($output) if $output;
Branch: 1 branch
BranchTrueFalseTotal
if $output 137 25 162
553 162 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 1429 ▶sub localise {
Subroutine localise: called CC=1 SCAR=0.0
Pod documented
575 1429 my $self = shift;
576 1429 $self->{ STASH } = $self->{ STASH }->clone(@_);
577 }
578  
579 1429 ▶sub delocalise {
Subroutine delocalise: called CC=1 SCAR=0.0
Pod documented
580 1429 my $self = shift;
581 1429 $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 1556 ▶sub visit {
Subroutine visit: called CC=1 SCAR=0.0
Pod documented
597 1556 my ($self, $document, $blocks) = @_;
598 1556 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 1553 ▶sub leave {
Subroutine leave: called CC=1 SCAR=0.0
Pod documented
613 1553 my $self = shift;
614 1553 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 2 ▶sub define_block {
Subroutine define_block: called CC=3 SCAR=11.1
Pod documented
630 2 my ($self, $name, $block) = @_;
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 14 ▶sub define_filter {
Subroutine define_filter: called CC=5 SCAR=17.8
Pod documented
645 14 my ($self, $name, $filter, $is_dynamic) = @_;
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 9 ▶sub define_vmethod {
Subroutine define_vmethod: called CC=1 SCAR=0.0
Pod documented
669 9 my $self = shift;
670 9 $self->stash->define_vmethod(@_);
671 }
672  
673  
674 #------------------------------------------------------------------------
675 # define_view($name, $params)
676 #
677 # Defines a new view.
678 #------------------------------------------------------------------------
679  
680 2 ▶sub define_view {
Subroutine define_view: called CC=5 SCAR=16.6
Pod documented
681 2 my ($self, $name, $params) = @_;
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 1 ▶sub define_views {
Subroutine define_views: called CC=3 SCAR=11.0
Pod documented
712 1 my ($self, $views) = @_;
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 1385 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
740 1385 my ($self, $blocks) = @_;
741 1385 $self->{ BLKSTACK } = [ ];
742 1385 $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 1782 ▶sub stash {
Subroutine stash: called CC=1 SCAR=0.0
Pod documented
755 1782 return $_[0]->{ STASH };
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 20 ▶sub debugging {
Subroutine debugging: called CC=12 SCAR=25.0
Pod documented
770 20 my $self = shift;
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 } // $DEBUG_FORMAT;
Condition33%1/3
$self->{"DEBUG_FORMAT"} // $Template::Context::DEBUG_FORMAT
l!l&&r!l&&!r
900
MC/DC0%0/2
$self->{"DEBUG_FORMAT"} // $Template::Context::DEBUG_FORMAT
$self->{"DEBUG_FORMAT"} $Template::Context::DEBUG_FORMAT
Truth table33%1/3
A // B
A= $self->{"DEBUG_FORMAT"}B= $Template::Context::DEBUG_FORMAT
ABresult
1X1
011
000
789 9 $format =~ s/\$(\w+)/$hash->{ $1 }/ge;
790 9 return $format;
791 }
792 elsif ( lc($args[0]) eq 'format' ) {
793 1 $self->{ DEBUG_FORMAT } = $args[1];
794 }
795 # else ignore
796 }
797  
798 11 return '';
799 }
800  
801  
802 #------------------------------------------------------------------------
803 # AUTOLOAD
804 #
805 # Provides pseudo-methods for read-only access to various internal
806 # members. For example, templates(), plugins(), filters(),
807 # eval_perl(), load_perl(), etc. These aren't called very often, or
808 # may never be called at all.
809 #------------------------------------------------------------------------
810  
811 28 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.2
812 28 my $self = shift;
813 28 my $method = $AUTOLOAD;
814 28 my $result;
815  
816 28 $method =~ s/.*:://;
817 28 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 0 28 28
818  
819 warn "no such context method/member: $method\n"
820 28 ▶ unless defined ($result = $self->{ uc $method });
Branch: 1 branch
BranchTrueFalseTotal
unless defined($result = $$self{uc $method}) 0 28 28
821  
822 28 return $result;
823 }
824  
825  
826 #------------------------------------------------------------------------
827 # DESTROY
828 #
829 # Stash may contain references back to the Context via macro closures,
830 # etc. This breaks the circular references.
831 #------------------------------------------------------------------------
832  
833 263 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
834 263 my $self = shift;
835 263 undef $self->{ STASH };
836 }
837  
838  
839  
840 #========================================================================
841 # -- PRIVATE METHODS --
842 #========================================================================
843  
844 #------------------------------------------------------------------------
845 # _init(\%config)
846 #
847 # Initialisation method called by Template::Base::new()
848 #------------------------------------------------------------------------
849  
850 267 ▶sub _init {
Subroutine _init: called CC=26 SCAR=33.1
851 267 my ($self, $config) = @_;
852 267 my ($name, $item, $method, $block, $blocks);
853 267 my @itemlut = (
854 LOAD_TEMPLATES => 'provider',
855 LOAD_PLUGINS => 'plugins',
856 LOAD_FILTERS => 'filters'
857 );
858  
859 # LOAD_TEMPLATE, LOAD_PLUGINS, LOAD_FILTERS - lists of providers
860 267 ▶ while (($name, $method) = splice(@itemlut, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($name, $method) = splice(@itemlut, 0, 2)) 801 267 1068
861 801 ▶ $item = $config->{ $name }
Condition60%3/5
$config->{$name} || 'Template::Config'->$method($config) || (return $self->error($Template::Config::ERROR))
l!l
8010
$config->{$name} or "Template::Config"->$method($config)
l!l&&r!l&&!r
77940
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
862 || Template::Config->$method($config)
863 || return $self->error($Template::Config::ERROR);
864 801 ▶ $self->{ $name } = ref $item eq 'ARRAY' ? $item : [ $item ];
Branch: 1 branch
BranchTrueFalseTotal
ref $item eq 'ARRAY' ? : 7 794 801
865 }
866  
867 267 my $providers = $self->{ LOAD_TEMPLATES };
868 267 ▶ my $prefix_map = $self->{ PREFIX_MAP } = $config->{ PREFIX_MAP } || { };
Condition100%2/2
$config->{'PREFIX_MAP'} || {}
l!l
1266
MC/DC100%1/1
$config->{'PREFIX_MAP'} || {}
$config->{'PREFIX_MAP'}
Truth table100%2/2
A
A= $config->{'PREFIX_MAP'}
Aresult
11
00
869 267 ▶ while (my ($key, $val) = each %$prefix_map) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$prefix_map) 3 267 270
870 $prefix_map->{ $key } = [
871 ref $val
872 ? $val
873 3 ▶ : map { $providers->[$_] } split(/\D+/, $val)
Branch: 2 branches
BranchTrueFalseTotal
unless ref $val eq "ARRAY" 3 0 3
ref $val ? : 0 3 3
874 ]
875 unless ref $val eq 'ARRAY';
876 }
877  
878 # STASH
879 267 ▶ $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; $predefs->{'_STRICT'} = $config->{'STRICT'}; 'Template::Config'->stash($predefs) or return $self->error($Template::Config::ERROR) }
l!l&&r!l&&!r
62610
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; $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; $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; $predefs->{'_STRICT'} = $config->{'STRICT'}; 'Template::Config'->stash($predefs) or return $self->error($Template::Config::ERROR) }
ABresult
1X1
011
000
880 my $predefs = $config->{ VARIABLES }
881 || $config->{ PRE_DEFINE }
882 261 ▶ || { };
Condition40%2/5
$config->{'VARIABLES'} || $config->{'PRE_DEFINE'} || {}
l!l
0261
$config->{"VARIABLES"} or $config->{"PRE_DEFINE"}
l!l&&r!l&&!r
00261
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
883  
884 # hack to get stash to know about debug mode
885 $predefs->{ _DEBUG } //= (
886 261 ▶ ($config->{ DEBUG } || 0) & &Template::Constants::DEBUG_UNDEF
Branch: 1 branch
BranchTrueFalseTotal
($config->{'DEBUG'} || 0) & &Template::Constants::DEBUG_UNDEF ? : 1 260 261
Condition75%3/4
$predefs->{"_DEBUG"} //= ($config->{"DEBUG"} || 0) & &Template::Constants::DEBUG_UNDEF ? 1 : 0
l!l
0261
$config->{'DEBUG'} || 0
l!l
8253
MC/DC0%0/1
$predefs->{"_DEBUG"} //= ($config->{"DEBUG"} || 0) & &Template::Constants::DEBUG_UNDEF ? 1 : 0
$predefs->{"_DEBUG"}
Truth table50%1/2
A
A= $predefs->{"_DEBUG"}
Aresult
11
00
887 ) ? 1 : 0;
888 261 $predefs->{ _STRICT } = $config->{ STRICT };
889  
890 261 ▶ Template::Config->stash($predefs)
Condition50%1/2
'Template::Config'->stash($predefs) || (return $self->error($Template::Config::ERROR))
l!l
2610
MC/DC0%0/1
'Template::Config'->stash($predefs) || (return $self->error($Template::Config::ERROR))
'Template::Config'->stash($predefs)
Truth table50%1/2
A
A= 'Template::Config'->stash($predefs)
Aresult
11
00
891 || return $self->error($Template::Config::ERROR);
892 };
893  
894 # compile any template BLOCKS specified as text
895 267 ▶ $blocks = $config->{ BLOCKS } || { };
Condition100%2/2
$config->{'BLOCKS'} || {}
l!l
14253
MC/DC100%1/1
$config->{'BLOCKS'} || {}
$config->{'BLOCKS'}
Truth table100%2/2
A
A= $config->{'BLOCKS'}
Aresult
11
00
896 $self->{ INIT_BLOCKS } = $self->{ BLOCKS } = {
897 map {
898 267 $block = $blocks->{ $_ };
899 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
900 || return undef
901 unless ref $block;
902 53 ($_ => $block);
903 }
904 keys %$blocks
905 };
906  
907 # define any VIEWS
908 $self->define_views( $config->{ VIEWS } )
909 267 ▶ if $config->{ VIEWS };
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"VIEWS"} 1 266 267
910  
911 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
912 # RECURSION - flag indicating is recursion into templates is supported
913 # EVAL_PERL - flag indicating if PERL blocks should be processed
914 # TRIM - flag to remove leading and trailing whitespace from output
915 # BLKSTACK - list of hashes of BLOCKs defined in current template(s)
916 # CONFIG - original configuration hash
917 # EXPOSE_BLOCKS - make blocks visible as pseudo-files
918 # DEBUG_FORMAT - format for generating template runtime debugging messages
919 # DEBUG - format for generating template runtime debugging messages
920  
921 267 ▶ $self->{ RECURSION } = $config->{ RECURSION } || 0;
Condition100%2/2
$config->{'RECURSION'} || 0
l!l
1266
MC/DC100%1/1
$config->{'RECURSION'} || 0
$config->{'RECURSION'}
Truth table100%2/2
A
A= $config->{'RECURSION'}
Aresult
11
00
922 267 ▶ $self->{ EVAL_PERL } = $config->{ EVAL_PERL } || 0;
Condition100%2/2
$config->{'EVAL_PERL'} || 0
l!l
9258
MC/DC100%1/1
$config->{'EVAL_PERL'} || 0
$config->{'EVAL_PERL'}
Truth table100%2/2
A
A= $config->{'EVAL_PERL'}
Aresult
11
00
923 267 ▶ $self->{ TRIM } = $config->{ TRIM } || 0;
Condition100%2/2
$config->{'TRIM'} || 0
l!l
10257
MC/DC100%1/1
$config->{'TRIM'} || 0
$config->{'TRIM'}
Truth table100%2/2
A
A= $config->{'TRIM'}
Aresult
11
00
924 267 $self->{ BLKSTACK } = [ ];
925 267 $self->{ CONFIG } = $config;
926 267 ▶ $self->{ EXPOSE_BLOCKS } = $config->{ EXPOSE_BLOCKS } // 0;
Condition100%2/2
$config->{"EXPOSE_BLOCKS"} // 0
l!l
1266
MC/DC100%1/1
$config->{"EXPOSE_BLOCKS"} // 0
$config->{"EXPOSE_BLOCKS"}
Truth table100%2/2
A
A= $config->{"EXPOSE_BLOCKS"}
Aresult
11
00
927 267 $self->{ DEBUG_FORMAT } = $config->{ DEBUG_FORMAT };
928 267 ▶ $self->{ DEBUG_DIRS } = ($config->{ DEBUG } || 0)
Condition100%2/2
$config->{'DEBUG'} || 0
l!l
8259
MC/DC100%1/1
$config->{'DEBUG'} || 0
$config->{'DEBUG'}
Truth table100%2/2
A
A= $config->{'DEBUG'}
Aresult
11
00
929 & Template::Constants::DEBUG_DIRS;
930 $self->{ DEBUG } = defined $config->{ DEBUG }
931 ? $config->{ DEBUG }
932 267 ▶ & ( Template::Constants::DEBUG_CONTEXT
Branch: 1 branch
BranchTrueFalseTotal
defined $config->{'DEBUG'} ? : 21 246 267
933 | Template::Constants::DEBUG_FLAGS )
934 : $DEBUG;
935  
936 267 return $self;
937 }
938  
939 106 1;
940  
941 __END__
« lib/Template/Parser.pm ↑ Summary lib/Template/Service.pm »