lib/Template/View.pm

filter: Statementstmt 97.3% 147 / 151 Branchbran 75.8% 85 / 112 Conditioncond 70.4% 50 / 71 MC/DCmcdc 54.7% 23 / 42 Subroutinesub 92.8% 13 / 14 Podpod 33.3% 3 / 9 totaltotal 80.4% 321 / 399 CC 87 SCAR 29.9
CC
87
Cov
84%
AUTOLOAD
30.6
print
29.5
_init
17.0
CRAP 19.8
« lib/Template/Test.pm ↑ Summary lib/Template/Plugin/Date.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::View
4 #
5 # DESCRIPTION
6 # A custom view of a template processing context. Can be used to
7 # implement custom "skins".
8 #
9 # AUTHOR
10 # Andy Wardley <abw@kfs.org>
11 #
12 # COPYRIGHT
13 # Copyright (C) 2000-2022 Andy Wardley. All Rights Reserved.
14 #
15 # This module is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
17 #
18 # TODO
19 # * allowing print to have a hash ref as final args will cause problems
20 # if you do this: [% view.print(hash1, hash2, hash3) %]. Current
21 # work-around is to do [% view.print(hash1); view.print(hash2);
22 # view.print(hash3) %] or [% view.print(hash1, hash2, hash3, { }) %]
23 #
24 #============================================================================
25  
26 package Template::View;
27  
28 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use base 'Template::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 3 our $VERSION = '3.106';
33 3 ▶our $DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::View::DEBUG 3 0 3
34 3 our @BASEARGS = qw( context );
35 3 our $AUTOLOAD;
36 3 our $MAP = {
37 HASH => 'hash',
38 ARRAY => 'list',
39 TEXT => 'text',
40 default => '',
41 };
42  
43  
44 #------------------------------------------------------------------------
45 # _init(\%config)
46 #
47 # Initialisation method called by the Template::Base class new()
48 # constructor. $self->{ context } has already been set, by virtue of
49 # being named in @BASEARGS. Remaining config arguments are presented
50 # as a hash reference.
51 #------------------------------------------------------------------------
52  
53 66 ▶sub _init {
Subroutine _init: called CC=17 SCAR=28.4
54 66 my ($self, $config) = @_;
55  
56 # move 'context' somewhere more private
57 66 $self->{ _CONTEXT } = $self->{ context };
58 66 delete $self->{ context };
59  
60 # generate table mapping object types to templates
61 66 ▶ my $map = $config->{ map } || { };
Condition100%2/2
$config->{'map'} || {}
l!l
561
MC/DC100%1/1
$config->{'map'} || {}
$config->{'map'}
Truth table100%2/2
A
A= $config->{'map'}
Aresult
11
00
62 66 ▶ $map->{ default } //= $config->{ default };
Condition100%3/3
$map->{"default"} //= $config->{"default"}
l!l&&r!l&&!r
3360
MC/DC100%2/2
$map->{"default"} //= $config->{"default"}
$map->{"default"} $config->{"default"}
Truth table100%3/3
A //= B
A= $map->{"default"}B= $config->{"default"}
ABresult
1X1
011
000
63 $self->{ map } = {
64 66 %$MAP,
65 %$map,
66 };
67  
68 # local BLOCKs definition table
69 66 ▶ $self->{ _BLOCKS } = $config->{ blocks } || { };
Condition100%2/2
$config->{'blocks'} || {}
l!l
1650
MC/DC100%1/1
$config->{'blocks'} || {}
$config->{'blocks'}
Truth table100%2/2
A
A= $config->{'blocks'}
Aresult
11
00
70  
71 # name of presentation method which printed objects might provide
72 66 ▶ $self->{ method } = $config->{ method } // 'present';
Condition50%1/2
$config->{"method"} // "present"
l!l
066
MC/DC0%0/1
$config->{"method"} // "present"
$config->{"method"}
Truth table50%1/2
A
A= $config->{"method"}
Aresult
11
00
73  
74 # view is sealed by default preventing variable update after
75 # definition, however we don't actually seal a view until the
76 # END of the view definition
77 66 my $sealed = $config->{ sealed };
78 66 ▶ $sealed //= 1;
Condition100%2/2
$sealed //= 1
l!l
165
MC/DC100%1/1
$sealed //= 1
$sealed
Truth table100%2/2
A
A= $sealed
Aresult
11
00
79 66 ▶ $self->{ sealed } = $sealed ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$sealed ? : 65 1 66
80  
81 # copy remaining config items from $config or set defaults
82 66 foreach my $arg (qw( base prefix suffix notfound silent )) {
83 330 ▶ $self->{ $arg } = $config->{ $arg } || '';
Condition100%2/2
$config->{$arg} || ''
l!l
39291
MC/DC100%1/1
$config->{$arg} || ''
$config->{$arg}
Truth table100%2/2
A
A= $config->{$arg}
Aresult
11
00
84 }
85  
86 # check that any base specified is defined
87 return $self->error('Invalid base specified for view')
88 66 ▶ if exists $config->{ base } && ! $self->{ base };
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{"base"} and not $self->{"base"} 1 65 66
Condition100%3/3
exists $config->{"base"} and not $self->{"base"}
!ll&&!rl&&r
5961
MC/DC100%2/2
exists $config->{"base"} and not $self->{"base"}
exists $config->{"base"} not $self->{"base"}
Truth table100%3/3
A and not(B)
A= exists $config->{"base"}B= not $self->{"base"}
ABresult
0X0
100
111
89  
90 # name of data item used by view()
91 65 ▶ $self->{ item } = $config->{ item } || 'item';
Condition100%2/2
$config->{'item'} || 'item'
l!l
263
MC/DC100%1/1
$config->{'item'} || 'item'
$config->{'item'}
Truth table100%2/2
A
A= $config->{'item'}
Aresult
11
00
92  
93 # map methods of form ${include_prefix}_foobar() to include('foobar')?
94 65 ▶ $self->{ include_prefix } = $config->{ include_prefix } || 'include_';
Condition50%1/2
$config->{'include_prefix'} || 'include_'
l!l
065
MC/DC0%0/1
$config->{'include_prefix'} || 'include_'
$config->{'include_prefix'}
Truth table50%1/2
A
A= $config->{'include_prefix'}
Aresult
11
00
95 # what about mapping foobar() to include('foobar')?
96 65 ▶ $self->{ include_naked } = $config->{ include_naked } // 1;
Condition100%2/2
$config->{"include_naked"} // 1
l!l
362
MC/DC100%1/1
$config->{"include_naked"} // 1
$config->{"include_naked"}
Truth table100%2/2
A
A= $config->{"include_naked"}
Aresult
11
00
97  
98 # map methods of form ${view_prefix}_foobar() to include('foobar')?
99 65 ▶ $self->{ view_prefix } = $config->{ view_prefix } || 'view_';
Condition50%1/2
$config->{'view_prefix'} || 'view_'
l!l
065
MC/DC0%0/1
$config->{'view_prefix'} || 'view_'
$config->{'view_prefix'}
Truth table50%1/2
A
A= $config->{'view_prefix'}
Aresult
11
00
100 # what about mapping foobar() to view('foobar')?
101 65 ▶ $self->{ view_naked } = $config->{ view_naked } || 0;
Condition100%2/2
$config->{'view_naked'} || 0
l!l
164
MC/DC100%1/1
$config->{'view_naked'} || 0
$config->{'view_naked'}
Truth table100%2/2
A
A= $config->{'view_naked'}
Aresult
11
00
102  
103 # the view is initially unsealed, allowing directives in the initial
104 # view template to create data items via the AUTOLOAD; once sealed via
105 # call to seal(), the AUTOLOAD will not update any internal items.
106 65 delete @$config{ qw(
107 base method map default prefix suffix notfound item
108 include_prefix include_naked silent sealed
109 view_prefix view_naked blocks
110 ) };
111 6 $config = { %{ $self->{ base }->{ data } }, %$config }
112 65 ▶ if $self->{ base };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"base"} 6 59 65
113 65 $self->{ data } = $config;
114 65 $self->{ SEALED } = 0;
115  
116 65 return $self;
117 }
118  
119  
120 #------------------------------------------------------------------------
121 # seal()
122 # unseal()
123 #
124 # Seal or unseal the view to allow/prevent new data items from being
125 # automatically created by the AUTOLOAD method.
126 #------------------------------------------------------------------------
127  
128 63 ▶sub seal {
Subroutine seal: called CC=1 SCAR=0.0
Pod undocumented
129 63 my $self = shift;
130 63 $self->{ SEALED } = $self->{ sealed };
131 }
132  
133 0 ▶sub unseal {
Subroutine unseal: not called CC=1 SCAR=6.9
Pod undocumented
134 0 my $self = shift;
135 0 $self->{ SEALED } = 0;
136 }
137  
138  
139 #------------------------------------------------------------------------
140 # clone(\%config)
141 #
142 # Cloning method which takes a copy of $self and then applies to it any
143 # modifications specified in the $config hash passed as an argument.
144 # Configuration items may also be specified as a list of "name => $value"
145 # arguments. Returns a reference to the cloned Template::View object.
146 #
147 # NOTE: may need to copy BLOCKS???
148 #------------------------------------------------------------------------
149  
150 13 ▶sub clone {
Subroutine clone: called CC=6 SCAR=18.0
Pod undocumented
151 13 my $self = shift;
152 13 my $clone = bless { %$self }, ref $self;
153 13 ▶ my $config = ref $_[0] eq 'HASH' ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 13 0 13
154  
155 # merge maps
156 $clone->{ map } = {
157 13 %{ $self->{ map } },
158 13 ▶ %{ $config->{ map } || { } },
Condition50%1/2
$config->{'map'} || {}
l!l
013
MC/DC0%0/1
$config->{'map'} || {}
$config->{'map'}
Truth table50%1/2
A
A= $config->{'map'}
Aresult
11
00
159 };
160  
161 # "map => { default=>'xxx' }" can be specified as "default => 'xxx'"
162 $clone->{ map }->{ default } = $config->{ default }
163 13 ▶ if defined $config->{ default };
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"default"} 2 11 13
164  
165 # update any remaining config items
166 13 my @args = qw(
167 base prefix suffix notfound item method include_prefix
168 include_naked view_prefix view_naked
169 );
170 13 foreach my $arg (@args) {
171 130 ▶ $clone->{ $arg } = $config->{ $arg } if defined $config->{ $arg };
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{$arg} 9 121 130
172 }
173 13 push(@args, qw( default map ));
174 13 delete @$config{ @args };
175  
176 # anything left is data
177 13 my $data = $clone->{ data } = { %{ $self->{ data } } };
178 13 @$data{ keys %$config } = values %$config;
179  
180 13 return $clone;
181 }
182  
183  
184 #------------------------------------------------------------------------
185 # print(@items, ..., \%config)
186 #
187 # Prints @items in turn by mapping each to an appropriate template using
188 # the internal 'map' hash. If an entry isn't found and the item is an
189 # object that implements the method named in the internal 'method' item,
190 # (default: 'present'), then the method will be called passing a reference
191 # to $self, against which the presenter method may make callbacks (e.g.
192 # to view_item()). If the presenter method isn't implemented, then the
193 # 'default' map entry is consulted and used if defined. The final argument
194 # may be a reference to a hash array providing local overrides to the internal
195 # defaults for various items (prefix, suffix, etc). In the presence
196 # of this parameter, a clone of the current object is first made, applying
197 # any configuration updates, and control is then delegated to it.
198 #------------------------------------------------------------------------
199  
200 43 ▶sub print {
Subroutine print: called CC=20 SCAR=33.9
Pod documented
201 43 my $self = shift;
202  
203 # if final config hash is specified then create a clone and delegate to it
204 # NOTE: potential problem when called print(\%data_hash1, \%data_hash2);
205 43 ▶ if ((scalar @_ > 1) && (ref $_[-1] eq 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1 and ref $_[-1] eq "HASH") 5 38 43
Condition66%2/3
scalar @_ > 1 and ref $_[-1] eq "HASH"
!ll&&!rl&&r
3805
MC/DC50%1/2
scalar @_ > 1 and ref $_[-1] eq "HASH"
scalar @_ > 1 ref $_[-1] eq "HASH"
Truth table66%2/3
A and B
A= scalar @_ > 1B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
206 5 my $cfg = pop @_;
207 5 ▶ my $clone = $self->clone($cfg)
Condition50%1/2
$self->clone($cfg) || (return)
l!l
50
MC/DC0%0/1
$self->clone($cfg) || (return)
$self->clone($cfg)
Truth table50%1/2
A
A= $self->clone($cfg)
Aresult
11
00
208 || return;
209 5 ▶ return $clone->print(@_)
Condition33%1/3
$clone->print(@_) || $self->error($clone->error)
l!l&&r!l&&!r
500
MC/DC0%0/2
$clone->print(@_) || $self->error($clone->error)
$clone->print(@_) $self->error($clone->error)
Truth table33%1/3
A || B
A= $clone->print(@_)B= $self->error($clone->error)
ABresult
1X1
011
000
210 || $self->error($clone->error());
211 }
212 38 my ($item, $type, $template, $present);
213 38 my $method = $self->{ method };
214 38 my $map = $self->{ map };
215 38 my $output = '';
216  
217 # print each argument
218 38 foreach $item (@_) {
219 38 my $newtype;
220  
221 38 ▶ if (! ($type = ref $item)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $type = ref $item) { } 9 29 38
elsif (not defined($template = $map->{$type})) { } 18 11 29
222 # non-references are TEXT
223 9 $type = 'TEXT';
224 9 $template = $map->{ $type };
225 }
226 elsif (! defined ($template = $map->{ $type })) {
227 # no specific map entry for object, maybe it implements a
228 # 'present' (or other) method?
229 18 ▶ if ( $method && UNIVERSAL::can($item, $method) ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($method and UNIVERSAL::can($item, $method)) { } 3 15 18
elsif (ref $item eq 'HASH' and defined($newtype = $item->{$method}) and defined($template = $$map{"$method=>$newtype"})) { } 0 15 15
elsif (defined $newtype and defined($template = $$map{"$method=>*"})) { } 0 15 15
elsif (not $template = $map->{'default'}) { } 7 8 15
Condition66%2/3
$method and UNIVERSAL::can($item, $method)
!ll&&!rl&&r
0153
MC/DC50%1/2
$method and UNIVERSAL::can($item, $method)
$method UNIVERSAL::can($item, $method)
Truth table66%2/3
A and B
A= $methodB= UNIVERSAL::can($item, $method)
ABresult
0X0
100
111
230 3 $present = $item->$method($self); ## call item method
231 # undef returned indicates error, note that we expect
232 # $item to have called error() on the view
233 3 ▶ return unless defined $present;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $present 0 3 3
234 3 $output .= $present;
235 3 next; ## NEXT
236 }
237 ▶ elsif ( ref($item) eq 'HASH'
Condition33%2/6
ref $item eq "HASH" and defined($newtype = $item->{$method}) and defined($template = $$map{"$method=>$newtype"})
!ll&&!rl&&r
1500
ref $item eq "HASH" and defined($newtype = $item->{$method})
!ll&&!rl&&r
1500
MC/DC0%0/3
ref $item eq "HASH" and defined($newtype = $item->{$method}) and defined($template = $$map{"$method=>$newtype"})
ref $item eq "HASH" defined($newtype = $item->{$method}) defined($template = $$map{"$method=>$newtype"})
Truth table25%1/4
A and B and C
A= ref $item eq "HASH"B= defined($newtype = $item->{$method})C= defined($template = $$map{"$method=>$newtype"})
ABCresult
0XX0
10X0
1100
1111
238 && defined($newtype = $item->{$method})
239 && defined($template = $map->{"$method=>$newtype"})) {
240 }
241 ▶ elsif ( defined($newtype)
Condition33%1/3
defined $newtype and defined($template = $$map{"$method=>*"})
!ll&&!rl&&r
1500
MC/DC0%0/2
defined $newtype and defined($template = $$map{"$method=>*"})
defined $newtype defined($template = $$map{"$method=>*"})
Truth table33%1/3
A and B
A= defined $newtypeB= defined($template = $$map{"$method=>*"})
ABresult
0X0
100
111
242 && defined($template = $map->{"$method=>*"}) ) {
243 0 $template =~ s/\*/$newtype/;
244 }
245 elsif (! ($template = $map->{ default }) ) {
246 # default not defined, so construct template name from type
247 7 ($template = $type) =~ s/\W+/_/g;
248 }
249 }
250 # else {
251 # $self->DEBUG("defined map type for $type: $template\n");
252 # }
253 35 ▶ $self->DEBUG("printing view '", $template || '', "', $item\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::View::DEBUG 0 35 35
Condition0%0/2
$template || ''
l!l
00
MC/DC0%0/1
$template || ''
$template
Truth table0%0/2
A
A= $template
Aresult
11
00
254 35 ▶ $output .= $self->view($template, $item)
Branch: 1 branch
BranchTrueFalseTotal
if $template 35 0 35
255 if $template;
256 }
257 37 return $output;
258 }
259  
260  
261 #------------------------------------------------------------------------
262 # view($template, $item, \%vars)
263 #
264 # Wrapper around include() which expects a template name, $template,
265 # followed by a data item, $item, and optionally, a further hash array
266 # of template variables. The $item is added as an entry to the $vars
267 # hash (which is created empty if not passed as an argument) under the
268 # name specified by the internal 'item' member, which is appropriately
269 # 'item' by default. Thus an external object present() method can
270 # callback against this object method, simply passing a data item to
271 # be displayed. The external object doesn't have to know what the
272 # view expects the item to be called in the $vars hash.
273 #------------------------------------------------------------------------
274  
275 54 ▶sub view {
Subroutine view: called CC=3 SCAR=11.0
Pod documented
276 54 my ($self, $template, $item) = splice(@_, 0, 3);
277 54 ▶ my $vars = ref $_[0] eq 'HASH' ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 54 54
278 54 ▶ $vars->{ $self->{ item } } = $item if defined $item;
Branch: 1 branch
BranchTrueFalseTotal
if defined $item 52 2 54
279 54 $self->include($template, $vars);
280 }
281  
282  
283 #------------------------------------------------------------------------
284 # include($template, \%vars)
285 #
286 # INCLUDE a template, $template, mapped according to the current prefix,
287 # suffix, default, etc., where $vars is an optional hash reference
288 # containing template variable definitions. If the template isn't found
289 # then the method will default to any 'notfound' template, if defined
290 # as an internal item.
291 #------------------------------------------------------------------------
292  
293 85 ▶sub include {
Subroutine include: called CC=3 SCAR=11.2
Pod undocumented
294 85 my ($self, $template, $vars) = @_;
295 85 my $context = $self->{ _CONTEXT };
296  
297 85 $template = $self->template($template);
298  
299 84 ▶ $vars = { } unless ref $vars eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $vars eq "HASH" 21 63 84
300 84 ▶ $vars->{ view } ||= $self;
Condition33%1/3
$vars->{"view"} ||= $self
l!l&&r!l&&!r
0840
MC/DC0%0/2
$vars->{"view"} ||= $self
$vars->{"view"} $self
Truth table33%1/3
A ||= B
A= $vars->{"view"}B= $self
ABresult
1X1
011
000
301  
302 84 $context->include( $template, $vars );
303  
304 # DEBUGGING
305 # my $out = $context->include( $template, $vars );
306 # print STDERR "VIEW return [$out]\n";
307 # return $out;
308 }
309  
310  
311 #------------------------------------------------------------------------
312 # template($template)
313 #
314 # Returns a compiled template for the specified template name, according
315 # to the current configuration parameters.
316 #------------------------------------------------------------------------
317  
318 92 ▶sub template {
Subroutine template: called CC=14 SCAR=26.6
Pod undocumented
319 92 my ($self, $name) = @_;
320 92 my $context = $self->{ _CONTEXT };
321 92 ▶ return $context->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 92 92
322 Template::Constants::ERROR_VIEW,
323 "no view template specified"
324 ) unless $name;
325  
326 92 my $notfound = $self->{ notfound };
327 92 my $base = $self->{ base };
328 92 my ($template, $block, $error);
329  
330 return $block
331 92 ▶ if ($block = $self->{ _BLOCKS }->{ $name });
Branch: 1 branch
BranchTrueFalseTotal
if $block = $self->{"_BLOCKS"}{$name} 36 56 92
332  
333 # try the named template
334 56 $template = $self->template_name($name);
335 56 ▶ $self->DEBUG("looking for $template\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::View::DEBUG 0 56 56
336 56 eval { $template = $context->template($template) };
337  
338 # try asking the base view if not found
339 56 ▶ if (($error = $@) && $base) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error = $@ and $base) 6 50 56
Condition100%3/3
$error = $@ and $base
!ll&&!rl&&r
4466
MC/DC100%2/2
$error = $@ and $base
$error = $@ $base
Truth table100%3/3
A and B
A= $error = $@B= $base
ABresult
0X0
100
111
340 6 ▶ $self->DEBUG("asking base for $name\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::View::DEBUG 0 6 6
341 6 eval { $template = $base->template($name) };
342 }
343  
344 # try the 'notfound' template (if defined) if that failed
345 56 ▶ if (($error = $@) && $notfound) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error = $@ and $notfound) { } 5 51 56
elsif ($error) { } 1 50 51
Condition100%3/3
$error = $@ and $notfound
!ll&&!rl&&r
5015
MC/DC100%2/2
$error = $@ and $notfound
$error = $@ $notfound
Truth table100%3/3
A and B
A= $error = $@B= $notfound
ABresult
0X0
100
111
346 5 ▶ unless ($template = $self->{ _BLOCKS }->{ $notfound }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($template = $self->{"_BLOCKS"}{$notfound}) 4 1 5
347 4 $notfound = $self->template_name($notfound);
348 4 ▶ $self->DEBUG("not found, looking for $notfound\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::View::DEBUG 0 4 4
349 4 eval { $template = $context->template($notfound) };
350  
351 4 ▶ return $context->throw(Template::Constants::ERROR_VIEW, $error)
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
352 if $@; # return first error
353 }
354 }
355 elsif ($error) {
356 1 ▶ $self->DEBUG("no 'notfound'\n")
Branch: 1 branch
BranchTrueFalseTotal
if $Template::View::DEBUG 0 1 1
357 if $DEBUG;
358 1 return $context->throw(Template::Constants::ERROR_VIEW, $error);
359 }
360 55 return $template;
361 }
362  
363  
364 #------------------------------------------------------------------------
365 # template_name($template)
366 #
367 # Returns the name of the specified template with any appropriate prefix
368 # and/or suffix added.
369 #------------------------------------------------------------------------
370  
371 62 ▶sub template_name {
Subroutine template_name: called CC=3 SCAR=11.4
Pod undocumented
372 62 my ($self, $template) = @_;
373 $template = $self->{ prefix } . $template . $self->{ suffix }
374 62 ▶ if $template;
Branch: 1 branch
BranchTrueFalseTotal
if $template 62 0 62
375  
376 62 ▶ $self->DEBUG("template name: $template\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::View::DEBUG 0 62 62
377 62 return $template;
378 }
379  
380  
381 #------------------------------------------------------------------------
382 # default($val)
383 #
384 # Special case accessor to retrieve/update 'default' as an alias for
385 # '$map->{ default }'.
386 #------------------------------------------------------------------------
387  
388 7 ▶sub default {
Subroutine default: called CC=2 SCAR=6.9
Pod documented
389 7 my $self = shift;
390 return @_
391 ? ($self->{ map }->{ default } = shift)
392 7 ▶ : $self->{ map }->{ default };
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 5 7
393 }
394  
395  
396 #------------------------------------------------------------------------
397 # AUTOLOAD
398 #
399 # Returns/updates public internal data items (i.e. not prefixed '_' or
400 # '.') or presents a view if the method matches the view_prefix item,
401 # e.g. view_foo(...) => view('foo', ...). Similarly, the
402 # include_prefix is used, if defined, to map include_foo(...) to
403 # include('foo', ...). If that fails then the entire method name will
404 # be used as the name of a template to include iff the include_named
405 # parameter is set (default: 1). Last attempt is to match the entire
406 # method name to a view() call, iff view_naked is set. Otherwise, a
407 # 'view' exception is raised reporting the error "no such view member:
408 # $method".
409 #------------------------------------------------------------------------
410  
411 120 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=27 SCAR=34.2
412 120 my $self = shift;
413 120 my $item = $AUTOLOAD;
414 120 $item =~ s/.*:://;
415 120 ▶ return if $item eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $item eq "DESTROY" 0 120 120
416  
417 120 my $starts_with = substr($item,0,1);
418 120 ▶ if ($starts_with eq '.' || $starts_with eq '_') {
Branch: 8 branches
BranchTrueFalseTotal
if ($starts_with eq '.' or $starts_with eq '_') { } 0 120 120
elsif (exists $self->{$item}) { } 20 100 120
elsif (exists $self->{'data'}{$item}) { } 48 52 100
elsif (@_ and not $self->{'SEALED'}) { } 6 46 52
elsif ($item =~ s/^$self->{'view_prefix'}//) { } 15 31 46
elsif ($item =~ s/^$self->{'include_prefix'}//) { } 3 28 31
elsif ($self->{'include_naked'}) { } 26 2 28
elsif ($self->{'view_naked'}) { } 1 1 2
Condition33%1/3
$starts_with eq "." or $starts_with eq "_"
l!l&&r!l&&!r
00120
MC/DC0%0/2
$starts_with eq "." or $starts_with eq "_"
$starts_with eq "." $starts_with eq "_"
Truth table33%1/3
A or B
A= $starts_with eq "."B= $starts_with eq "_"
ABresult
1X1
011
000
419 0 return $self->{ _CONTEXT }->throw(Template::Constants::ERROR_VIEW,
420 "attempt to view private member: $item");
421 }
422 elsif (exists $self->{ $item }) {
423 # update existing config item (e.g. 'prefix') if unsealed
424 return $self->{ _CONTEXT }->throw(Template::Constants::ERROR_VIEW,
425 "cannot update config item in sealed view: $item")
426 20 ▶ if @_ && $self->{ SEALED };
Branch: 1 branch
BranchTrueFalseTotal
if @_ and $self->{"SEALED"} 1 19 20
Condition100%3/3
@_ and $self->{"SEALED"}
!ll&&!rl&&r
1721
MC/DC100%2/2
@_ and $self->{"SEALED"}
@_ $self->{"SEALED"}
Truth table100%3/3
A and B
A= @_B= $self->{"SEALED"}
ABresult
0X0
100
111
427 19 ▶ $self->DEBUG("accessing item: $item\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::View::DEBUG 0 19 19
428 19 ▶ return @_ ? ($self->{ $item } = shift) : $self->{ $item };
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 17 19
429 }
430 elsif (exists $self->{ data }->{ $item }) {
431 # get/update existing data item (must be unsealed to update)
432 48 ▶ if (@_ && $self->{ SEALED }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and $self->{"SEALED"}) 3 45 48
Condition100%3/3
@_ and $self->{"SEALED"}
!ll&&!rl&&r
4323
MC/DC100%2/2
@_ and $self->{"SEALED"}
@_ $self->{"SEALED"}
Truth table100%3/3
A and B
A= @_B= $self->{"SEALED"}
ABresult
0X0
100
111
433 return $self->{ _CONTEXT }->throw(
434 Template::Constants::ERROR_VIEW,
435 "cannot update item in sealed view: $item"
436 3 ▶ ) unless $self->{ silent };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"silent"} 1 2 3
437 # ignore args if silent
438 2 @_ = ();
439 }
440 $self->DEBUG(@_
441 47 ▶ ? "updating data item: $item <= $_[0]\n"
Branch: 2 branches
BranchTrueFalseTotal
if $Template::View::DEBUG 0 47 47
@_ ? : 0 0 0
442 : "returning data item: $item\n"
443 ) if $DEBUG;
444 return @_
445 ? ($self->{ data }->{ $item } = shift)
446 47 ▶ : $self->{ data }->{ $item };
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 45 47
447 }
448 ▶ elsif (@_ && ! $self->{ SEALED }) {
Condition100%3/3
@_ and not $self->{"SEALED"}
!ll&&!rl&&r
23236
MC/DC100%2/2
@_ and not $self->{"SEALED"}
@_ not $self->{"SEALED"}
Truth table100%3/3
A and not(B)
A= @_B= not $self->{"SEALED"}
ABresult
0X0
100
111
449 # set data item if unsealed
450 6 ▶ $self->DEBUG("setting unsealed data: $item => @_\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::View::DEBUG 0 6 6
451 6 $self->{ data }->{ $item } = shift;
452 }
453 elsif ($item =~ s/^$self->{ view_prefix }//) {
454 15 ▶ $self->DEBUG("returning view($item)\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::View::DEBUG 0 15 15
455 15 return $self->view($item, @_);
456 }
457 elsif ($item =~ s/^$self->{ include_prefix }//) {
458 3 ▶ $self->DEBUG("returning include($item)\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::View::DEBUG 0 3 3
459 3 return $self->include($item, @_);
460 }
461 elsif ($self->{ include_naked }) {
462 26 ▶ $self->DEBUG("returning naked include($item)\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::View::DEBUG 0 26 26
463 26 return $self->include($item, @_);
464 }
465 elsif ($self->{ view_naked }) {
466 1 ▶ $self->DEBUG("returning naked view($item)\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::View::DEBUG 0 1 1
467 1 return $self->view($item, @_);
468 }
469 else {
470 return $self->{ _CONTEXT }->throw(
471 1 Template::Constants::ERROR_VIEW,
472 "no such view member: $item"
473 );
474 }
475 }
476  
477  
478 3 1;
479  
480  
481 __END__
« lib/Template/Test.pm ↑ Summary lib/Template/Plugin/Date.pm »