lib/Template/View.pm

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