lib/Template/VMethods.pm

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