lib/Template/VMethods.pm

filter: Statementstmt 97.1% 275 / 283 Branchbran 79.0% 87 / 110 Conditioncond 67.6% 44 / 65 MC/DCmcdc 41.6% 15 / 36 Subroutinesub 97.3% 74 / 76 Podpod 0.0% 0 / 64 totaltotal 78.0% 495 / 634 CC 87 SCAR 14.4
CC
87
Cov
89%
text_split
13.4
text_replace
12.3
list_splice
6.0
CRAP 4.2
« lib/Template/Plugin/List.pm ↑ Summary lib/Template/Plugin/String.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::VMethods
4 #
5 # DESCRIPTION
6 # Module defining virtual methods for the Template Toolkit
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 # COPYRIGHT
12 # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved.
13 #
14 # This module is free software; you can redistribute it and/or
15 # modify it under the same terms as Perl itself.
16 #
17 #============================================================================
18  
19 package Template::VMethods;
20  
21 106 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 106 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 106 ▶use Scalar::Util qw( blessed looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 106 ▶use Template::Filters;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 106 our $VERSION = '3.106';
27 106 ▶our $DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::VMethods::DEBUG 106 0 106
28  
29 106 our $ROOT_VMETHODS = {
30 inc => \&root_inc,
31 dec => \&root_dec,
32 };
33  
34 106 our $TEXT_VMETHODS = {
35 item => \&text_item,
36 list => \&text_list,
37 hash => \&text_hash,
38 length => \&text_length,
39 size => \&text_size,
40 empty => \&text_empty,
41 defined => \&text_defined,
42 upper => \&text_upper,
43 lower => \&text_lower,
44 ucfirst => \&text_ucfirst,
45 lcfirst => \&text_lcfirst,
46 match => \&text_match,
47 search => \&text_search,
48 repeat => \&text_repeat,
49 replace => \&text_replace,
50 remove => \&text_remove,
51 split => \&text_split,
52 chunk => \&text_chunk,
53 substr => \&text_substr,
54 trim => \&text_trim,
55 collapse => \&text_collapse,
56 squote => \&text_squote,
57 dquote => \&text_dquote,
58 html => \&Template::Filters::html_filter,
59 xml => \&Template::Filters::xml_filter,
60 };
61  
62 106 our $HASH_VMETHODS = {
63 item => \&hash_item,
64 hash => \&hash_hash,
65 size => \&hash_size,
66 empty => \&hash_empty,
67 each => \&hash_each,
68 keys => \&hash_keys,
69 values => \&hash_values,
70 items => \&hash_items,
71 pairs => \&hash_pairs,
72 list => \&hash_list,
73 exists => \&hash_exists,
74 defined => \&hash_defined,
75 delete => \&hash_delete,
76 import => \&hash_import,
77 sort => \&hash_sort,
78 nsort => \&hash_nsort,
79 };
80  
81 106 our $LIST_VMETHODS = {
82 item => \&list_item,
83 list => \&list_list,
84 hash => \&list_hash,
85 push => \&list_push,
86 pop => \&list_pop,
87 unshift => \&list_unshift,
88 shift => \&list_shift,
89 max => \&list_max,
90 size => \&list_size,
91 empty => \&list_empty,
92 defined => \&list_defined,
93 first => \&list_first,
94 last => \&list_last,
95 reverse => \&list_reverse,
96 grep => \&list_grep,
97 join => \&list_join,
98 sort => \&list_sort,
99 nsort => \&list_nsort,
100 unique => \&list_unique,
101 import => \&list_import,
102 merge => \&list_merge,
103 slice => \&list_slice,
104 splice => \&list_splice,
105 };
106  
107 # Template::Stash needs the above, so defer loading this module
108 # until they are defined.
109 106 require Template::Stash;
110 106 our $PRIVATE = $Template::Stash::PRIVATE;
111  
112 #========================================================================
113 # root virtual methods
114 #========================================================================
115  
116 4 ▶sub root_inc {
Subroutine root_inc: called CC=1 SCAR=0.0
Pod undocumented
117 106 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
118 4 my $item = shift;
119 4 ++$item;
120 }
121  
122 0 ▶sub root_dec {
Subroutine root_dec: not called CC=1 SCAR=0.6
Pod undocumented
123 106 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 0 my $item = shift;
125 0 --$item;
126 }
127  
128  
129 #========================================================================
130 # text virtual methods
131 #========================================================================
132  
133 6 ▶sub text_item {
Subroutine text_item: called CC=1 SCAR=0.0
Pod undocumented
134 6 $_[0];
135 }
136  
137 5 ▶sub text_list {
Subroutine text_list: called CC=1 SCAR=0.0
Pod undocumented
138 5 [ $_[0] ];
139 }
140  
141 3 ▶sub text_hash {
Subroutine text_hash: called CC=1 SCAR=0.0
Pod undocumented
142 3 { value => $_[0] };
143 }
144  
145 3 ▶sub text_length {
Subroutine text_length: called CC=1 SCAR=0.0
Pod undocumented
146 3 length $_[0];
147 }
148  
149 1 ▶sub text_size {
Subroutine text_size: called CC=1 SCAR=0.0
Pod undocumented
150 1 return 1;
151 }
152  
153 2 ▶sub text_empty {
Subroutine text_empty: called CC=2 SCAR=6.9
Pod undocumented
154 2 ▶ return 0 == text_length($_[0]) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
0 == text_length($_[0]) ? : 1 1 2
155 }
156  
157 4 ▶sub text_defined {
Subroutine text_defined: called CC=1 SCAR=0.0
Pod undocumented
158 4 return 1;
159 }
160  
161 1 ▶sub text_upper {
Subroutine text_upper: called CC=1 SCAR=0.0
Pod undocumented
162 1 return uc $_[0];
163 }
164  
165 2 ▶sub text_lower {
Subroutine text_lower: called CC=1 SCAR=0.0
Pod undocumented
166 2 return lc $_[0];
167 }
168  
169 3 ▶sub text_ucfirst {
Subroutine text_ucfirst: called CC=1 SCAR=0.0
Pod undocumented
170 3 return ucfirst $_[0];
171 }
172  
173 2 ▶sub text_lcfirst {
Subroutine text_lcfirst: called CC=1 SCAR=0.0
Pod undocumented
174 2 return lcfirst $_[0];
175 }
176  
177 2 ▶sub text_trim {
Subroutine text_trim: called CC=1 SCAR=0.0
Pod undocumented
178 2 my $text = $_[0];
179 2 $text =~ s/^\s+//;
180 2 $text =~ s/\s+$//;
181 2 return $text;
182 }
183  
184 2 ▶sub text_collapse {
Subroutine text_collapse: called CC=1 SCAR=0.0
Pod undocumented
185 2 my $text = $_[0];
186 2 $text =~ s/^\s+//;
187 2 $text =~ s/\s+$//;
188 2 $text =~ s/\s+/ /g;
189 2 return $text;
190 }
191  
192 10 ▶sub text_match {
Subroutine text_match: called CC=5 SCAR=16.7
Pod undocumented
193 10 my ($str, $search, $global) = @_;
194 10 ▶ return $str unless defined $str and defined $search;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and defined $search 0 10 10
Condition33%1/3
defined $str and defined $search
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $str and defined $search
defined $str defined $search
Truth table33%1/3
A and B
A= defined $strB= defined $search
ABresult
0X0
100
111
195 10 ▶ my @matches = $global ? ($str =~ /$search/g)
Branch: 1 branch
BranchTrueFalseTotal
$global ? : 3 7 10
196 : ($str =~ /$search/);
197 10 ▶ return @matches ? \@matches : '';
Branch: 1 branch
BranchTrueFalseTotal
@matches ? : 6 4 10
198 }
199  
200 5 ▶sub text_search {
Subroutine text_search: called CC=3 SCAR=12.5
Pod undocumented
201 5 my ($str, $pattern) = @_;
202 5 ▶ return $str unless defined $str and defined $pattern;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and defined $pattern 0 5 5
Condition33%1/3
defined $str and defined $pattern
!ll&&!rl&&r
005
MC/DC0%0/2
defined $str and defined $pattern
defined $str defined $pattern
Truth table33%1/3
A and B
A= defined $strB= defined $pattern
ABresult
0X0
100
111
203 5 return $str =~ /$pattern/;
204 }
205  
206 2 ▶sub text_repeat {
Subroutine text_repeat: called CC=3 SCAR=11.4
Pod undocumented
207 2 my ($str, $count) = @_;
208 2 ▶ $str //= '';
Condition50%1/2
$str //= ""
l!l
20
MC/DC0%0/1
$str //= ""
$str
Truth table50%1/2
A
A= $str
Aresult
11
00
209 2 ▶ return '' unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 0 2 2
210 2 return $str x $count;
211 }
212  
213 31 ▶sub text_replace {
Subroutine text_replace: called CC=12 SCAR=25.1
Pod undocumented
214 31 my ($text, $pattern, $replace, $global) = @_;
215 31 ▶ $text //= '';
Condition50%1/2
$text //= ""
l!l
310
MC/DC0%0/1
$text //= ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
216 31 ▶ $pattern //= '';
Condition50%1/2
$pattern //= ""
l!l
310
MC/DC0%0/1
$pattern //= ""
$pattern
Truth table50%1/2
A
A= $pattern
Aresult
11
00
217 31 ▶ $replace //= '';
Condition50%1/2
$replace //= ""
l!l
310
MC/DC0%0/1
$replace //= ""
$replace
Truth table50%1/2
A
A= $replace
Aresult
11
00
218 31 ▶ $global //= 1;
Condition100%2/2
$global //= 1
l!l
130
MC/DC100%1/1
$global //= 1
$global
Truth table100%2/2
A
A= $global
Aresult
11
00
219  
220 31 ▶ if ($replace =~ /\$\d+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replace =~ /\$\d+/) { } 20 11 31
221 # replacement string may contain backrefs
222 38 ▶ my $expand = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
223 38 my ($chunk, $start, $end) = @_;
224 38 $chunk =~ s{ \\(\\|\$) | \$ (\d+) }{
225 69 ▶ $1 ? $1
Branch: 2 branches
BranchTrueFalseTotal
$1 ? : 5 64 69
$2 > $start->$#* || $2 == 0 || !defined($start->[$2]) ? : 7 57 64
Condition83%5/6
$2 > $start->$#* or $2 == 0 or not defined $start->[$2]
l!l&&r!l&&!r
6157
$2 > $start->$#* or $2 == 0
l!l&&r!l&&!r
6058
MC/DC66%2/3
$2 > $start->$#* or $2 == 0 or not defined $start->[$2]
$2 > $start->$#* $2 == 0 not defined $start->[$2]
Truth table75%3/4
A or B or not(C)
A= $2 > $start->$#*B= $2 == 0C= not defined $start->[$2]
ABCresult
1XX1
01X1
0011
0000
226 : ($2 > $#$start || $2 == 0 || !defined $start->[$2]) ? ''
227 : substr($text, $start->[$2], $end->[$2] - $start->[$2]);
228 }exg;
229 38 $chunk;
230 20 };
231 20 ▶ if ($global) {
Branch: 1 branch
BranchTrueFalseTotal
if ($global) { } 20 0 20
232 20 my $prev_end = -1;
233 20 $text =~ s{$pattern}{
234 39 my $s = $-[0];
235 39 my $e = $+[0];
236 39 ▶ if ($s == $e && $s == $prev_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s == $e and $s == $prev_end) { } 1 38 39
Condition66%2/3
$s == $e and $s == $prev_end
!ll&&!rl&&r
3801
MC/DC50%1/2
$s == $e and $s == $prev_end
$s == $e $s == $prev_end
Truth table66%2/3
A and B
A= $s == $eB= $s == $prev_end
ABresult
0X0
100
111
237 1 '';
238 } else {
239 38 $prev_end = $e;
240 38 &$expand($replace, [@-], [@+]);
241 }
242 }eg;
243 }
244 else {
245 0 $text =~ s{$pattern}{ &$expand($replace, [@-], [@+]) }e;
246 }
247 }
248 else {
249 11 ▶ if ($global) {
Branch: 1 branch
BranchTrueFalseTotal
if ($global) { } 10 1 11
250 10 my $prev_end = -1;
251 10 $text =~ s{$pattern}{
252 17 my $s = $-[0];
253 17 my $e = $+[0];
254 17 ▶ if ($s == $e && $s == $prev_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s == $e and $s == $prev_end) { } 1 16 17
Condition66%2/3
$s == $e and $s == $prev_end
!ll&&!rl&&r
1601
MC/DC50%1/2
$s == $e and $s == $prev_end
$s == $e $s == $prev_end
Truth table66%2/3
A and B
A= $s == $eB= $s == $prev_end
ABresult
0X0
100
111
255 1 '';
256 } else {
257 16 $prev_end = $e;
258 16 $replace;
259 }
260 }eg;
261 }
262 else {
263 1 $text =~ s/$pattern/$replace/;
264 }
265 }
266 31 return $text;
267 }
268  
269 1 ▶sub text_remove {
Subroutine text_remove: called CC=3 SCAR=12.0
Pod undocumented
270 1 my ($str, $search) = @_;
271 1 ▶ return $str unless defined $str and defined $search;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and defined $search 0 1 1
Condition33%1/3
defined $str and defined $search
!ll&&!rl&&r
001
MC/DC0%0/2
defined $str and defined $search
defined $str defined $search
Truth table33%1/3
A and B
A= defined $strB= defined $search
ABresult
0X0
100
111
272 1 $str =~ s/$search//g;
273 1 return $str;
274 }
275  
276 11 ▶sub text_split {
Subroutine text_split: called CC=9 SCAR=26.0
Pod undocumented
277 11 my ($str, $split, $limit) = @_;
278 11 ▶ $str //= '';
Condition50%1/2
$str //= ""
l!l
110
MC/DC0%0/1
$str //= ""
$str
Truth table50%1/2
A
A= $str
Aresult
11
00
279  
280 # For versions of Perl prior to 5.18 we have to be very careful about
281 # spelling out each possible combination of arguments because split()
282 # is very sensitive to them, for example C<split(' ', ...)> behaves
283 # differently to C<$space=' '; split($space, ...)>. Test 33 of
284 # vmethods/text.t depends on this behaviour.
285  
286 11 ▶ if ($] < 5.018) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.018") 0 11 11
287 0 ▶ if (defined $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $limit) { } 0 0 0
288 return [
289 0 ▶ defined $split
Branch: 1 branch
BranchTrueFalseTotal
defined $split ? : 0 0 0
290 ? split($split, $str, $limit)
291 : split(' ', $str, $limit)
292 ];
293 }
294 else {
295 return [
296 0 ▶ defined $split
Branch: 1 branch
BranchTrueFalseTotal
defined $split ? : 0 0 0
297 ? split($split, $str)
298 : split(' ', $str)
299 ];
300 }
301 }
302  
303 # split's behavior changed in Perl 5.18.0 making this:
304 # C<$space=' '; split($space, ...)>
305 # behave the same as this:
306 # C<split(' ', ...)>
307 # qr// behaves the same, so use that for user-defined split.
308  
309 11 my $split_re;
310 11 ▶ if (defined $split) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $split) 8 3 11
311 8 eval {
312 8 $split_re = qr/$split/;
313 };
314 }
315 11 ▶ $split_re //= ' ';
Condition100%2/2
$split_re //= " "
l!l
83
MC/DC100%1/1
$split_re //= " "
$split_re
Truth table100%2/2
A
A= $split_re
Aresult
11
00
316 11 ▶ $limit ||= 0;
Condition100%2/2
$limit ||= 0
l!l
110
MC/DC100%1/1
$limit ||= 0
$limit
Truth table100%2/2
A
A= $limit
Aresult
11
00
317 11 return [split($split_re, $str, $limit)];
318 }
319  
320 8 ▶sub text_chunk {
Subroutine text_chunk: called CC=5 SCAR=16.1
Pod undocumented
321 8 my ($string, $size) = @_;
322 8 my @list;
323 8 ▶ $size ||= 1;
Condition50%1/2
$size ||= 1
l!l
80
MC/DC0%0/1
$size ||= 1
$size
Truth table50%1/2
A
A= $size
Aresult
11
00
324 8 ▶ if ($size < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size < 0) { } 5 3 8
325 # sexeger! It's faster to reverse the string, search
326 # it from the front and then reverse the output than to
327 # search it from the end, believe it nor not!
328 5 $string = reverse $string;
329 5 $size = -$size;
330 5 ▶ unshift(@list, scalar reverse $1)
Branch: 1 branch
BranchTrueFalseTotal
$string =~ /((.{$size})|(.+))/g and do { unshift @list, scalar reverse($1) } 19 5 24
331 while ($string =~ /((.{$size})|(.+))/g);
332 }
333 else {
334 3 ▶ push(@list, $1) while ($string =~ /((.{$size})|(.+))/g);
Branch: 1 branch
BranchTrueFalseTotal
$string =~ /((.{$size})|(.+))/g and do { push @list, $1 } 15 3 18
335 }
336 8 return \@list;
337 }
338  
339 11 ▶sub text_substr {
Subroutine text_substr: called CC=4 SCAR=13.9
Pod undocumented
340 11 my ($text, $offset, $length, $replacement) = @_;
341 11 ▶ $offset ||= 0;
Condition100%2/2
$offset ||= 0
l!l
38
MC/DC100%1/1
$offset ||= 0
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
342  
343 11 ▶ if(defined $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $length) { } 9 2 11
344 9 ▶ if (defined $replacement) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replacement) { } 2 7 9
345 2 substr( $text, $offset, $length, $replacement );
346 2 return $text;
347 }
348 else {
349 7 return substr( $text, $offset, $length );
350 }
351 }
352 else {
353 2 return substr( $text, $offset );
354 }
355 }
356  
357 1 ▶sub text_squote {
Subroutine text_squote: called CC=2 SCAR=6.9
Pod undocumented
358 1 my $text = shift;
359 1 for ($text) {
360 1 s/(['\\])/\\$1/g;
361 }
362 1 return $text;
363 }
364  
365 1 ▶sub text_dquote {
Subroutine text_dquote: called CC=2 SCAR=6.9
Pod undocumented
366 1 my $text = shift;
367 1 for ($text) {
368 1 s/(["\\])/\\$1/g;
369 1 s/\n/\\n/g;
370 }
371 1 return $text;
372 }
373  
374 #========================================================================
375 # hash virtual methods
376 #========================================================================
377  
378  
379 2 ▶sub hash_item {
Subroutine hash_item: called CC=4 SCAR=15.6
Pod undocumented
380 2 my ($hash, $item) = @_;
381 2 ▶ $item //= '';
Condition50%1/2
$item //= ""
l!l
20
MC/DC0%0/1
$item //= ""
$item
Truth table50%1/2
A
A= $item
Aresult
11
00
382 2 ▶ return if $PRIVATE && $item =~ /$PRIVATE/;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::VMethods::PRIVATE and $item =~ /$Template::VMethods::PRIVATE/ 0 2 2
Condition33%1/3
$Template::VMethods::PRIVATE and $item =~ /$Template::VMethods::PRIVATE/
!ll&&!rl&&r
200
MC/DC0%0/2
$Template::VMethods::PRIVATE and $item =~ /$Template::VMethods::PRIVATE/
$Template::VMethods::PRIVATE $item =~ /$Template::VMethods::PRIVATE/
Truth table33%1/3
A and B
A= $Template::VMethods::PRIVATEB= $item =~ /$Template::VMethods::PRIVATE/
ABresult
0X0
100
111
383 2 $hash->{ $item };
384 }
385  
386 0 ▶sub hash_hash {
Subroutine hash_hash: not called CC=1 SCAR=6.9
Pod undocumented
387 0 $_[0];
388 }
389  
390 3 ▶sub hash_size {
Subroutine hash_size: called CC=1 SCAR=0.0
Pod undocumented
391 3 scalar keys %{$_[0]};
392 }
393  
394 2 ▶sub hash_empty {
Subroutine hash_empty: called CC=2 SCAR=6.9
Pod undocumented
395 2 ▶ return 0 == hash_size($_[0]) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
0 == hash_size($_[0]) ? : 1 1 2
396 }
397  
398 1 ▶sub hash_each {
Subroutine hash_each: called CC=1 SCAR=0.0
Pod undocumented
399 # this will be changed in TT3 to do what hash_pairs() does
400 1 [ %{ $_[0] } ];
401 }
402  
403 8 ▶sub hash_keys {
Subroutine hash_keys: called CC=1 SCAR=0.0
Pod undocumented
404 8 [ keys %{ $_[0] } ];
405 }
406  
407 1 ▶sub hash_values {
Subroutine hash_values: called CC=1 SCAR=0.0
Pod undocumented
408 1 [ values %{ $_[0] } ];
409 }
410  
411 7 ▶sub hash_items {
Subroutine hash_items: called CC=1 SCAR=0.0
Pod undocumented
412 7 [ %{ $_[0] } ];
413 }
414  
415 4 ▶sub hash_pairs {
Subroutine hash_pairs: called CC=1 SCAR=0.0
Pod undocumented
416 [ map {
417 12 { key => $_ , value => $_[0]->{ $_ } }
418 }
419 4 sort keys %{ $_[0] }
420 ];
421 }
422  
423 1 ▶sub hash_list {
Subroutine hash_list: called CC=5 SCAR=17.8
Pod undocumented
424 1 my ($hash, $what) = @_;
425 1 ▶ $what ||= '';
Condition50%1/2
$what ||= ""
l!l
01
MC/DC0%0/1
$what ||= ""
$what
Truth table50%1/2
A
A= $what
Aresult
11
00
426 return ($what eq 'keys') ? [ keys %$hash ]
427 : ($what eq 'values') ? [ values %$hash ]
428 : ($what eq 'each') ? [ %$hash ]
429 :
430 [
431 # for now we do what pairs does but this will be changed
432 # in TT3 to return [ $hash ] by default
433 1 ▶ map { { key => $_ , value => $hash->{ $_ } } }
Branch: 3 branches
BranchTrueFalseTotal
$what eq 'keys' ? : 0 1 1
$what eq 'values' ? : 0 1 1
$what eq 'each' ? : 0 1 1
434 sort keys %$hash
435 ];
436 }
437  
438 2 ▶sub hash_exists {
Subroutine hash_exists: called CC=1 SCAR=0.0
Pod undocumented
439 2 exists $_[0]->{ $_[1] };
440 }
441  
442 6 ▶sub hash_defined {
Subroutine hash_defined: called CC=2 SCAR=6.9
Pod undocumented
443 # return the item requested, or 1 if no argument
444 # to indicate that the hash itself is defined
445 6 my $hash = shift;
446 6 ▶ return @_ ? defined $hash->{ $_[0] } : 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 5 1 6
447 }
448  
449 2 ▶sub hash_delete {
Subroutine hash_delete: called CC=2 SCAR=6.9
Pod undocumented
450 2 my $hash = shift;
451 2 delete $hash->{ $_ } for @_;
452 }
453  
454 21 ▶sub hash_import {
Subroutine hash_import: called CC=2 SCAR=6.9
Pod undocumented
455 21 my ($hash, $imp) = @_;
456 21 ▶ if (ref $imp eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $imp eq "HASH") 19 2 21
457 19 @$hash{ keys %$imp } = values %$imp;
458 }
459 21 return '';
460 }
461  
462 1 ▶sub hash_sort {
Subroutine hash_sort: called CC=1 SCAR=0.0
Pod undocumented
463 1 my ($hash) = @_;
464 1 [ sort { lc $hash->{$a} cmp lc $hash->{$b} } (keys %$hash) ];
465 }
466  
467 1 ▶sub hash_nsort {
Subroutine hash_nsort: called CC=1 SCAR=0.0
Pod undocumented
468 1 my ($hash) = @_;
469 1 [ sort { $hash->{$a} <=> $hash->{$b} } (keys %$hash) ];
470 }
471  
472  
473 #========================================================================
474 # list virtual methods
475 #========================================================================
476  
477  
478 8 ▶sub list_item {
Subroutine list_item: called CC=2 SCAR=6.9
Pod undocumented
479 8 ▶ $_[0]->[ $_[1] || 0 ];
Condition100%2/2
$_[1] || 0
l!l
44
MC/DC100%1/1
$_[1] || 0
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
480 }
481  
482 2 ▶sub list_list {
Subroutine list_list: called CC=1 SCAR=0.0
Pod undocumented
483 2 $_[0];
484 }
485  
486 5 ▶sub list_hash {
Subroutine list_hash: called CC=3 SCAR=11.0
Pod undocumented
487 5 my $list = shift;
488 5 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 3 5
489 2 ▶ my $n = shift || 0;
Condition100%2/2
shift() || 0
l!l
11
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
490 2 return { map { ($n++, $_) } @$list };
491 }
492 106 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
493 3 return { @$list };
494 }
495  
496 13 ▶sub list_push {
Subroutine list_push: called CC=1 SCAR=0.0
Pod undocumented
497 13 my $list = shift;
498 13 push(@$list, @_);
499 13 return '';
500 }
501  
502 1 ▶sub list_pop {
Subroutine list_pop: called CC=1 SCAR=0.0
Pod undocumented
503 1 my $list = shift;
504 1 pop(@$list);
505 }
506  
507 1 ▶sub list_unshift {
Subroutine list_unshift: called CC=1 SCAR=0.0
Pod undocumented
508 1 my $list = shift;
509 1 unshift(@$list, @_);
510 1 return '';
511 }
512  
513 2 ▶sub list_shift {
Subroutine list_shift: called CC=1 SCAR=0.0
Pod undocumented
514 2 my $list = shift;
515 2 shift(@$list);
516 }
517  
518 1 ▶sub list_max {
Subroutine list_max: called CC=1 SCAR=0.0
Pod undocumented
519 106 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
520 1 my $list = shift;
521 1 $#$list;
522 }
523  
524 7 ▶sub list_size {
Subroutine list_size: called CC=1 SCAR=0.0
Pod undocumented
525 106 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
526 7 my $list = shift;
527 7 $#$list + 1;
528 }
529  
530 2 ▶sub list_empty {
Subroutine list_empty: called CC=2 SCAR=6.9
Pod undocumented
531 2 ▶ return 0 == list_size($_[0]) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
0 == list_size($_[0]) ? : 1 1 2
532 }
533  
534 4 ▶sub list_defined {
Subroutine list_defined: called CC=3 SCAR=11.0
Pod undocumented
535 # return the item requested, or 1 if no argument to
536 # indicate that the hash itself is defined
537 4 my $list = shift;
538 4 ▶ return 1 unless @_; # list.defined is always true
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 3 4
539 3 ▶ return unless looks_like_number $_[0]; # list.defined('bah') is always false
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($_[0]) 1 2 3
540 2 return defined $list->[$_[0]]; # list.defined(n)
541 }
542  
543 27 ▶sub list_first {
Subroutine list_first: called CC=2 SCAR=6.9
Pod undocumented
544 27 my $list = shift;
545 27 ▶ return $list->[0] unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 26 1 27
546 1 return [ @$list[0..$_[0]-1] ];
547 }
548  
549 25 ▶sub list_last {
Subroutine list_last: called CC=2 SCAR=6.9
Pod undocumented
550 25 my $list = shift;
551 25 ▶ return $list->[-1] unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 24 1 25
552 1 return [ @$list[-$_[0]..-1] ];
553 }
554  
555 1 ▶sub list_reverse {
Subroutine list_reverse: called CC=1 SCAR=0.0
Pod undocumented
556 1 my $list = shift;
557 1 [ reverse @$list ];
558 }
559  
560 10 ▶sub list_grep {
Subroutine list_grep: called CC=2 SCAR=7.1
Pod undocumented
561 10 my ($list, $pattern) = @_;
562 10 ▶ $pattern = '' unless defined $pattern;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pattern 0 10 10
563 10 return [ grep /$pattern/, @$list ];
564 }
565  
566 60 ▶sub list_join {
Subroutine list_join: called CC=3 SCAR=11.1
Pod undocumented
567 60 my ($list, $joint) = @_;
568 60 ▶ join($joint // ' ', map { $_ // '' } @$list);
Condition75%3/4
$joint // " "
l!l
546
$_ // ""
l!l
2620
MC/DC100%1/1
$joint // " "
$joint
MC/DC0%0/1
$_ // ""
$_
Truth table100%2/2
A
A= $joint
Aresult
11
00
Truth table50%1/2
A
A= $_
Aresult
11
00
569 }
570  
571 184 ▶sub _list_sort_make_key {
Subroutine _list_sort_make_key: called CC=5 SCAR=16.2
572 184 my ($item, $fields) = @_;
573 184 my @keys;
574  
575 184 ▶ if (ref($item) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $item eq 'HASH') { } 34 150 184
elsif (builtin::blessed($item)) { } 28 122 150
576 34 @keys = map { $item->{ $_ } } @$fields;
577 }
578 elsif (blessed $item) {
579 28 ▶ @keys = map { $item->can($_) ? $item->$_() : $item } @$fields;
Branch: 1 branch
BranchTrueFalseTotal
$item->can($_) ? : 32 0 32
580 }
581 else {
582 122 @keys = $item;
583 }
584  
585 # ugly hack to generate a single string using a delimiter that is
586 # unlikely (but not impossible) to be found in the wild.
587 184 ▶ return lc join('/*^UNLIKELY^*/', map { $_ // '' } @keys);
Condition50%1/2
$_ // ""
l!l
1880
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
588 }
589  
590 55 ▶sub list_sort {
Subroutine list_sort: called CC=3 SCAR=11.0
Pod undocumented
591 55 my ($list, @fields) = @_;
592 55 ▶ return $list unless @$list > 1; # no need to sort 1 item lists
Branch: 1 branch
BranchTrueFalseTotal
unless @$list > 1 3 52 55
593 return [
594 @fields # Schwartzian Transform
595 46 ? map { $_->[0] } # for case insensitivity
596 48 sort { $a->[1] cmp $b->[1] }
597 46 map { [ $_, _list_sort_make_key($_, \@fields) ] }
598 @$list
599 135 : map { $_->[0] }
600 156 sort { $a->[1] cmp $b->[1] }
601 52 ▶ map { [ $_, lc $_ ] }
Branch: 1 branch
BranchTrueFalseTotal
@fields ? : 13 39 52
602 @$list,
603 ];
604 }
605  
606 6 ▶sub list_nsort {
Subroutine list_nsort: called CC=2 SCAR=6.9
Pod undocumented
607 6 my ($list, @fields) = @_;
608 6 ▶ return $list unless @$list > 1; # no need to sort 1 item lists
Branch: 1 branch
BranchTrueFalseTotal
unless @$list > 1 0 6 6
609  
610 69 ▶ my $sort = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
611 69 my $cmp;
612  
613 69 ▶ if(@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) { } 8 61 69
614 # compare each field individually
615 8 for my $field (@fields) {
616 8 my $A = _list_sort_make_key($a, [ $field ]);
617 8 my $B = _list_sort_make_key($b, [ $field ]);
618 8 ▶ ($cmp = $A <=> $B) and last;
Branch: 1 branch
BranchTrueFalseTotal
$cmp = $A <=> $B and last 8 0 8
619 }
620 }
621 else {
622 61 my $A = _list_sort_make_key($a);
623 61 my $B = _list_sort_make_key($b);
624 61 $cmp = $A <=> $B;
625 }
626  
627 69 $cmp;
628 6 };
629  
630 6 return [ sort $sort @{ $list } ];
631 }
632  
633 2 ▶sub list_unique {
Subroutine list_unique: called CC=1 SCAR=0.0
Pod undocumented
634 2 my %u;
635 2 [ grep { ++$u{$_} == 1 } @{$_[0]} ];
636 }
637  
638 4 ▶sub list_import {
Subroutine list_import: called CC=2 SCAR=7.1
Pod undocumented
639 4 my $list = shift;
640 4 ▶ push(@$list, grep defined, map ref eq 'ARRAY' ? @$_ : undef, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 5 0 5
641 4 return $list;
642 }
643  
644 2 ▶sub list_merge {
Subroutine list_merge: called CC=2 SCAR=7.2
Pod undocumented
645 2 my $list = shift;
646 2 ▶ return [ @$list, grep defined, map ref eq 'ARRAY' ? @$_ : undef, @_ ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 3 0 3
647 }
648  
649 4 ▶sub list_slice {
Subroutine list_slice: called CC=5 SCAR=16.1
Pod undocumented
650 4 my ($list, $from, $to) = @_;
651 4 ▶ $from ||= 0;
Condition100%2/2
$from ||= 0
l!l
22
MC/DC100%1/1
$from ||= 0
$from
Truth table100%2/2
A
A= $from
Aresult
11
00
652 4 ▶ $to //= $#$list;
Condition100%2/2
$to //= $list->$#*
l!l
22
MC/DC100%1/1
$to //= $list->$#*
$to
Truth table100%2/2
A
A= $to
Aresult
11
00
653 4 ▶ $from += @$list if $from < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $from < 0 1 3 4
654 4 ▶ $to += @$list if $to < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $to < 0 1 3 4
655 4 return [ @$list[$from..$to] ];
656 }
657  
658 7 ▶sub list_splice {
Subroutine list_splice: called CC=6 SCAR=17.9
Pod undocumented
659 7 my ($list, $offset, $length, @replace) = @_;
660 7 ▶ if (@replace) {
Branch: 3 branches
BranchTrueFalseTotal
if (@replace) { } 4 3 7
elsif (defined $length) { } 1 2 3
elsif (defined $offset) { } 1 1 2
661 # @replace can contain a list of multiple replace items, or
662 # be a single reference to a list
663 4 ▶ @replace = @{ $replace[0] }
Branch: 1 branch
BranchTrueFalseTotal
if @replace == 1 and ref $replace[0] eq "ARRAY" 2 2 4
Condition100%3/3
@replace == 1 and ref $replace[0] eq "ARRAY"
!ll&&!rl&&r
112
MC/DC100%2/2
@replace == 1 and ref $replace[0] eq "ARRAY"
@replace == 1 ref $replace[0] eq "ARRAY"
Truth table100%3/3
A and B
A= @replace == 1B= ref $replace[0] eq "ARRAY"
ABresult
0X0
100
111
664 if @replace == 1 && ref $replace[0] eq 'ARRAY';
665 4 return [ splice @$list, $offset, $length, @replace ];
666 }
667 elsif (defined $length) {
668 1 return [ splice @$list, $offset, $length ];
669 }
670 elsif (defined $offset) {
671 1 return [ splice @$list, $offset ];
672 }
673 else {
674 1 return [ splice(@$list) ];
675 }
676 }
677  
678 106 1;
679  
680 __END__
« lib/Template/Plugin/List.pm ↑ Summary lib/Template/Plugin/String.pm »