lib/Template/Stash.pm

filter: Statementstmt 91.4% 172 / 188 Branchbran 75.0% 114 / 152 Conditioncond 72.4% 71 / 98 MC/DCmcdc 52.5% 31 / 59 Subroutinesub 94.7% 18 / 19 Podpod 100% 9 / 9 totaltotal 79.0% 415 / 525 CC 108 SCAR 38.4
CC
108
Cov
82%
_dotop
96.1
_assign
41.5
_reconstruct_ident
7.0
CRAP 46.7
« blib/lib/Template/Stash/Context.pm ↑ Summary lib/Template/Test.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Stash
4 #
5 # DESCRIPTION
6 # Definition of an object class which stores and manages access to
7 # variables for the Template Toolkit.
8 #
9 # AUTHOR
10 # Andy Wardley <abw@wardley.org>
11 #
12 # COPYRIGHT
13 # Copyright (C) 1996-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 #============================================================================
19  
20 package Template::Stash;
21  
22 106 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 106 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 106 ▶use Template::VMethods;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 106 ▶use Template::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 106 ▶use Scalar::Util qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 106 our $VERSION = '3.106';
29 106 ▶our $DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::Stash::DEBUG 106 0 106
30 106 our $PRIVATE = qr/^[_.]/;
31 106 our $UNDEF_TYPE = 'var.undef';
32 106 our $UNDEF_INFO = 'undefined variable: %s';
33  
34 # alias _dotop() to dotop() so that we have a consistent method name
35 # between the Perl and XS stash implementations
36 106 *dotop = \&_dotop;
37  
38  
39 #------------------------------------------------------------------------
40 # Virtual Methods
41 #
42 # If any of $ROOT_OPS, $SCALAR_OPS, $HASH_OPS or $LIST_OPS are already
43 # defined then we merge their contents with the default virtual methods
44 # define by Template::VMethods. Otherwise we can directly alias the
45 # corresponding Template::VMethod package vars.
46 #------------------------------------------------------------------------
47  
48 our $ROOT_OPS = defined $ROOT_OPS
49 0 ▶ ? { %{$Template::VMethods::ROOT_VMETHODS}, %$ROOT_OPS }
Branch: 1 branch
BranchTrueFalseTotal
defined $Template::Stash::ROOT_OPS ? : 0 106 106
50 : $Template::VMethods::ROOT_VMETHODS;
51  
52 our $SCALAR_OPS = defined $SCALAR_OPS
53 106 ▶ ? { %{$Template::VMethods::TEXT_VMETHODS}, %$SCALAR_OPS }
Branch: 1 branch
BranchTrueFalseTotal
defined $Template::Stash::SCALAR_OPS ? : 1 105 106
54 : $Template::VMethods::TEXT_VMETHODS;
55  
56 our $HASH_OPS = defined $HASH_OPS
57 0 ▶ ? { %{$Template::VMethods::HASH_VMETHODS}, %$HASH_OPS }
Branch: 1 branch
BranchTrueFalseTotal
defined $Template::Stash::HASH_OPS ? : 0 106 106
58 : $Template::VMethods::HASH_VMETHODS;
59  
60 our $LIST_OPS = defined $LIST_OPS
61 106 ▶ ? { %{$Template::VMethods::LIST_VMETHODS}, %$LIST_OPS }
Branch: 1 branch
BranchTrueFalseTotal
defined $Template::Stash::LIST_OPS ? : 1 105 106
62 : $Template::VMethods::LIST_VMETHODS;
63  
64  
65 #------------------------------------------------------------------------
66 # define_vmethod($type, $name, \&sub)
67 #
68 # Defines a virtual method of type $type (SCALAR, HASH, or LIST), with
69 # name $name, that invokes &sub when called. It is expected that &sub
70 # be able to handle the type that it will be called upon.
71 #------------------------------------------------------------------------
72  
73 15 ▶sub define_vmethod {
Subroutine define_vmethod: called CC=6 SCAR=17.9
Pod documented
74 15 my ($class, $type, $name, $sub) = @_;
75 15 my $op;
76 15 $type = lc $type;
77  
78 15 ▶ if ($type eq 'scalar' || $type eq 'item') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'scalar' or $type eq 'item') { } 4 11 15
elsif ($type eq 'hash') { } 4 7 11
elsif ($type eq 'list' or $type eq 'array') { } 6 1 7
Condition100%3/3
$type eq "scalar" or $type eq "item"
l!l&&r!l&&!r
2211
MC/DC100%2/2
$type eq "scalar" or $type eq "item"
$type eq "scalar" $type eq "item"
Truth table100%3/3
A or B
A= $type eq "scalar"B= $type eq "item"
ABresult
1X1
011
000
79 4 $op = $SCALAR_OPS;
80 }
81 elsif ($type eq 'hash') {
82 4 $op = $HASH_OPS;
83 }
84 ▶ elsif ($type eq 'list' || $type eq 'array') {
Condition100%3/3
$type eq "list" or $type eq "array"
l!l&&r!l&&!r
421
MC/DC100%2/2
$type eq "list" or $type eq "array"
$type eq "list" $type eq "array"
Truth table100%3/3
A or B
A= $type eq "list"B= $type eq "array"
ABresult
1X1
011
000
85 6 $op = $LIST_OPS;
86 }
87 else {
88 1 die "invalid vmethod type: $type\n";
89 }
90  
91 14 $op->{ $name } = $sub;
92  
93 14 return 1;
94 }
95  
96  
97 #========================================================================
98 # ----- CLASS METHODS -----
99 #========================================================================
100  
101 #------------------------------------------------------------------------
102 # new(\%params)
103 #
104 # Constructor method which creates a new Template::Stash object.
105 # An optional hash reference may be passed containing variable
106 # definitions that will be used to initialise the stash.
107 #
108 # Returns a reference to a newly created Template::Stash.
109 #------------------------------------------------------------------------
110  
111 293 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
112 293 my $class = shift;
113 293 ▶ my $params = ref $_[0] eq 'HASH' ? shift(@_) : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 293 0 293
114  
115 293 my $self = {
116 global => { },
117 %$params,
118 %$ROOT_OPS,
119 '_PARENT' => undef,
120 };
121  
122 293 bless $self, $class;
123 }
124  
125  
126 #========================================================================
127 # ----- PUBLIC OBJECT METHODS -----
128 #========================================================================
129  
130 #------------------------------------------------------------------------
131 # clone(\%params)
132 #
133 # Creates a copy of the current stash object to effect localisation
134 # of variables. The new stash is blessed into the same class as the
135 # parent (which may be a derived class) and has a '_PARENT' member added
136 # which contains a reference to the parent stash that created it
137 # ($self). This member is used in a successive declone() method call to
138 # return the reference to the parent.
139 #
140 # A parameter may be provided which should reference a hash of
141 # variable/values which should be defined in the new stash. The
142 # update() method is called to define these new variables in the cloned
143 # stash.
144 #
145 # Returns a reference to a cloned Template::Stash.
146 #------------------------------------------------------------------------
147  
148 1702 ▶sub clone {
Subroutine clone: called CC=4 SCAR=13.9
Pod documented
149 1702 my ($self, $params) = @_;
150 1702 ▶ $params ||= { };
Condition100%2/2
$params ||= {}
l!l
1567135
MC/DC100%1/1
$params ||= {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
151  
152 # look out for magical 'import' argument which imports another hash
153 1702 my $import = $params->{ import };
154 1702 ▶ if (ref $import eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $import eq 'HASH') { } 2 1700 1702
155 2 delete $params->{ import };
156 }
157 else {
158 1700 undef $import;
159 }
160  
161 1702 my $clone = bless {
162 %$self, # copy all parent members
163 %$params, # copy all new data
164 '_PARENT' => $self, # link to parent
165 }, ref $self;
166  
167 # perform hash import if defined
168 1702 ▶ &{ $HASH_OPS->{ import } }($clone, $import)
Branch: 1 branch
BranchTrueFalseTotal
if defined $import 2 1700 1702
169 if defined $import;
170  
171 1702 return $clone;
172 }
173  
174  
175 #------------------------------------------------------------------------
176 # declone($export)
177 #
178 # Returns a reference to the PARENT stash. When called in the following
179 # manner:
180 # $stash = $stash->declone();
181 # the reference count on the current stash will drop to 0 and be "freed"
182 # and the caller will be left with a reference to the parent. This
183 # contains the state of the stash before it was cloned.
184 #------------------------------------------------------------------------
185  
186 1698 ▶sub declone {
Subroutine declone: called CC=2 SCAR=6.9
Pod documented
187 1698 my $self = shift;
188 1698 ▶ $self->{ _PARENT } || $self;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_PARENT"} or $self 1697 1 1698
189 }
190  
191  
192 #------------------------------------------------------------------------
193 # get($ident)
194 #
195 # Returns the value for an variable stored in the stash. The variable
196 # may be specified as a simple string, e.g. 'foo', or as an array
197 # reference representing compound variables. In the latter case, each
198 # pair of successive elements in the list represent a node in the
199 # compound variable. The first is the variable name, the second a
200 # list reference of arguments or 0 if undefined. So, the compound
201 # variable [% foo.bar('foo').baz %] would be represented as the list
202 # [ 'foo', 0, 'bar', ['foo'], 'baz', 0 ]. Returns the value of the
203 # identifier or an empty string if undefined. Errors are thrown via
204 # die().
205 #------------------------------------------------------------------------
206  
207 667 ▶sub get {
Subroutine get: called CC=7 SCAR=19.5
Pod documented
208 667 my ($self, $ident, $args) = @_;
209 667 my ($root, $result);
210 667 $root = $self;
211  
212 667 ▶ if (ref $ident eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ident eq 'ARRAY' or (index($ident, '.') != -1) and $ident = [map({s/\(.*$//; $_, 0;} split(/\./, $ident, 0))]) { } 334 333 667
Condition100%5/5
ref $ident eq "ARRAY" or (index($ident, ".") != -1) and $ident = [map({s/\(.*$//; $_, 0;} split(/\./, $ident, 0))]
l!l&&r!l&&!r
3268333
(index($ident, ".") != -1) and $ident = [map({s/\(.*$//; $_, 0;} split(/\./, $ident, 0))]
!ll
3338
MC/DC100%2/2
ref $ident eq "ARRAY" or (index($ident, ".") != -1) and $ident = [map({s/\(.*$//; $_, 0;} split(/\./, $ident, 0))]
ref $ident eq "ARRAY" (index($ident, ".") != -1)
Truth table100%3/3
A or B
A= ref $ident eq "ARRAY"B= (index($ident, ".") != -1)
ABresult
1X1
011
000
213 || (index($ident,'.') > -1)
214 16 && ($ident = [ map { s/\(.*$//; ($_, 0) } split(/\./, $ident) ])) {
215 334 my $size = $#$ident;
216  
217 # if $ident is a list reference, then we evaluate each item in the
218 # identifier against the previous result, using the root stash
219 # ($self) as the first implicit 'result'...
220  
221 334 ▶ foreach (my $i = 0; $i <= $size; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $size) 737 325 1062
222 737 $result = $self->_dotop($root, @$ident[$i, $i+1]);
223 736 ▶ last unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 8 728 736
224 728 $root = $result;
225 }
226 }
227 else {
228 333 $result = $self->_dotop($root, $ident, $args);
229 }
230  
231 664 ▶ return $result // $self->undefined($ident, $args);
Condition66%2/3
$result // $self->undefined($ident, $args)
l!l&&r!l&&!r
4502140
MC/DC0%0/2
$result // $self->undefined($ident, $args)
$result $self->undefined($ident, $args)
Truth table66%2/3
A // B
A= $resultB= $self->undefined($ident, $args)
ABresult
1X1
011
000
232 }
233  
234  
235 #------------------------------------------------------------------------
236 # set($ident, $value, $default)
237 #
238 # Updates the value for a variable in the stash. The first parameter
239 # should be the variable name or array, as per get(). The second
240 # parameter should be the intended value for the variable. The third,
241 # optional parameter is a flag which may be set to indicate 'default'
242 # mode. When set true, the variable will only be updated if it is
243 # currently undefined or has a false value. The magical 'IMPORT'
244 # variable identifier may be used to indicate that $value is a hash
245 # reference whose values should be imported. Returns the value set,
246 # or an empty string if not set (e.g. default mode). In the case of
247 # IMPORT, returns the number of items imported from the hash.
248 #------------------------------------------------------------------------
249  
250 562 ▶sub set {
Subroutine set: called CC=7 SCAR=19.5
Pod documented
251 562 my ($self, $ident, $value, $default) = @_;
252 562 my ($root, $result, $error);
253  
254 562 $root = $self;
255  
256 ELEMENT: {
257 562 ▶ if (ref $ident eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ident eq 'ARRAY' or (index($ident, '.') != -1) and $ident = [map({s/\(.*$//; $_, 0;} split(/\./, $ident, 0))]) { } 14 548 562
Condition100%5/5
ref $ident eq "ARRAY" or (index($ident, ".") != -1) and $ident = [map({s/\(.*$//; $_, 0;} split(/\./, $ident, 0))]
l!l&&r!l&&!r
122548
(index($ident, ".") != -1) and $ident = [map({s/\(.*$//; $_, 0;} split(/\./, $ident, 0))]
!ll
5482
MC/DC100%2/2
ref $ident eq "ARRAY" or (index($ident, ".") != -1) and $ident = [map({s/\(.*$//; $_, 0;} split(/\./, $ident, 0))]
ref $ident eq "ARRAY" (index($ident, ".") != -1)
Truth table100%3/3
A or B
A= ref $ident eq "ARRAY"B= (index($ident, ".") != -1)
ABresult
1X1
011
000
258 || (index($ident,'.') != -1) # has a '.' in it somewhere
259 && ($ident = [
260 4 map { s/\(.*$//; ($_, 0) }
261 split(/\./, $ident)
262 ])) {
263  
264 # a compound identifier may contain multiple elements (e.g.
265 # foo.bar.baz) and we must first resolve all but the last,
266 # using _dotop() with the $lvalue flag set which will create
267 # intermediate hashes if necessary...
268 14 my $size = $#$ident;
269 14 ▶ foreach (my $i = 0; $i < $size - 2; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size - 2) 16 14 30
270 16 $result = $self->_dotop($root, @$ident[$i, $i+1], 1);
271 16 ▶ last ELEMENT unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 16 16
272 16 $root = $result;
273 }
274  
275 # then we call _assign() to assign the value to the last element
276 14 $result = $self->_assign(
277 $root, @$ident[$size-1, $size],
278 $value, $default
279 );
280 }
281 else {
282 548 $result = $self->_assign($root, $ident, 0, $value, $default);
283 }
284 }
285  
286 562 ▶ return $result // '';
Condition100%2/2
$result // ""
l!l
5602
MC/DC0%0/1
$result // ""
$result
Truth table50%1/2
A
A= $result
Aresult
11
00
287 }
288  
289  
290 #------------------------------------------------------------------------
291 # getref($ident)
292 #
293 # Returns a "reference" to a particular item. This is represented as a
294 # closure which will return the actual stash item when called.
295 #------------------------------------------------------------------------
296  
297 6 ▶sub getref {
Subroutine getref: called CC=6 SCAR=18.2
Pod documented
298 6 my ($self, $ident, $args) = @_;
299 6 my ($root, $item, $result);
300 6 $root = $self;
301  
302 6 ▶ if (ref $ident eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ident eq 'ARRAY') { } 3 3 6
303 3 my $size = $#$ident;
304  
305 3 ▶ foreach (my $i = 0; $i <= $size; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $size) 6 0 6
306 6 ($item, $args) = @$ident[$i, $i + 1];
307 6 ▶ last if $i >= $size - 2; # don't evaluate last node
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $size - 2 3 3 6
308 last unless defined
309 3 ▶ ($root = $self->_dotop($root, $item, $args));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($root = $self->_dotop($root, $item, $args)) 0 3 3
310 }
311 }
312 else {
313 3 $item = $ident;
314 }
315  
316 6 ▶ if (defined $root) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $root) { } 6 0 6
317 11 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
318 11 ▶ my @args = (@{$args||[]}, @_);
Condition100%2/2
$args || []
l!l
38
MC/DC100%1/1
$args || []
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
319 11 $self->_dotop($root, $item, \@args);
320 }
321 6 }
322 else {
323 0 ▶ return sub { '' };
Subroutine __ANON__: not called CC=1 SCAR=6.9
324 }
325 }
326  
327  
328  
329  
330 #------------------------------------------------------------------------
331 # update(\%params)
332 #
333 # Update multiple variables en masse. No magic is performed. Simple
334 # variable names only.
335 #------------------------------------------------------------------------
336  
337 1499 ▶sub update {
Subroutine update: called CC=2 SCAR=6.9
Pod documented
338 1499 my ($self, $params) = @_;
339  
340 # look out for magical 'import' argument to import another hash
341 1499 my $import = $params->{ import };
342 1499 ▶ if (ref $import eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $import eq "HASH") 1 1498 1499
343 1 @$self{ keys %$import } = values %$import;
344 1 delete $params->{ import };
345 }
346  
347 1499 @$self{ keys %$params } = values %$params;
348 }
349  
350  
351 #------------------------------------------------------------------------
352 # undefined($ident, $args)
353 #
354 # Method called when a get() returns an undefined value. Can be redefined
355 # in a subclass to implement alternate handling.
356 #------------------------------------------------------------------------
357  
358 1730 ▶sub undefined {
Subroutine undefined: called CC=6 SCAR=18.0
Pod documented
359 1730 my ($self, $ident, $args) = @_;
360  
361 1730 ▶ if ($self->{ _STRICT }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_STRICT'}) { } 26 1704 1730
362 # .defined must work on undefined values even in STRICT mode —
363 # it would be nonsensical to throw "undefined variable" when the
364 # user is explicitly testing whether a variable is defined (GH #170)
365 26 ▶ if (ref $ident eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ident eq "ARRAY") 8 18 26
366 8 my $last_item = $ident->[$#$ident - 1];
367 8 ▶ return '' if defined $last_item && $last_item eq 'defined';
Branch: 1 branch
BranchTrueFalseTotal
if defined $last_item and $last_item eq "defined" 4 4 8
Condition66%2/3
defined $last_item and $last_item eq "defined"
!ll&&!rl&&r
044
MC/DC50%1/2
defined $last_item and $last_item eq "defined"
defined $last_item $last_item eq "defined"
Truth table66%2/3
A and B
A= defined $last_itemB= $last_item eq "defined"
ABresult
0X0
100
111
368 }
369  
370 # Sorry, but we can't provide a sensible source file and line without
371 # re-designing the whole architecture of TT (see TT3)
372 die Template::Exception->new(
373 $UNDEF_TYPE,
374 sprintf(
375 $UNDEF_INFO,
376 $self->_reconstruct_ident($ident)
377 )
378 22 ▶ ) if $self->{ _STRICT };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_STRICT"} 22 0 22
379 }
380 else {
381 # There was a time when I thought this was a good idea. But it's not.
382 1704 return '';
383 }
384 }
385  
386 22 ▶sub _reconstruct_ident {
Subroutine _reconstruct_ident: called CC=7 SCAR=19.5
387 22 my ($self, $ident) = @_;
388 22 my ($name, $args, @output);
389 22 ▶ my @input = ref $ident eq 'ARRAY' ? @$ident : ($ident);
Branch: 1 branch
BranchTrueFalseTotal
ref $ident eq 'ARRAY' ? : 4 18 22
390  
391 22 ▶ while (@input) {
Branch: 1 branch
BranchTrueFalseTotal
if (@input) 26 22 48
392 26 $name = shift @input;
393 26 ▶ $args = shift @input || 0;
Condition100%2/2
shift @input || 0
l!l
224
MC/DC100%1/1
shift @input || 0
shift @input
Truth table100%2/2
A
A= shift @input
Aresult
11
00
394 26 ▶ $name .= '(' . join(', ', map { /^\d+$/ ? $_ : "'$_'" } @$args) . ')'
Branch: 2 branches
BranchTrueFalseTotal
if $args and ref $args eq "ARRAY" 2 24 26
/^\d+$/ ? : 2 2 4
Condition66%2/3
$args and ref $args eq "ARRAY"
!ll&&!rl&&r
2402
MC/DC50%1/2
$args and ref $args eq "ARRAY"
$args ref $args eq "ARRAY"
Truth table66%2/3
A and B
A= $argsB= ref $args eq "ARRAY"
ABresult
0X0
100
111
395 if $args && ref $args eq 'ARRAY';
396 26 push(@output, $name);
397 }
398  
399 22 return join('.', @output);
400 }
401  
402  
403 #========================================================================
404 # ----- PRIVATE OBJECT METHODS -----
405 #========================================================================
406  
407 #------------------------------------------------------------------------
408 # _dotop($root, $item, \@args, $lvalue)
409 #
410 # This is the core 'dot' operation method which evaluates elements of
411 # variables against their root. All variables have an implicit root
412 # which is the stash object itself (a hash). Thus, a non-compound
413 # variable 'foo' is actually '(stash.)foo', the compound 'foo.bar' is
414 # '(stash.)foo.bar'. The first parameter is a reference to the current
415 # root, initially the stash itself. The second parameter contains the
416 # name of the variable element, e.g. 'foo'. The third optional
417 # parameter is a reference to a list of any parenthesised arguments
418 # specified for the variable, which are passed to sub-routines, object
419 # methods, etc. The final parameter is an optional flag to indicate
420 # if this variable is being evaluated on the left side of an assignment
421 # (e.g. foo.bar.baz = 10). When set true, intermediated hashes will
422 # be created (e.g. bar) if necessary.
423 #
424 # Returns the result of evaluating the item against the root, having
425 # performed any variable "magic". The value returned can then be used
426 # as the root of the next _dotop() in a compound sequence. Returns
427 # undef if the variable is undefined.
428 #------------------------------------------------------------------------
429  
430 1118 ▶sub _dotop {
Subroutine _dotop: called CC=50 SCAR=45.7
431 1118 my ($self, $root, $item, $args, $lvalue) = @_;
432 1118 my $rootref = ref $root;
433 1118 ▶ my $atroot = (blessed $root && $root->isa(ref $self));
Condition100%3/3
builtin::blessed($root) && $root->isa(ref $self)
!ll&&!rl&&r
38139698
MC/DC100%2/2
builtin::blessed($root) && $root->isa(ref $self)
builtin::blessed($root) $root->isa(ref $self)
Truth table100%3/3
A && B
A= builtin::blessed($root)B= $root->isa(ref $self)
ABresult
0X0
100
111
434 1118 my ($value, @result);
435  
436 1118 ▶ $args ||= [ ];
Condition100%2/2
$args ||= []
l!l
202916
MC/DC100%1/1
$args ||= []
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
437 1118 ▶ $lvalue ||= 0;
Condition100%2/2
$lvalue ||= 0
l!l
161102
MC/DC100%1/1
$lvalue ||= 0
$lvalue
Truth table100%2/2
A
A= $lvalue
Aresult
11
00
438  
439 # print STDERR "_dotop(root=$root, item=$item, args=[@$args])\n"
440 # if $DEBUG;
441  
442 # return undef without an error if either side of the dot is unviable
443 1118 ▶ return undef unless defined($root) and defined($item);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root and defined $item 0 1118 1118
Condition33%1/3
defined $root and defined $item
!ll&&!rl&&r
001118
MC/DC0%0/2
defined $root and defined $item
defined $root defined $item
Truth table33%1/3
A and B
A= defined $rootB= defined $item
ABresult
0X0
100
111
444  
445 # or if an attempt is made to access a private member, starting _ or .
446 1118 ▶ return undef if $PRIVATE && $item =~ /$PRIVATE/;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::Stash::PRIVATE and $item =~ /$Template::Stash::PRIVATE/ 0 1118 1118
Condition33%1/3
$Template::Stash::PRIVATE and $item =~ /$Template::Stash::PRIVATE/
!ll&&!rl&&r
011180
MC/DC0%0/2
$Template::Stash::PRIVATE and $item =~ /$Template::Stash::PRIVATE/
$Template::Stash::PRIVATE $item =~ /$Template::Stash::PRIVATE/
Truth table33%1/3
A and B
A= $Template::Stash::PRIVATEB= $item =~ /$Template::Stash::PRIVATE/
ABresult
0X0
100
111
447  
448 1118 ▶ if ($atroot || $rootref eq 'HASH') {
Branch: 6 branches
BranchTrueFalseTotal
if ($atroot or $rootref eq 'HASH') { } 810 308 1118
elsif ($rootref eq 'ARRAY') { } 168 140 308
elsif (builtin::blessed($root) and $root->can('can')) { } 39 101 140
elsif ($value = $Template::Stash::SCALAR_OPS->{$item} and not $lvalue) { } 100 1 101
elsif ($value = $Template::Stash::LIST_OPS->{$item} and not $lvalue) { } 1 0 1
elsif ($self->{'_DEBUG'}) { } 0 0 0
Condition100%3/3
$atroot or $rootref eq "HASH"
l!l&&r!l&&!r
698112308
MC/DC100%2/2
$atroot or $rootref eq "HASH"
$atroot $rootref eq "HASH"
Truth table100%3/3
A or B
A= $atrootB= $rootref eq "HASH"
ABresult
1X1
011
000
449 # if $root is a regular HASH or a Template::Stash kinda HASH (the
450 # *real* root of everything). We first lookup the named key
451 # in the hash, or create an empty hash in its place if undefined
452 # and the $lvalue flag is set. Otherwise, we check the HASH_OPS
453 # pseudo-methods table, calling the code if found, or return undef.
454  
455 810 ▶ if (defined($value = $root->{ $item })) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined($value = $root->{$item})) { } 547 263 810
elsif ($lvalue) { } 2 261 263
elsif ($value = $Template::Stash::HASH_OPS->{$item} and !$atroot || $item eq 'import') { } 43 218 261
elsif (ref $item eq 'ARRAY') { } 2 216 218
456 547 ▶ return $value unless ref $value eq 'CODE'; ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "CODE" 530 17 547
457 17 @result = &$value(@$args); ## @result
458 }
459 elsif ($lvalue) {
460 # we create an intermediate hash if this is an lvalue
461 2 return $root->{ $item } = { }; ## RETURN
462 }
463 # ugly hack: only allow import vmeth to be called on root stash
464 ▶ elsif (($value = $HASH_OPS->{ $item })
Condition100%6/6
$value = $Template::Stash::HASH_OPS->{$item} and !$atroot || $item eq "import"
!ll&&!rl&&r
217143
not $atroot or $item eq "import"
l!l&&r!l&&!r
33101
MC/DC100%3/3
$value = $Template::Stash::HASH_OPS->{$item} and !$atroot || $item eq "import"
$value = $Template::Stash::HASH_OPS->{$item} not $atroot $item eq "import"
Truth table100%4/4
A and not(B) or C
A= $value = $Template::Stash::HASH_OPS->{$item}B= not $atrootC= $item eq "import"
ABCresult
0XX0
1000
11X1
1011
465 && (! $atroot || $item eq 'import')) {
466 43 @result = &$value($root, @$args); ## @result
467 }
468 elsif ( ref $item eq 'ARRAY' ) {
469 # hash slice
470 2 return [@$root{@$item}]; ## RETURN
471 }
472 }
473 elsif ($rootref eq 'ARRAY') {
474 # if root is an ARRAY then we check for a LIST_OPS pseudo-method
475 # or return the numerical index into the array, or undef
476 168 ▶ if ($value = $LIST_OPS->{ $item }) {
Branch: 3 branches
BranchTrueFalseTotal
if ($value = $Template::Stash::LIST_OPS->{$item}) { } 151 17 168
elsif ($item =~ /^-?\d+$/) { } 16 1 17
elsif (ref $item eq 'ARRAY') { } 1 0 1
477 151 @result = &$value($root, @$args); ## @result
478 }
479 elsif ($item =~ /^-?\d+$/) {
480 16 $value = $root->[$item];
481 16 ▶ return $value unless ref $value eq 'CODE'; ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "CODE" 16 0 16
482 0 @result = &$value(@$args); ## @result
483 }
484 elsif ( ref $item eq 'ARRAY' ) {
485 # array slice
486 1 return [@$root[@$item]]; ## RETURN
487 }
488 }
489  
490 # NOTE: we do the can-can because UNIVSERAL::isa($something, 'UNIVERSAL')
491 # doesn't appear to work with CGI, returning true for the first call
492 # and false for all subsequent calls.
493  
494 # UPDATE: that doesn't appear to be the case any more
495  
496 ▶ elsif (blessed($root) && $root->can('can')) {
Condition66%2/3
builtin::blessed($root) and $root->can("can")
!ll&&!rl&&r
101039
MC/DC50%1/2
builtin::blessed($root) and $root->can("can")
builtin::blessed($root) $root->can("can")
Truth table66%2/3
A and B
A= builtin::blessed($root)B= $root->can("can")
ABresult
0X0
100
111
497  
498 # if $root is a blessed reference (i.e. inherits from the
499 # UNIVERSAL object base class) then we call the item as a method.
500 # If that fails then we try to fallback on HASH behaviour if
501 # possible.
502 39 eval { @result = $root->$item(@$args); };
503  
504 39 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 13 26 39
505 # temporary hack - required to propagate errors thrown
506 # by views; if $@ is a ref (e.g. Template::Exception
507 # object then we assume it's a real error that needs
508 # real throwing
509  
510 13 ▶ my $class = ref($root) || $root;
Condition33%1/3
ref $root || $root
l!l&&r!l&&!r
1300
MC/DC0%0/2
ref $root || $root
ref $root $root
Truth table33%1/3
A || B
A= ref $rootB= $root
ABresult
1X1
011
000
511  
512 # Fail only if the function exists
513 13 ▶ die $@ if ( ref($@) || $root->can($item) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ or $root->can($item) 1 12 13
Condition66%2/3
ref $@ or $root->can($item)
l!l&&r!l&&!r
0112
MC/DC50%1/2
ref $@ or $root->can($item)
ref $@ $root->can($item)
Truth table66%2/3
A or B
A= ref $@B= $root->can($item)
ABresult
1X1
011
000
514  
515 # failed to call object method, so try some fallbacks
516 12 ▶ if (reftype $root eq 'HASH') {
Branch: 5 branches
BranchTrueFalseTotal
if (builtin::reftype($root) eq 'HASH') { } 7 5 12
elsif (builtin::reftype($root) eq 'ARRAY') { } 5 0 5
elsif ($value = $Template::Stash::SCALAR_OPS->{$item}) { } 0 0 0
elsif ($value = $Template::Stash::LIST_OPS->{$item}) { } 0 0 0
elsif ($self->{'_DEBUG'}) { } 0 0 0
517 7 ▶ if( defined($value = $root->{ $item })) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined($value = $root->{$item})) { } 3 4 7
elsif ($value = $Template::Stash::HASH_OPS->{$item}) { } 2 2 4
elsif ($value = $Template::Stash::LIST_OPS->{$item}) { } 1 1 2
518 3 ▶ return $value unless ref $value eq 'CODE'; ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "CODE" 3 0 3
519 0 @result = &$value(@$args);
520 }
521 elsif ($value = $HASH_OPS->{ $item }) {
522 2 @result = &$value($root, @$args);
523 }
524 elsif ($value = $LIST_OPS->{ $item }) {
525 1 @result = &$value([$root], @$args);
526 }
527 }
528 elsif (reftype $root eq 'ARRAY') {
529 5 ▶ if( $value = $LIST_OPS->{ $item }) {
Branch: 3 branches
BranchTrueFalseTotal
if ($value = $Template::Stash::LIST_OPS->{$item}) { } 3 2 5
elsif ($item =~ /^-?\d+$/) { } 2 0 2
elsif (ref $item eq 'ARRAY') { } 0 0 0
530 3 @result = &$value($root, @$args);
531 }
532 elsif( $item =~ /^-?\d+$/ ) {
533 2 $value = $root->[$item];
534 2 ▶ return $value unless ref $value eq 'CODE'; ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "CODE" 2 0 2
535 0 @result = &$value(@$args); ## @result
536 }
537 elsif ( ref $item eq 'ARRAY' ) {
538 # array slice
539 0 return [@$root[@$item]]; ## RETURN
540 }
541 }
542 elsif ($value = $SCALAR_OPS->{ $item }) {
543 0 @result = &$value($root, @$args);
544 }
545 elsif ($value = $LIST_OPS->{ $item }) {
546 0 @result = &$value([$root], @$args);
547 }
548 elsif ($self->{ _DEBUG }) {
549 0 @result = (undef, $@);
550 }
551 }
552 }
553 ▶ elsif (($value = $SCALAR_OPS->{ $item }) && ! $lvalue) {
Condition66%2/3
$value = $Template::Stash::SCALAR_OPS->{$item} and not $lvalue
!ll&&!rl&&r
10100
MC/DC50%1/2
$value = $Template::Stash::SCALAR_OPS->{$item} and not $lvalue
$value = $Template::Stash::SCALAR_OPS->{$item} not $lvalue
Truth table66%2/3
A and not(B)
A= $value = $Template::Stash::SCALAR_OPS->{$item}B= not $lvalue
ABresult
0X0
100
111
554 # at this point, it doesn't look like we've got a reference to
555 # anything we know about, so we try the SCALAR_OPS pseudo-methods
556 # table (but not for l-values)
557 100 @result = &$value($root, @$args); ## @result
558 }
559 ▶ elsif (($value = $LIST_OPS->{ $item }) && ! $lvalue) {
Condition33%1/3
$value = $Template::Stash::LIST_OPS->{$item} and not $lvalue
!ll&&!rl&&r
001
MC/DC0%0/2
$value = $Template::Stash::LIST_OPS->{$item} and not $lvalue
$value = $Template::Stash::LIST_OPS->{$item} not $lvalue
Truth table33%1/3
A and not(B)
A= $value = $Template::Stash::LIST_OPS->{$item}B= not $lvalue
ABresult
0X0
100
111
560 # last-ditch: can we promote a scalar to a one-element
561 # list and apply a LIST_OPS virtual method?
562 1 @result = &$value([$root], @$args);
563 }
564 elsif ($self->{ _DEBUG }) {
565 0 die "don't know how to access [ $root ].$item\n"; ## DIE
566 }
567 else {
568 0 @result = ();
569 }
570  
571 # fold multiple return items into a list unless first item is undef
572 561 ▶ if (defined $result[0]) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $result[0]) { } 337 224 561
elsif (defined $result[1]) { } 0 224 224
elsif ($self->{'_DEBUG'}) { } 2 222 224
573 return ## RETURN
574 337 ▶ scalar @result > 1 ? [ @result ] : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
scalar @result > 1 ? : 0 337 337
575 }
576 elsif (defined $result[1]) {
577 0 die $result[1]; ## DIE
578 }
579 elsif ($self->{ _DEBUG }) {
580 2 die "$item is undefined\n"; ## DIE
581 }
582  
583 222 return undef;
584 }
585  
586  
587 #------------------------------------------------------------------------
588 # _assign($root, $item, \@args, $value, $default)
589 #
590 # Similar to _dotop() above, but assigns a value to the given variable
591 # instead of simply returning it. The first three parameters are the
592 # root item, the item and arguments, as per _dotop(), followed by the
593 # value to which the variable should be set and an optional $default
594 # flag. If set true, the variable will only be set if currently false
595 # (undefined/zero)
596 #------------------------------------------------------------------------
597  
598 562 ▶sub _assign {
Subroutine _assign: called CC=19 SCAR=37.3
599 562 my ($self, $root, $item, $args, $value, $default) = @_;
600 562 my $rootref = ref $root;
601 562 ▶ my $atroot = (blessed $root && $root->isa(ref $self));
Condition66%2/3
builtin::blessed($root) && $root->isa(ref $self)
!ll&&!rl&&r
120550
MC/DC50%1/2
builtin::blessed($root) && $root->isa(ref $self)
builtin::blessed($root) $root->isa(ref $self)
Truth table66%2/3
A && B
A= builtin::blessed($root)B= $root->isa(ref $self)
ABresult
0X0
100
111
602 562 my $result;
603 562 ▶ $args ||= [ ];
Condition50%1/2
$args ||= []
l!l
0562
MC/DC0%0/1
$args ||= []
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
604 562 ▶ $default ||= 0;
Condition100%2/2
$default ||= 0
l!l
2560
MC/DC100%1/1
$default ||= 0
$default
Truth table100%2/2
A
A= $default
Aresult
11
00
605  
606 # return undef without an error if either side of the dot is unviable
607 562 ▶ return undef unless $root and defined $item;
Branch: 1 branch
BranchTrueFalseTotal
unless $root and defined $item 0 562 562
Condition33%1/3
$root and defined $item
!ll&&!rl&&r
00562
MC/DC0%0/2
$root and defined $item
$root defined $item
Truth table33%1/3
A and B
A= $rootB= defined $item
ABresult
0X0
100
111
608  
609 # or if an attempt is made to update a private member, starting _ or .
610 562 ▶ return undef if $PRIVATE && $item =~ /$PRIVATE/;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::Stash::PRIVATE and $item =~ /$Template::Stash::PRIVATE/ 0 562 562
Condition33%1/3
$Template::Stash::PRIVATE and $item =~ /$Template::Stash::PRIVATE/
!ll&&!rl&&r
05620
MC/DC0%0/2
$Template::Stash::PRIVATE and $item =~ /$Template::Stash::PRIVATE/
$Template::Stash::PRIVATE $item =~ /$Template::Stash::PRIVATE/
Truth table33%1/3
A and B
A= $Template::Stash::PRIVATEB= $item =~ /$Template::Stash::PRIVATE/
ABresult
0X0
100
111
611  
612 562 ▶ if ($rootref eq 'HASH' || $atroot) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rootref eq 'HASH' or $atroot) { } 559 3 562
elsif ($rootref eq 'ARRAY' and $item =~ /^-?\d+$/) { } 3 0 3
elsif (builtin::blessed($root)) { } 0 0 0
Condition100%3/3
$rootref eq "HASH" or $atroot
l!l&&r!l&&!r
95503
MC/DC100%2/2
$rootref eq "HASH" or $atroot
$rootref eq "HASH" $atroot
Truth table100%3/3
A or B
A= $rootref eq "HASH"B= $atroot
ABresult
1X1
011
000
613 # if the root is a hash we set the named key
614 return ($root->{ $item } = $value) ## RETURN
615 559 ▶ unless $default && $root->{ $item };
Branch: 1 branch
BranchTrueFalseTotal
unless $default and $root->{$item} 559 0 559
Condition33%1/3
$default and $root->{$item}
!ll&&!rl&&r
55900
MC/DC0%0/2
$default and $root->{$item}
$default $root->{$item}
Truth table33%1/3
A and B
A= $defaultB= $root->{$item}
ABresult
0X0
100
111
616 }
617 ▶ elsif ($rootref eq 'ARRAY' && $item =~ /^-?\d+$/) {
Condition33%1/3
$rootref eq "ARRAY" and $item =~ /^-?\d+$/
!ll&&!rl&&r
003
MC/DC0%0/2
$rootref eq "ARRAY" and $item =~ /^-?\d+$/
$rootref eq "ARRAY" $item =~ /^-?\d+$/
Truth table33%1/3
A and B
A= $rootref eq "ARRAY"B= $item =~ /^-?\d+$/
ABresult
0X0
100
111
618 # or set a list item by index number
619 3 ▶ return ($root->[$item] = $value) ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
unless $default and $root->[$item] 2 1 3
Condition100%3/3
$default and $root->[$item]
!ll&&!rl&&r
111
MC/DC100%2/2
$default and $root->[$item]
$default $root->[$item]
Truth table100%3/3
A and B
A= $defaultB= $root->[$item]
ABresult
0X0
100
111
620 unless $default && $root->[$item];
621 }
622 elsif (blessed($root)) {
623 # try to call the item as a method of an object
624  
625 0 ▶ return $root->$item(@$args, $value) ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
unless $default and $root->$item 0 0 0
Condition0%0/3
$default and $root->$item
!ll&&!rl&&r
000
MC/DC0%0/2
$default and $root->$item
$default $root->$item
Truth table0%0/3
A and B
A= $defaultB= $root->$item
ABresult
0X0
100
111
626 unless $default && $root->$item();
627  
628 # 2 issues:
629 # - method call should be wrapped in eval { }
630 # - fallback on hash methods if object method not found
631 #
632 # eval { $result = $root->$item(@$args, $value); };
633 #
634 # if ($@) {
635 # die $@ if ref($@) || ($@ !~ /Can't locate object method/);
636 #
637 # # failed to call object method, so try some fallbacks
638 # if (UNIVERSAL::isa($root, 'HASH') && exists $root->{ $item }) {
639 # $result = ($root->{ $item } = $value)
640 # unless $default && $root->{ $item };
641 # }
642 # }
643 # return $result; ## RETURN
644 }
645 else {
646 0 die "don't know how to assign to [$root].[$item]\n"; ## DIE
647 }
648  
649 1 return undef;
650 }
651  
652 106 1;
653  
654 __END__
« blib/lib/Template/Stash/Context.pm ↑ Summary lib/Template/Test.pm »