blib/lib/Template/Stash/Context.pm

filter: Statementstmt 66.0% 109 / 165 Branchbran 45.0% 64 / 142 Conditioncond 44.2% 42 / 95 MC/DCmcdc 20.7% 11 / 53 Subroutinesub 70.5% 12 / 17 Podpod 100% 7 / 7 totaltotal 51.1% 245 / 479 CC 103 SCAR 57.5
CC
103
Cov
53%
_dotop
665.5
_assign
65.7
getref
42.0
CRAP 314.7
« blib/lib/Template/App/ttree.pm ↑ Summary lib/Template/Stash.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.106';
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 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
125 1 my $class = shift;
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 3 ▶sub clone {
Subroutine clone: called CC=5 SCAR=18.3
Pod documented
163 3 my ($self, $params) = @_;
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 0 ▶ &{ $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 3 ▶sub declone {
Subroutine declone: called CC=2 SCAR=7.2
Pod documented
201 3 my $self = shift;
202 3 ▶ $self->{ _PARENT } || $self;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_PARENT"} or $self 3 0 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 15 ▶sub get {
Subroutine get: called CC=10 SCAR=23.0
Pod documented
222 15 my ($self, $ident, $args) = @_;
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 $result // $self->undefined($ident, $args);
Condition66%2/3
$result // $self->undefined($ident, $args)
l!l&&r!l&&!r
1230
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
253 }
254  
255  
256 #------------------------------------------------------------------------
257 # set($ident, $value, $default)
258 #
259 # Updates the value for a variable in the stash. The first parameter
260 # should be the variable name or array, as per get(). The second
261 # parameter should be the intended value for the variable. The third,
262 # optional parameter is a flag which may be set to indicate 'default'
263 # mode. When set true, the variable will only be updated if it is
264 # currently undefined or has a false value. The magical 'IMPORT'
265 # variable identifier may be used to indicate that $value is a hash
266 # reference whose values should be imported. Returns the value set,
267 # or an empty string if not set (e.g. default mode). In the case of
268 # IMPORT, returns the number of items imported from the hash.
269 #------------------------------------------------------------------------
270  
271 7 ▶sub set {
Subroutine set: called CC=7 SCAR=19.5
Pod documented
272 7 my ($self, $ident, $value, $default) = @_;
273 7 my ($root, $result, $error);
274  
275 7 $root = $self;
276  
277 ELEMENT: {
278 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
279 || ($ident =~ /\./)
280 && ($ident = [
281 2 map { s/\(.*$//; ($_, 0) }
282 split(/\./, $ident)
283 ])
284 ) {
285 # a compound identifier may contain multiple elements (e.g.
286 # foo.bar.baz) and we must first resolve all but the last,
287 # using _dotop() with the $lvalue flag set which will create
288 # intermediate hashes if necessary...
289 1 my $size = $#$ident;
290 1 ▶ foreach (my $i = 0; $i < $size - 2; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size - 2) 1 1 2
291 1 $result = $self->_dotop($root, @$ident[$i, $i+1], 1);
292 1 ▶ last ELEMENT unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 1 1
293 1 $root = $result;
294 }
295  
296 # then we call _assign() to assign the value to the last element
297 1 $result = $self->_assign(
298 $root, @$ident[$size-1, $size],
299 $value, $default
300 );
301 }
302 else {
303 6 $result = $self->_assign($root, $ident, 0, $value, $default);
304 }
305 }
306  
307 7 ▶ return $result // '';
Condition50%1/2
$result // ""
l!l
70
MC/DC0%0/1
$result // ""
$result
Truth table50%1/2
A
A= $result
Aresult
11
00
308 }
309  
310  
311 #------------------------------------------------------------------------
312 # getref($ident)
313 #
314 # Returns a "reference" to a particular item. This is represented as a
315 # closure which will return the actual stash item when called.
316 # WARNING: still experimental!
317 #------------------------------------------------------------------------
318  
319 0 ▶sub getref {
Subroutine getref: not called CC=6 SCAR=37.4
Pod documented
320 0 my ($self, $ident, $args) = @_;
321 0 my ($root, $item, $result);
322 0 $root = $self;
323  
324 0 ▶ if (ref $ident eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ident eq 'ARRAY') { } 0 0 0
325 0 my $size = $#$ident;
326  
327 0 ▶ foreach (my $i = 0; $i <= $size; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $size) 0 0 0
328 0 ($item, $args) = @$ident[$i, $i + 1];
329 0 ▶ last if $i >= $size - 2; # don't evaluate last node
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $size - 2 0 0 0
330 last unless defined
331 0 ▶ ($root = $self->_dotop($root, $item, $args));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($root = $self->_dotop($root, $item, $args)) 0 0 0
332 }
333 }
334 else {
335 0 $item = $ident;
336 }
337  
338 0 ▶ if (defined $root) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $root) { } 0 0 0
339 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
340 0 ▶ my @args = (@{$args||[]}, @_);
Condition0%0/2
$args || []
l!l
00
MC/DC0%0/1
$args || []
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
341 0 $self->_dotop($root, $item, \@args);
342 }
343 0 }
344 else {
345 0 ▶ return sub { '' };
Subroutine __ANON__: not called CC=1 SCAR=6.9
346 }
347 }
348  
349  
350  
351  
352 #------------------------------------------------------------------------
353 # update(\%params)
354 #
355 # Update multiple variables en masse. No magic is performed. Simple
356 # variable names only.
357 #------------------------------------------------------------------------
358  
359 3 ▶sub update {
Subroutine update: called CC=3 SCAR=13.5
Pod documented
360 3 my ($self, $params) = @_;
361  
362 # look out for magical 'import' argument to import another hash
363 3 my $import = $params->{ import };
364 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
365 0 @$self{ keys %$import } = values %$import;
366 0 delete $params->{ import };
367 }
368  
369 3 @$self{ keys %$params } = values %$params;
370 }
371  
372  
373 #========================================================================
374 # ----- PRIVATE OBJECT METHODS -----
375 #========================================================================
376  
377 #------------------------------------------------------------------------
378 # _dotop($root, $item, \@args, $lvalue, $nextItem)
379 #
380 # This is the core 'dot' operation method which evaluates elements of
381 # variables against their root. All variables have an implicit root
382 # which is the stash object itself (a hash). Thus, a non-compound
383 # variable 'foo' is actually '(stash.)foo', the compound 'foo.bar' is
384 # '(stash.)foo.bar'. The first parameter is a reference to the current
385 # root, initially the stash itself. The second parameter contains the
386 # name of the variable element, e.g. 'foo'. The third optional
387 # parameter is a reference to a list of any parenthesised arguments
388 # specified for the variable, which are passed to sub-routines, object
389 # methods, etc. The final parameter is an optional flag to indicate
390 # if this variable is being evaluated on the left side of an assignment
391 # (e.g. foo.bar.baz = 10). When set true, intermediated hashes will
392 # be created (e.g. bar) if necessary.
393 #
394 # Returns the result of evaluating the item against the root, having
395 # performed any variable "magic". The value returned can then be used
396 # as the root of the next _dotop() in a compound sequence. Returns
397 # undef if the variable is undefined.
398 #------------------------------------------------------------------------
399  
400 25 ▶sub _dotop {
Subroutine _dotop: called CC=54 SCAR=65.0
401 25 my ($self, $root, $item, $args, $lvalue, $nextItem) = @_;
402 25 my $rootref = ref $root;
403 25 my ($value, @result, $ret, $retVal);
404 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
405 25 ▶ my $scalarContext = 1 if ( $nextItem eq "scalar" );
Branch: 1 branch
BranchTrueFalseTotal
if $nextItem eq "scalar" 1 24 25
406 25 ▶ my $returnRef = 1 if ( $nextItem eq "ref" );
Branch: 1 branch
BranchTrueFalseTotal
if $nextItem eq "ref" 1 24 25
407  
408 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
409 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
410  
411 # print STDERR "_dotop(root=$root, item=$item, args=[@$args])\n"
412 # if $DEBUG;
413  
414 # return undef without an error if either side of the dot is unviable
415 # or if an attempt is made to access a private member, starting _ or .
416 return undef
417 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
418  
419 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
420 (($value = $LIST_OPS->{ $item }) || $item =~ /^-?\d+$/) ) {
421 #
422 # Promote scalar to one element list, to be processed below.
423 #
424 0 $rootref = 'ARRAY';
425 0 $root = [$root];
426 }
427 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
428 # if $root is a regular HASH or a Template::Stash kinda HASH (the
429 # *real* root of everything). We first lookup the named key
430 # in the hash, or create an empty hash in its place if undefined
431 # and the $lvalue flag is set. Otherwise, we check the HASH_OPS
432 # pseudo-methods table, calling the code if found, or return undef.
433  
434 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
435 21 ($ret, $retVal, @result) = _dotop_return(
436 $value, $args, $returnRef,
437 $scalarContext
438 );
439 21 ▶ return $retVal if ( $ret ); ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
if $ret 15 6 21
440 }
441 elsif ($lvalue) {
442 # we create an intermediate hash if this is an lvalue
443 0 return $root->{ $item } = { }; ## RETURN
444 }
445 elsif ($value = $HASH_OPS->{ $item }) {
446 0 @result = &$value($root, @$args); ## @result
447 }
448 elsif (ref $item eq 'ARRAY') {
449 # hash slice
450 0 return [@$root{@$item}]; ## RETURN
451 }
452 elsif ($value = $SCALAR_OPS->{ $item }) {
453 #
454 # Apply scalar ops to every hash element, in place.
455 #
456 0 foreach my $key ( keys %$root ) {
457 0 $root->{$key} = &$value($root->{$key}, @$args);
458 }
459 }
460 }
461 elsif ($rootref eq 'ARRAY') {
462 # if root is an ARRAY then we check for a LIST_OPS pseudo-method
463 # (except for l-values for which it doesn't make any sense)
464 # or return the numerical index into the array, or undef
465  
466 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
467 1 @result = &$value($root, @$args); ## @result
468 }
469 ▶ 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
470 #
471 # Apply scalar ops to every array element, in place.
472 #
473 0 ▶ for ( my $i = 0 ; $i < @$root ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$root) 0 0 0
474 0 $root->[$i] = &$value($root->[$i], @$args); ## @result
475 }
476 }
477 elsif ($item =~ /^-?\d+$/) {
478 0 $value = $root->[$item];
479 0 ($ret, $retVal, @result) = _dotop_return(
480 $value, $args, $returnRef,
481 $scalarContext
482 );
483 0 ▶ return $retVal if ( $ret ); ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
if $ret 0 0 0
484 }
485 elsif (ref $item eq 'ARRAY' ) {
486 # array slice
487 0 return [@$root[@$item]]; ## RETURN
488 }
489 }
490  
491 # NOTE: we do the can-can because UNIVSERAL::isa($something, 'UNIVERSAL')
492 # doesn't appear to work with CGI, returning true for the first call
493 # and false for all subsequent calls.
494 ▶ 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
495 # if $root is a blessed reference (i.e. inherits from the
496 # UNIVERSAL object base class) then we call the item as a method.
497 # If that fails then we try to fallback on HASH behaviour if
498 # possible.
499 0 ▶ return ref $root->can($item) if ( $returnRef ); ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
if $returnRef 0 0 0
500 0 eval {
501 0 ▶ @result = $scalarContext
Branch: 1 branch
BranchTrueFalseTotal
$scalarContext ? : 0 0 0
502 ? scalar $root->$item(@$args)
503 : $root->$item(@$args); ## @result
504 };
505  
506 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
507 # failed to call object method, so try some fallbacks
508 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
509 && defined($value = $root->{ $item })) {
510 0 ($ret, $retVal, @result) = _dotop_return(
511 $value, $args,
512 $returnRef, $scalarContext
513 );
514 0 ▶ return $retVal if ( $ret ); ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
if $ret 0 0 0
515 }
516 ▶ 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
517 && ($value = $LIST_OPS->{ $item })) {
518 0 @result = &$value($root, @$args);
519 }
520 else {
521 0 @result = (undef, $@);
522 }
523 }
524 }
525 ▶ 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
526 # at this point, it doesn't look like we've got a reference to
527 # anything we know about, so we try the SCALAR_OPS pseudo-methods
528 # table (but not for l-values)
529  
530 0 @result = &$value($root, @$args); ## @result
531 }
532 elsif ($self->{ _DEBUG }) {
533 0 die "don't know how to access [ $root ].$item\n"; ## DIE
534 }
535 else {
536 0 @result = ();
537 }
538  
539 # fold multiple return items into a list unless first item is undef
540 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
541 7 ▶ return ref(@result > 1
Branch: 2 branches
BranchTrueFalseTotal
if $returnRef 0 7 7
@result > 1 ? : 0 0 0
542 ? [ @result ]
543 : $result[0]
544 ) if ( $returnRef ); ## RETURN
545 7 ▶ if ( $scalarContext ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scalarContext) { } 1 6 7
546 1 ▶ return scalar @result if ( @result > 1 ); ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
if @result > 1 0 1 1
547 0 ▶ return scalar(@{$result[0]}) if ( ref $result[0] eq "ARRAY" );
Branch: 1 branch
BranchTrueFalseTotal
if ref $result[0] eq "ARRAY" 0 1 1
548 0 ▶ return scalar(%{$result[0]}) if ( ref $result[0] eq "HASH" );
Branch: 1 branch
BranchTrueFalseTotal
if ref $result[0] eq "HASH" 0 1 1
549 1 return $result[0]; ## RETURN
550 } else {
551 6 ▶ return @result > 1 ? [ @result ] : $result[0]; ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
@result > 1 ? : 1 5 6
552 }
553 }
554 elsif (defined $result[1]) {
555 0 die $result[1]; ## DIE
556 }
557 elsif ($self->{ _DEBUG }) {
558 0 die "$item is undefined\n"; ## DIE
559 }
560  
561 3 return undef;
562 }
563  
564 #------------------------------------------------------------------------
565 # ($ret, $retVal, @result) = _dotop_return($value, $args, $returnRef,
566 # $scalarContext);
567 #
568 # Handle the various return processing for _dotop
569 #------------------------------------------------------------------------
570  
571 sub _dotop_return
572 21 ▶{
Subroutine _dotop_return: called CC=7 SCAR=19.6
573 21 my($value, $args, $returnRef, $scalarContext) = @_;
574 21 my(@result);
575  
576 21 ▶ return (1, ref $value) if ( $returnRef ); ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
if $returnRef 1 20 21
577 20 ▶ if ( $scalarContext ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scalarContext) { } 1 19 20
578 1 ▶ return (1, scalar(@$value)) if ref $value eq 'ARRAY'; ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 0 1 1
579 1 ▶ return (1, scalar(%$value)) if ref $value eq 'HASH'; ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 0 1 1
580 1 ▶ return (1, scalar($value)) unless ref $value eq 'CODE'; ## RETURN;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "CODE" 0 1 1
581 1 @result = scalar &$value(@$args) ## @result;
582 } else {
583 19 ▶ return (1, $value) unless ref $value eq 'CODE'; ## RETURN
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "CODE" 14 5 19
584 5 @result = &$value(@$args); ## @result
585 }
586 6 return (0, undef, @result);
587 }
588  
589  
590 #------------------------------------------------------------------------
591 # _assign($root, $item, \@args, $value, $default)
592 #
593 # Similar to _dotop() above, but assigns a value to the given variable
594 # instead of simply returning it. The first three parameters are the
595 # root item, the item and arguments, as per _dotop(), followed by the
596 # value to which the variable should be set and an optional $default
597 # flag. If set true, the variable will only be set if currently false
598 # (undefined/zero)
599 #------------------------------------------------------------------------
600  
601 7 ▶sub _assign {
Subroutine _assign: called CC=15 SCAR=41.9
602 7 my ($self, $root, $item, $args, $value, $default) = @_;
603 7 my $rootref = ref $root;
604 7 my $result;
605 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
606 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
607  
608 # print(STDERR "_assign(root=$root, item=$item, args=[@$args], \n",
609 # "value=$value, default=$default)\n")
610 # if $DEBUG;
611  
612 # return undef without an error if either side of the dot is unviable
613 # or if an attempt is made to update a private member, starting _ or .
614 return undef ## RETURN
615 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
616  
617 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
618 # if the root is a hash we set the named key
619 return ($root->{ $item } = $value) ## RETURN
620 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
621 }
622 ▶ 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
623 # or set a list item by index number
624 return ($root->[$item] = $value) ## RETURN
625 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
626 }
627 elsif (UNIVERSAL::isa($root, 'UNIVERSAL')) {
628 # try to call the item as a method of an object
629 0 return $root->$item(@$args, $value); ## RETURN
630 }
631 else {
632 0 die "don't know how to assign to [$root].[$item]\n"; ## DIE
633 }
634  
635 0 return undef;
636 }
637  
638 1 1;
639  
640 __END__
« blib/lib/Template/App/ttree.pm ↑ Summary lib/Template/Stash.pm »