blib/lib/Template/Stash/Context.pm

filter: Statementstmt 66.0% 109 / 165 Branchbran 45.2% 67 / 148 Conditioncond 44.3% 39 / 88 MC/DCmcdc 22.4% 11 / 49 Subroutinesub 70.5% 12 / 17 Podpod 100% 7 / 7 totaltotal 51.6% 245 / 474 CC 103 SCAR 57.5
CC
103
Cov
54%
_dotop
665.5
_assign
65.7
getref
42.0
CRAP 314.7
« blib/lib/Template/Parser.pm ↑ Summary blib/lib/Template/Test.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Stash::Context
4 #
5 # DESCRIPTION
6 # This is an alternate stash object which includes a patch from
7 # Craig Barratt to implement various new virtual methods to allow
8 # dotted template variable to denote if object methods and subroutines
9 # should be called in scalar or list context. It adds a little overhead
10 # to each stash call and I'm a little wary of doing that. So for now,
11 # it's implemented as a separate stash module which will allow us to
12 # test it out, benchmark it and switch it in or out as we require.
13 #
14 # This is what Craig has to say about it:
15 #
16 # Here's a better set of features for the core. Attached is a new version
17 # of Stash.pm (based on TT2.02) that:
18 #
19 # - supports the special op "scalar" that forces scalar context on
20 # function calls, eg:
21 #
22 # cgi.param("foo").scalar
23 #
24 # calls cgi.param("foo") in scalar context (unlike my wimpy
25 # scalar op from last night). Array context is the default.
26 #
27 # With non-function operands, scalar behaves like the perl
28 # version (eg: no-op for scalar, size for arrays, etc).
29 #
30 # - supports the special op "ref" that behaves like the perl ref.
31 # If applied to a function the function is not called. Eg:
32 #
33 # cgi.param("foo").ref
34 #
35 # does *not* call cgi.param and evaluates to "CODE". Similarly,
36 # HASH.ref, ARRAY.ref return what you expect.
37 #
38 # - adds a new scalar and list op called "array" that is a no-op for
39 # arrays and promotes scalars to one-element arrays.
40 #
41 # - allows scalar ops to be applied to arrays and hashes in place,
42 # eg: ARRAY.repeat(3) repeats each element in place.
43 #
44 # - allows list ops to be applied to scalars by promoting the scalars
45 # to one-element arrays (like an implicit "array"). So you can
46 # do things like SCALAR.size, SCALAR.join and get a useful result.
47 #
48 # This also means you can now use x.0 to safely get the first element
49 # whether x is an array or scalar.
50 #
51 # The new Stash.pm passes the TT2.02 test suite. But I haven't tested the
52 # new features very much. One nagging implementation problem is that the
53 # "scalar" and "ref" ops have higher precedence than user variable names.
54 #
55 # AUTHORS
56 # Andy Wardley <abw@kfs.org>
57 # Craig Barratt <craig@arraycomm.com>
58 #
59 # COPYRIGHT
60 # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved.
61 # Copyright (C) 1998-2001 Canon Research Centre Europe Ltd.
62 #
63 # This module is free software; you can redistribute it and/or
64 # modify it under the same terms as Perl itself.
65 #
66 #============================================================================
67  
68 package Template::Stash::Context;
69  
70 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 1 ▶use base 'Template::Stash';
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 1 our $VERSION = '3.100';
75 1 ▶our $DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::Stash::Context::DEBUG 1 0 1
76  
77  
78 #========================================================================
79 # -- PACKAGE VARIABLES AND SUBS --
80 #========================================================================
81  
82 #------------------------------------------------------------------------
83 # copy virtual methods from those in the regular Template::Stash
84 #------------------------------------------------------------------------
85  
86 1 ▶our $ROOT_OPS = {
Branch: 1 branch
BranchTrueFalseTotal
defined $Template::Stash::Context::ROOT_OPS ? : 0 1 1
87 %$Template::Stash::ROOT_OPS,
88 defined $ROOT_OPS ? %$ROOT_OPS : (),
89 };
90  
91 our $SCALAR_OPS = {
92 %$Template::Stash::SCALAR_OPS,
93 0 ▶ 'array' => sub { return [$_[0]] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
94 1 ▶ defined $SCALAR_OPS ? %$SCALAR_OPS : (),
Branch: 1 branch
BranchTrueFalseTotal
defined $Template::Stash::Context::SCALAR_OPS ? : 0 1 1
95 };
96  
97 our $LIST_OPS = {
98 %$Template::Stash::LIST_OPS,
99 0 ▶ 'array' => sub { return $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 1 ▶ defined $LIST_OPS ? %$LIST_OPS : (),
Branch: 1 branch
BranchTrueFalseTotal
defined $Template::Stash::Context::LIST_OPS ? : 0 1 1
101 };
102  
103 1 ▶our $HASH_OPS = {
Branch: 1 branch
BranchTrueFalseTotal
defined $Template::Stash::Context::HASH_OPS ? : 0 1 1
104 %$Template::Stash::HASH_OPS,
105 defined $HASH_OPS ? %$HASH_OPS : (),
106 };
107  
108  
109  
110 #========================================================================
111 # ----- CLASS METHODS -----
112 #========================================================================
113  
114 #------------------------------------------------------------------------
115 # new(\%params)
116 #
117 # Constructor method which creates a new Template::Stash object.
118 # An optional hash reference may be passed containing variable
119 # definitions that will be used to initialise the stash.
120 #
121 # Returns a reference to a newly created Template::Stash.
122 #------------------------------------------------------------------------
123  
124 sub new {
125 1 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
126 1 ▶ my $params = ref $_[0] eq 'HASH' ? shift(@_) : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 1 0 1
127  
128 1 my $self = {
129 global => { },
130 %$params,
131 %$ROOT_OPS,
132 '_PARENT' => undef,
133 '_CLASS' => $class,
134 };
135  
136 1 bless $self, $class;
137 }
138  
139  
140 #========================================================================
141 # ----- PUBLIC OBJECT METHODS -----
142 #========================================================================
143  
144 #------------------------------------------------------------------------
145 # clone(\%params)
146 #
147 # Creates a copy of the current stash object to effect localisation
148 # of variables. The new stash is blessed into the same class as the
149 # parent (which may be a derived class) and has a '_PARENT' member added
150 # which contains a reference to the parent stash that created it
151 # ($self). This member is used in a successive declone() method call to
152 # return the reference to the parent.
153 #
154 # A parameter may be provided which should reference a hash of
155 # variable/values which should be defined in the new stash. The
156 # update() method is called to define these new variables in the cloned
157 # stash.
158 #
159 # Returns a reference to a cloned Template::Stash.
160 #------------------------------------------------------------------------
161  
162 sub clone {
163 3 ▶ my ($self, $params) = @_;
Subroutine clone: called CC=5 SCAR=18.3
Pod documented
164 3 ▶ $params ||= { };
Condition50%1/2
$params ||= {}
l!l
30
MC/DC0%0/1
$params ||= {}
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
165  
166 # look out for magical 'import' argument which imports another hash
167 3 my $import = $params->{ import };
168 3 ▶ if (defined $import && UNIVERSAL::isa($import, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $import and UNIVERSAL::isa($import, 'HASH')) { } 0 3 3
Condition33%1/3
defined $import and UNIVERSAL::isa($import, "HASH")
!ll&&!rl&&r
300
MC/DC0%0/2
defined $import and UNIVERSAL::isa($import, "HASH")
defined $import UNIVERSAL::isa($import, "HASH")
Truth table33%1/3
A and B
A= defined $importB= UNIVERSAL::isa($import, "HASH")
ABresult
0X0
100
111
169 0 delete $params->{ import };
170 }
171 else {
172 3 undef $import;
173 }
174  
175 3 my $clone = bless {
176 %$self, # copy all parent members
177 %$params, # copy all new data
178 '_PARENT' => $self, # link to parent
179 }, ref $self;
180  
181 # perform hash import if defined
182 3 ▶ &{ $HASH_OPS->{ import }}($clone, $import)
Branch: 1 branch
BranchTrueFalseTotal
if defined $import 0 3 3
183 if defined $import;
184  
185 3 return $clone;
186 }
187  
188  
189 #------------------------------------------------------------------------
190 # declone($export)
191 #
192 # Returns a reference to the PARENT stash. When called in the following
193 # manner:
194 # $stash = $stash->declone();
195 # the reference count on the current stash will drop to 0 and be "freed"
196 # and the caller will be left with a reference to the parent. This
197 # contains the state of the stash before it was cloned.
198 #------------------------------------------------------------------------
199  
200 sub declone {
201 3 ▶ my $self = shift;
Subroutine declone: called CC=2 SCAR=7.2
Pod documented
202 3 ▶ $self->{ _PARENT } || $self;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_PARENT"} or $self 0 3 3
203 }
204  
205  
206 #------------------------------------------------------------------------
207 # get($ident)
208 #
209 # Returns the value for an variable stored in the stash. The variable
210 # may be specified as a simple string, e.g. 'foo', or as an array
211 # reference representing compound variables. In the latter case, each
212 # pair of successive elements in the list represent a node in the
213 # compound variable. The first is the variable name, the second a
214 # list reference of arguments or 0 if undefined. So, the compound
215 # variable [% foo.bar('foo').baz %] would be represented as the list
216 # [ 'foo', 0, 'bar', ['foo'], 'baz', 0 ]. Returns the value of the
217 # identifier or an empty string if undefined. Errors are thrown via
218 # die().
219 #------------------------------------------------------------------------
220  
221 sub get {
222 15 ▶ my ($self, $ident, $args) = @_;
Subroutine get: called CC=10 SCAR=23.0
Pod documented
223 15 my ($root, $result);
224 15 $root = $self;
225  
226 15 ▶ if (ref $ident eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ident eq 'ARRAY' or $ident =~ /\./ and $ident = [map({s/\(.*$//; $_, 0;} split(/\./, $ident, 0))]) { } 11 4 15
Condition100%5/5
ref $ident eq "ARRAY" or $ident =~ /\./ and $ident = [map({s/\(.*$//; $_, 0;} split(/\./, $ident, 0))]
l!l&&r!l&&!r
474
$ident =~ /\./ and $ident = [map({s/\(.*$//; $_, 0;} split(/\./, $ident, 0))]
!ll
47
MC/DC100%2/2
ref $ident eq "ARRAY" or $ident =~ /\./ and $ident = [map({s/\(.*$//; $_, 0;} split(/\./, $ident, 0))]
ref $ident eq "ARRAY" $ident =~ /\./
Truth table100%3/3
A or B
A= ref $ident eq "ARRAY"B= $ident =~ /\./
ABresult
1X1
011
000
227 || ($ident =~ /\./)
228 14 && ($ident = [ map { s/\(.*$//; ($_, 0) } split(/\./, $ident) ])) {
229 11 my $size = $#$ident;
230  
231 # if $ident is a list reference, then we evaluate each item in the
232 # identifier against the previous result, using the root stash
233 # ($self) as the first implicit 'result'...
234  
235 11 ▶ foreach (my $i = 0; $i <= $size; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $size) 20 11 31
236 20 ▶ if ( $i + 2 <= $size && ($ident->[$i+2] eq "scalar"
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 2 <= $size and $$ident[$i + 2] eq 'scalar' || $$ident[$i + 2] eq 'ref') { } 2 18 20
Condition100%6/6
$i + 2 <= $size and $$ident[$i + 2] eq "scalar" || $$ident[$i + 2] eq "ref"
!ll&&!rl&&r
992
$$ident[$i + 2] eq "scalar" or $$ident[$i + 2] eq "ref"
l!l&&r!l&&!r
119
MC/DC100%3/3
$i + 2 <= $size and $$ident[$i + 2] eq "scalar" || $$ident[$i + 2] eq "ref"
$i + 2 <= $size $$ident[$i + 2] eq "scalar" $$ident[$i + 2] eq "ref"
Truth table100%4/4
A and B or C
A= $i + 2 <= $sizeB= $$ident[$i + 2] eq "scalar"C= $$ident[$i + 2] eq "ref"
ABCresult
0XX0
1000
11X1
1011
237 || $ident->[$i+2] eq "ref") ) {
238 2 $result = $self->_dotop($root, @$ident[$i, $i+1], 0,
239 $ident->[$i+2]);
240 2 $i += 2;
241 } else {
242 18 $result = $self->_dotop($root, @$ident[$i, $i+1]);
243 }
244 20 ▶ last unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 20 20
245 20 $root = $result;
246 }
247 }
248 else {
249 4 $result = $self->_dotop($root, $ident, $args);
250 }
251  
252 15 ▶ return defined $result
Branch: 1 branch
BranchTrueFalseTotal
defined $result ? : 12 3 15
253 ? $result
254 : $self->undefined($ident, $args);
255 }
256  
257  
258 #------------------------------------------------------------------------
259 # set($ident, $value, $default)
260 #
261 # Updates the value for a variable in the stash. The first parameter
262 # should be the variable name or array, as per get(). The second
263 # parameter should be the intended value for the variable. The third,
264 # optional parameter is a flag which may be set to indicate 'default'
265 # mode. When set true, the variable will only be updated if it is
266 # currently undefined or has a false value. The magical 'IMPORT'
267 # variable identifier may be used to indicate that $value is a hash
268 # reference whose values should be imported. Returns the value set,
269 # or an empty string if not set (e.g. default mode). In the case of
270 # IMPORT, returns the number of items imported from the hash.
271 #------------------------------------------------------------------------
272  
273 sub set {
274 7 ▶ my ($self, $ident, $value, $default) = @_;
Subroutine set: called CC=7 SCAR=19.5
Pod documented
275 7 my ($root, $result, $error);
276  
277 7 $root = $self;
278  
279 ELEMENT: {
280 7 ▶ if (ref $ident eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ident eq 'ARRAY' or $ident =~ /\./ and $ident = [map({s/\(.*$//; $_, 0;} split(/\./, $ident, 0))]) { } 1 6 7
Condition80%4/5
ref $ident eq "ARRAY" or $ident =~ /\./ and $ident = [map({s/\(.*$//; $_, 0;} split(/\./, $ident, 0))]
l!l&&r!l&&!r
016
$ident =~ /\./ and $ident = [map({s/\(.*$//; $_, 0;} split(/\./, $ident, 0))]
!ll
61
MC/DC50%1/2
ref $ident eq "ARRAY" or $ident =~ /\./ and $ident = [map({s/\(.*$//; $_, 0;} split(/\./, $ident, 0))]
ref $ident eq "ARRAY" $ident =~ /\./
Truth table66%2/3
A or B
A= ref $ident eq "ARRAY"B= $ident =~ /\./
ABresult
1X1
011
000
281 || ($ident =~ /\./)
282 && ($ident = [
283 2 map { s/\(.*$//; ($_, 0) }
284 split(/\./, $ident)
285 ])
286 ) {
287 # a compound identifier may contain multiple elements (e.g.
288 # foo.bar.baz) and we must first resolve all but the last,
289 # using _dotop() with the $lvalue flag set which will create
290 # intermediate hashes if necessary...
291 1 my $size = $#$ident;
292 1 ▶ foreach (my $i = 0; $i < $size - 2; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size - 2) 1 1 2
293 1 $result = $self->_dotop($root, @$ident[$i, $i+1], 1);
294 1 ▶ last ELEMENT unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 1 1
295 1 $root = $result;
296 }
297  
298 # then we call _assign() to assign the value to the last element
299 1 $result = $self->_assign(
300 $root, @$ident[$size-1, $size],
301 $value, $default
302 );
303 }
304 else {
305 6 $result = $self->_assign($root, $ident, 0, $value, $default);
306 }
307 }
308  
309 7 ▶ return defined $result ? $result : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $result ? : 7 0 7
310 }
311  
312  
313 #------------------------------------------------------------------------
314 # getref($ident)
315 #
316 # Returns a "reference" to a particular item. This is represented as a
317 # closure which will return the actual stash item when called.
318 # WARNING: still experimental!
319 #------------------------------------------------------------------------
320  
321 sub getref {
322 0 ▶ my ($self, $ident, $args) = @_;
Subroutine getref: not called CC=6 SCAR=37.4
Pod documented
323 0 my ($root, $item, $result);
324 0 $root = $self;
325  
326 0 ▶ if (ref $ident eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ident eq 'ARRAY') { } 0 0 0
327 0 my $size = $#$ident;
328  
329 0 ▶ foreach (my $i = 0; $i <= $size; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $size) 0 0 0
330 0 ($item, $args) = @$ident[$i, $i + 1];
331 0 ▶ last if $i >= $size - 2; # don't evaluate last node
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $size - 2 0 0 0
332 last unless defined
333 0 ▶ ($root = $self->_dotop($root, $item, $args));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($root = $self->_dotop($root, $item, $args)) 0 0 0
334 }
335 }
336 else {
337 0 $item = $ident;
338 }
339  
340 0 ▶ if (defined $root) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $root) { } 0 0 0
341 return sub {
342 0 ▶ my @args = (@{$args||[]}, @_);
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$args or [] 0 0 0
343 0 $self->_dotop($root, $item, \@args);
344 }
345 0 }
346 else {
347 0 ▶ return sub { '' };
Subroutine __ANON__: not called CC=1 SCAR=6.9
348 }
349 }
350  
351  
352  
353  
354 #------------------------------------------------------------------------
355 # update(\%params)
356 #
357 # Update multiple variables en masse. No magic is performed. Simple
358 # variable names only.
359 #------------------------------------------------------------------------
360  
361 sub update {
362 3 ▶ my ($self, $params) = @_;
Subroutine update: called CC=3 SCAR=13.5
Pod documented
363  
364 # look out for magical 'import' argument to import another hash
365 3 my $import = $params->{ import };
366 3 ▶ if (defined $import && UNIVERSAL::isa($import, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $import and UNIVERSAL::isa($import, "HASH")) 0 3 3
Condition33%1/3
defined $import and UNIVERSAL::isa($import, "HASH")
!ll&&!rl&&r
300
MC/DC0%0/2
defined $import and UNIVERSAL::isa($import, "HASH")
defined $import UNIVERSAL::isa($import, "HASH")
Truth table33%1/3
A and B
A= defined $importB= UNIVERSAL::isa($import, "HASH")
ABresult
0X0
100
111
367 0 @$self{ keys %$import } = values %$import;
368 0 delete $params->{ import };
369 }
370  
371 3 @$self{ keys %$params } = values %$params;
372 }
373  
374  
375 #========================================================================
376 # ----- PRIVATE OBJECT METHODS -----
377 #========================================================================
378  
379 #------------------------------------------------------------------------
380 # _dotop($root, $item, \@args, $lvalue, $nextItem)
381 #
382 # This is the core 'dot' operation method which evaluates elements of
383 # variables against their root. All variables have an implicit root
384 # which is the stash object itself (a hash). Thus, a non-compound
385 # variable 'foo' is actually '(stash.)foo', the compound 'foo.bar' is
386 # '(stash.)foo.bar'. The first parameter is a reference to the current
387 # root, initially the stash itself. The second parameter contains the
388 # name of the variable element, e.g. 'foo'. The third optional
389 # parameter is a reference to a list of any parenthesised arguments
390 # specified for the variable, which are passed to sub-routines, object
391 # methods, etc. The final parameter is an optional flag to indicate
392 # if this variable is being evaluated on the left side of an assignment
393 # (e.g. foo.bar.baz = 10). When set true, intermediated hashes will
394 # be created (e.g. bar) if necessary.
395 #
396 # Returns the result of evaluating the item against the root, having
397 # performed any variable "magic". The value returned can then be used
398 # as the root of the next _dotop() in a compound sequence. Returns
399 # undef if the variable is undefined.
400 #------------------------------------------------------------------------
401  
402 sub _dotop {
403 25 ▶ my ($self, $root, $item, $args, $lvalue, $nextItem) = @_;
Subroutine _dotop: called CC=54 SCAR=65.0
404 25 my $rootref = ref $root;
405 25 my ($value, @result, $ret, $retVal);
406 25 ▶ $nextItem ||= "";
Condition100%2/2
$nextItem ||= ""
l!l
223
MC/DC100%1/1
$nextItem ||= ""
$nextItem
Truth table100%2/2
A
A= $nextItem
Aresult
11
00
407 25 ▶ my $scalarContext = 1 if ( $nextItem eq "scalar" );
Branch: 1 branch
BranchTrueFalseTotal
if $nextItem eq "scalar" 1 24 25
408 25 ▶ my $returnRef = 1 if ( $nextItem eq "ref" );
Branch: 1 branch
BranchTrueFalseTotal
if $nextItem eq "ref" 1 24 25
409  
410 25 ▶ $args ||= [ ];
Condition100%2/2
$args ||= []
l!l
124
MC/DC100%1/1
$args ||= []
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
411 25 ▶ $lvalue ||= 0;
Condition100%2/2
$lvalue ||= 0
l!l
124
MC/DC100%1/1
$lvalue ||= 0
$lvalue
Truth table100%2/2
A
A= $lvalue
Aresult
11
00
412  
413 # print STDERR "_dotop(root=$root, item=$item, args=[@$args])\n"
414 # if $DEBUG;
415  
416 # return undef without an error if either side of the dot is unviable
417 # or if an attempt is made to access a private member, starting _ or .
418 return undef
419 25 ▶ unless defined($root) and defined($item) and $item !~ /^[\._]/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root and defined $item and not $item =~ /^[\._]/ 0 25 25
Condition33%2/6
defined $root and defined $item and not $item =~ /^[\._]/
!ll&&!rl&&r
0025
defined $root and defined $item
!ll&&!rl&&r
0025
MC/DC0%0/3
defined $root and defined $item and not $item =~ /^[\._]/
defined $root defined $item not $item =~ /^[\._]/
Truth table25%1/4
A and B and not(C)
A= defined $rootB= defined $itemC= not $item =~ /^[\._]/
ABCresult
0XX0
10X0
1100
1111
420  
421 25 ▶ if (ref(\$root) eq "SCALAR" && !$lvalue &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$root eq "SCALAR" and not $lvalue and ($value = $Template::Stash::Context::LIST_OPS->{$item}) || $item =~ /^-?\d+$/) 0 25 25
Condition22%2/9
ref \$root eq "SCALAR" and not $lvalue and ($value = $Template::Stash::Context::LIST_OPS->{$item}) || $item =~ /^-?\d+$/
!ll&&!rl&&r
2500
ref \$root eq "SCALAR" and not $lvalue
!ll&&!rl&&r
2500
$value = $Template::Stash::Context::LIST_OPS->{$item} or $item =~ /^-?\d+$/
l!l&&r!l&&!r
000
MC/DC0%0/4
ref \$root eq "SCALAR" and not $lvalue and ($value = $Template::Stash::Context::LIST_OPS->{$item}) || $item =~ /^-?\d+$/
ref \$root eq "SCALAR" not $lvalue $value = $Template::Stash::Context::LIST_OPS->{$item} $item =~ /^-?\d+$/
Truth table20%1/5
A and not(B) and C or D
A= ref \$root eq "SCALAR"B= not $lvalueC= $value = $Template::Stash::Context::LIST_OPS->{$item}D= $item =~ /^-?\d+$/
ABCDresult
0XXX0
10XX0
11000
111X1
11011
422 (($value = $LIST_OPS->{ $item }) || $item =~ /^-?\d+$/) ) {
423 #
424 # Promote scalar to one element list, to be processed below.
425 #
426 0 $rootref = 'ARRAY';
427 0 $root = [$root];
428 }
429 25 ▶ if ($rootref eq $self->{_CLASS} || $rootref eq 'HASH') {
Branch: 5 branches
BranchTrueFalseTotal
if ($rootref eq $self->{'_CLASS'} or $rootref eq 'HASH') { } 24 1 25
elsif ($rootref eq 'ARRAY') { } 1 0 1
elsif (ref $root and UNIVERSAL::can($root, 'can')) { } 0 0 0
elsif ($value = $Template::Stash::Context::SCALAR_OPS->{$item} and not $lvalue) { } 0 0 0
elsif ($self->{'_DEBUG'}) { } 0 0 0
Condition100%3/3
$rootref eq $self->{"_CLASS"} or $rootref eq "HASH"
l!l&&r!l&&!r
1681
MC/DC100%2/2
$rootref eq $self->{"_CLASS"} or $rootref eq "HASH"
$rootref eq $self->{"_CLASS"} $rootref eq "HASH"
Truth table100%3/3
A or B
A= $rootref eq $self->{"_CLASS"}B= $rootref eq "HASH"
ABresult
1X1
011
000
430 # if $root is a regular HASH or a Template::Stash kinda HASH (the
431 # *real* root of everything). We first lookup the named key
432 # in the hash, or create an empty hash in its place if undefined
433 # and the $lvalue flag is set. Otherwise, we check the HASH_OPS
434 # pseudo-methods table, calling the code if found, or return undef.
435  
436 24 ▶ if (defined($value = $root->{ $item })) {
Branch: 5 branches
BranchTrueFalseTotal
if (defined($value = $root->{$item})) { } 21 3 24
elsif ($lvalue) { } 0 3 3
elsif ($value = $Template::Stash::Context::HASH_OPS->{$item}) { } 0 3 3
elsif (ref $item eq 'ARRAY') { } 0 3 3
elsif ($value = $Template::Stash::Context::SCALAR_OPS->{$item}) { } 0 3 3
437 21 ($ret, $retVal, @result) = _dotop_return(
438 $value, $args, $returnRef,
439 $scalarContext
440 );
441 21 ▶ return $retVal if ( $ret ); ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
if $ret 15 6 21
442 }
443 elsif ($lvalue) {
444 # we create an intermediate hash if this is an lvalue
445 0 return $root->{ $item } = { }; ## RETURN
446 }
447 elsif ($value = $HASH_OPS->{ $item }) {
448 0 @result = &$value($root, @$args); ## @result
449 }
450 elsif (ref $item eq 'ARRAY') {
451 # hash slice
452 0 return [@$root{@$item}]; ## RETURN
453 }
454 elsif ($value = $SCALAR_OPS->{ $item }) {
455 #
456 # Apply scalar ops to every hash element, in place.
457 #
458 0 foreach my $key ( keys %$root ) {
459 0 $root->{$key} = &$value($root->{$key}, @$args);
460 }
461 }
462 }
463 elsif ($rootref eq 'ARRAY') {
464 # if root is an ARRAY then we check for a LIST_OPS pseudo-method
465 # (except for l-values for which it doesn't make any sense)
466 # or return the numerical index into the array, or undef
467  
468 1 ▶ if (($value = $LIST_OPS->{ $item }) && ! $lvalue) {
Branch: 4 branches
BranchTrueFalseTotal
if ($value = $Template::Stash::Context::LIST_OPS->{$item} and not $lvalue) { } 1 0 1
elsif ($value = $Template::Stash::Context::SCALAR_OPS->{$item} and not $lvalue) { } 0 0 0
elsif ($item =~ /^-?\d+$/) { } 0 0 0
elsif (ref $item eq 'ARRAY') { } 0 0 0
Condition33%1/3
$value = $Template::Stash::Context::LIST_OPS->{$item} and not $lvalue
!ll&&!rl&&r
001
MC/DC0%0/2
$value = $Template::Stash::Context::LIST_OPS->{$item} and not $lvalue
$value = $Template::Stash::Context::LIST_OPS->{$item} not $lvalue
Truth table33%1/3
A and not(B)
A= $value = $Template::Stash::Context::LIST_OPS->{$item}B= not $lvalue
ABresult
0X0
100
111
469 1 @result = &$value($root, @$args); ## @result
470 }
471 ▶ elsif (($value = $SCALAR_OPS->{ $item }) && ! $lvalue) {
Condition0%0/3
$value = $Template::Stash::Context::SCALAR_OPS->{$item} and not $lvalue
!ll&&!rl&&r
000
MC/DC0%0/2
$value = $Template::Stash::Context::SCALAR_OPS->{$item} and not $lvalue
$value = $Template::Stash::Context::SCALAR_OPS->{$item} not $lvalue
Truth table0%0/3
A and not(B)
A= $value = $Template::Stash::Context::SCALAR_OPS->{$item}B= not $lvalue
ABresult
0X0
100
111
472 #
473 # Apply scalar ops to every array element, in place.
474 #
475 0 ▶ for ( my $i = 0 ; $i < @$root ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$root) 0 0 0
476 0 $root->[$i] = &$value($root->[$i], @$args); ## @result
477 }
478 }
479 elsif ($item =~ /^-?\d+$/) {
480 0 $value = $root->[$item];
481 0 ($ret, $retVal, @result) = _dotop_return(
482 $value, $args, $returnRef,
483 $scalarContext
484 );
485 0 ▶ return $retVal if ( $ret ); ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
if $ret 0 0 0
486 }
487 elsif (ref $item eq 'ARRAY' ) {
488 # array slice
489 0 return [@$root[@$item]]; ## RETURN
490 }
491 }
492  
493 # NOTE: we do the can-can because UNIVSERAL::isa($something, 'UNIVERSAL')
494 # doesn't appear to work with CGI, returning true for the first call
495 # and false for all subsequent calls.
496 ▶ elsif (ref($root) && UNIVERSAL::can($root, 'can')) {
Condition0%0/3
ref $root and UNIVERSAL::can($root, "can")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $root and UNIVERSAL::can($root, "can")
ref $root UNIVERSAL::can($root, "can")
Truth table0%0/3
A and B
A= ref $rootB= UNIVERSAL::can($root, "can")
ABresult
0X0
100
111
497 # if $root is a blessed reference (i.e. inherits from the
498 # UNIVERSAL object base class) then we call the item as a method.
499 # If that fails then we try to fallback on HASH behaviour if
500 # possible.
501 0 ▶ return ref $root->can($item) if ( $returnRef ); ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
if $returnRef 0 0 0
502 0 eval {
503 0 ▶ @result = $scalarContext
Branch: 1 branch
BranchTrueFalseTotal
$scalarContext ? : 0 0 0
504 ? scalar $root->$item(@$args)
505 : $root->$item(@$args); ## @result
506 };
507  
508 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
509 # failed to call object method, so try some fallbacks
510 0 ▶ if (UNIVERSAL::isa($root, 'HASH')
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($root, 'HASH') and defined($value = $root->{$item})) { } 0 0 0
elsif (UNIVERSAL::isa($root, 'ARRAY') and $value = $Template::Stash::Context::LIST_OPS->{$item}) { } 0 0 0
Condition0%0/3
UNIVERSAL::isa($root, "HASH") and defined($value = $root->{$item})
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::isa($root, "HASH") and defined($value = $root->{$item})
UNIVERSAL::isa($root, "HASH") defined($value = $root->{$item})
Truth table0%0/3
A and B
A= UNIVERSAL::isa($root, "HASH")B= defined($value = $root->{$item})
ABresult
0X0
100
111
511 && defined($value = $root->{ $item })) {
512 0 ($ret, $retVal, @result) = _dotop_return(
513 $value, $args,
514 $returnRef, $scalarContext
515 );
516 0 ▶ return $retVal if ( $ret ); ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
if $ret 0 0 0
517 }
518 ▶ elsif (UNIVERSAL::isa($root, 'ARRAY')
Condition0%0/3
UNIVERSAL::isa($root, "ARRAY") and $value = $Template::Stash::Context::LIST_OPS->{$item}
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::isa($root, "ARRAY") and $value = $Template::Stash::Context::LIST_OPS->{$item}
UNIVERSAL::isa($root, "ARRAY") $value = $Template::Stash::Context::LIST_OPS->{$item}
Truth table0%0/3
A and B
A= UNIVERSAL::isa($root, "ARRAY")B= $value = $Template::Stash::Context::LIST_OPS->{$item}
ABresult
0X0
100
111
519 && ($value = $LIST_OPS->{ $item })) {
520 0 @result = &$value($root, @$args);
521 }
522 else {
523 0 @result = (undef, $@);
524 }
525 }
526 }
527 ▶ elsif (($value = $SCALAR_OPS->{ $item }) && ! $lvalue) {
Condition0%0/3
$value = $Template::Stash::Context::SCALAR_OPS->{$item} and not $lvalue
!ll&&!rl&&r
000
MC/DC0%0/2
$value = $Template::Stash::Context::SCALAR_OPS->{$item} and not $lvalue
$value = $Template::Stash::Context::SCALAR_OPS->{$item} not $lvalue
Truth table0%0/3
A and not(B)
A= $value = $Template::Stash::Context::SCALAR_OPS->{$item}B= not $lvalue
ABresult
0X0
100
111
528 # at this point, it doesn't look like we've got a reference to
529 # anything we know about, so we try the SCALAR_OPS pseudo-methods
530 # table (but not for l-values)
531  
532 0 @result = &$value($root, @$args); ## @result
533 }
534 elsif ($self->{ _DEBUG }) {
535 0 die "don't know how to access [ $root ].$item\n"; ## DIE
536 }
537 else {
538 0 @result = ();
539 }
540  
541 # fold multiple return items into a list unless first item is undef
542 10 ▶ if (defined $result[0]) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $result[0]) { } 7 3 10
elsif (defined $result[1]) { } 0 3 3
elsif ($self->{'_DEBUG'}) { } 0 3 3
543 7 ▶ return ref(@result > 1
Branch: 2 branches
BranchTrueFalseTotal
if $returnRef 0 7 7
@result > 1 ? : 0 0 0
544 ? [ @result ]
545 : $result[0]
546 ) if ( $returnRef ); ## RETURN
547 7 ▶ if ( $scalarContext ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scalarContext) { } 1 6 7
548 1 ▶ return scalar @result if ( @result > 1 ); ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
if @result > 1 0 1 1
549 1 ▶ return scalar(@{$result[0]}) if ( ref $result[0] eq "ARRAY" );
Branch: 1 branch
BranchTrueFalseTotal
if ref $result[0] eq "ARRAY" 0 1 1
550 1 ▶ return scalar(%{$result[0]}) if ( ref $result[0] eq "HASH" );
Branch: 1 branch
BranchTrueFalseTotal
if ref $result[0] eq "HASH" 0 1 1
551 1 return $result[0]; ## RETURN
552 } else {
553 6 ▶ return @result > 1 ? [ @result ] : $result[0]; ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
@result > 1 ? : 1 5 6
554 }
555 }
556 elsif (defined $result[1]) {
557 0 die $result[1]; ## DIE
558 }
559 elsif ($self->{ _DEBUG }) {
560 0 die "$item is undefined\n"; ## DIE
561 }
562  
563 3 return undef;
564 }
565  
566 #------------------------------------------------------------------------
567 # ($ret, $retVal, @result) = _dotop_return($value, $args, $returnRef,
568 # $scalarContext);
569 #
570 # Handle the various return processing for _dotop
571 #------------------------------------------------------------------------
572  
573 sub _dotop_return
574 {
575 21 ▶ my($value, $args, $returnRef, $scalarContext) = @_;
Subroutine _dotop_return: called CC=7 SCAR=19.6
576 21 my(@result);
577  
578 21 ▶ return (1, ref $value) if ( $returnRef ); ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
if $returnRef 1 20 21
579 20 ▶ if ( $scalarContext ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scalarContext) { } 1 19 20
580 1 ▶ return (1, scalar(@$value)) if ref $value eq 'ARRAY'; ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 0 1 1
581 1 ▶ return (1, scalar(%$value)) if ref $value eq 'HASH'; ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 0 1 1
582 1 ▶ return (1, scalar($value)) unless ref $value eq 'CODE'; ## RETURN;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "CODE" 0 1 1
583 1 @result = scalar &$value(@$args) ## @result;
584 } else {
585 19 ▶ return (1, $value) unless ref $value eq 'CODE'; ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "CODE" 14 5 19
586 5 @result = &$value(@$args); ## @result
587 }
588 6 return (0, undef, @result);
589 }
590  
591  
592 #------------------------------------------------------------------------
593 # _assign($root, $item, \@args, $value, $default)
594 #
595 # Similar to _dotop() above, but assigns a value to the given variable
596 # instead of simply returning it. The first three parameters are the
597 # root item, the item and arguments, as per _dotop(), followed by the
598 # value to which the variable should be set and an optional $default
599 # flag. If set true, the variable will only be set if currently false
600 # (undefined/zero)
601 #------------------------------------------------------------------------
602  
603 sub _assign {
604 7 ▶ my ($self, $root, $item, $args, $value, $default) = @_;
Subroutine _assign: called CC=15 SCAR=41.9
605 7 my $rootref = ref $root;
606 7 my $result;
607 7 ▶ $args ||= [ ];
Condition50%1/2
$args ||= []
l!l
07
MC/DC0%0/1
$args ||= []
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
608 7 ▶ $default ||= 0;
Condition50%1/2
$default ||= 0
l!l
07
MC/DC0%0/1
$default ||= 0
$default
Truth table50%1/2
A
A= $default
Aresult
11
00
609  
610 # print(STDERR "_assign(root=$root, item=$item, args=[@$args], \n",
611 # "value=$value, default=$default)\n")
612 # if $DEBUG;
613  
614 # return undef without an error if either side of the dot is unviable
615 # or if an attempt is made to update a private member, starting _ or .
616 return undef ## RETURN
617 7 ▶ unless $root and defined $item and $item !~ /^[\._]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $root and defined $item and not $item =~ /^[\._]/ 0 7 7
Condition33%2/6
$root and defined $item and not $item =~ /^[\._]/
!ll&&!rl&&r
007
$root and defined $item
!ll&&!rl&&r
007
MC/DC0%0/3
$root and defined $item and not $item =~ /^[\._]/
$root defined $item not $item =~ /^[\._]/
Truth table25%1/4
A and B and not(C)
A= $rootB= defined $itemC= not $item =~ /^[\._]/
ABCresult
0XX0
10X0
1100
1111
618  
619 7 ▶ if ($rootref eq 'HASH' || $rootref eq $self->{_CLASS}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rootref eq 'HASH' or $rootref eq $self->{'_CLASS'}) { } 7 0 7
elsif ($rootref eq 'ARRAY' and $item =~ /^-?\d+$/) { } 0 0 0
elsif (UNIVERSAL::isa($root, 'UNIVERSAL')) { } 0 0 0
Condition66%2/3
$rootref eq "HASH" or $rootref eq $self->{"_CLASS"}
l!l&&r!l&&!r
160
MC/DC0%0/2
$rootref eq "HASH" or $rootref eq $self->{"_CLASS"}
$rootref eq "HASH" $rootref eq $self->{"_CLASS"}
Truth table66%2/3
A or B
A= $rootref eq "HASH"B= $rootref eq $self->{"_CLASS"}
ABresult
1X1
011
000
620 # if the root is a hash we set the named key
621 return ($root->{ $item } = $value) ## RETURN
622 7 ▶ unless $default && $root->{ $item };
Branch: 1 branch
BranchTrueFalseTotal
unless $default and $root->{$item} 7 0 7
Condition33%1/3
$default and $root->{$item}
!ll&&!rl&&r
700
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
623 }
624 ▶ elsif ($rootref eq 'ARRAY' && $item =~ /^-?\d+$/) {
Condition0%0/3
$rootref eq "ARRAY" and $item =~ /^-?\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$rootref eq "ARRAY" and $item =~ /^-?\d+$/
$rootref eq "ARRAY" $item =~ /^-?\d+$/
Truth table0%0/3
A and B
A= $rootref eq "ARRAY"B= $item =~ /^-?\d+$/
ABresult
0X0
100
111
625 # or set a list item by index number
626 return ($root->[$item] = $value) ## RETURN
627 0 ▶ unless $default && $root->{ $item };
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
628 }
629 elsif (UNIVERSAL::isa($root, 'UNIVERSAL')) {
630 # try to call the item as a method of an object
631 0 return $root->$item(@$args, $value); ## RETURN
632 }
633 else {
634 0 die "don't know how to assign to [$root].[$item]\n"; ## DIE
635 }
636  
637 0 return undef;
638 }
639  
640 1 1;
641  
642 __END__
« blib/lib/Template/Parser.pm ↑ Summary blib/lib/Template/Test.pm »