blib/lib/Template/VMethods.pm untested

filter: Statementstmt 0.0% 0 / 305 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 56 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 75 Podpod 0.0% 0 / 65 totaltotal 0.0% 0 / 597 CC 48 SCAR 22.8
CC
48
Cov
0%
text_split
42.0
list_splice
30.0
text_match
20.0
CRAP 9.8
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 use strict;
22 use warnings;
23 use Scalar::Util qw( blessed looks_like_number );
24 use Template::Filters;
25  
26 our $VERSION = '3.105';
27 our $DEBUG = 0 unless defined $DEBUG;
28  
29 our $ROOT_VMETHODS = {
30 inc => \&root_inc,
31 dec => \&root_dec,
32 };
33  
34 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 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 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 require Template::Stash;
110 our $PRIVATE = $Template::Stash::PRIVATE;
111  
112 #========================================================================
113 # root virtual methods
114 #========================================================================
115  
116 sub root_inc {
117 no warnings;
118 my $item = shift;
119 ++$item;
120 }
121  
122 sub root_dec {
123 no warnings;
124 my $item = shift;
125 --$item;
126 }
127  
128  
129 #========================================================================
130 # text virtual methods
131 #========================================================================
132  
133 sub text_item {
134 $_[0];
135 }
136  
137 sub text_list {
138 [ $_[0] ];
139 }
140  
141 sub text_hash {
142 { value => $_[0] };
143 }
144  
145 sub text_length {
146 length $_[0];
147 }
148  
149 sub text_size {
150 return 1;
151 }
152  
153 sub text_empty {
154 return 0 == text_length($_[0]) ? 1 : 0;
155 }
156  
157 sub text_defined {
158 return 1;
159 }
160  
161 sub text_upper {
162 return uc $_[0];
163 }
164  
165 sub text_lower {
166 return lc $_[0];
167 }
168  
169 sub text_ucfirst {
170 return ucfirst $_[0];
171 }
172  
173 sub text_lcfirst {
174 return lcfirst $_[0];
175 }
176  
177 sub text_trim {
178 my $text = $_[0];
179 $text =~ s/^\s+//;
180 $text =~ s/\s+$//;
181 return $text;
182 }
183  
184 sub text_collapse {
185 my $text = $_[0];
186 $text =~ s/^\s+//;
187 $text =~ s/\s+$//;
188 $text =~ s/\s+/ /g;
189 return $text;
190 }
191  
192 sub text_match {
193 my ($str, $search, $global) = @_;
194 return $str unless defined $str and defined $search;
195 my @matches = $global ? ($str =~ /$search/g)
196 : ($str =~ /$search/);
197 return @matches ? \@matches : '';
198 }
199  
200 sub text_search {
201 my ($str, $pattern) = @_;
202 return $str unless defined $str and defined $pattern;
203 return $str =~ /$pattern/;
204 }
205  
206 sub text_repeat {
207 my ($str, $count) = @_;
208 $str //= '';
209 return '' unless $count;
210 return $str x $count;
211 }
212  
213 sub text_replace {
214 my ($text, $pattern, $replace, $global) = @_;
215 $text //= '';
216 $pattern //= '';
217 $replace //= '';
218 $global //= 1;
219  
220 if ($replace =~ /\$\d+/) {
221 # replacement string may contain backrefs
222 my $expand = sub {
223 my ($chunk, $start, $end) = @_;
224 $chunk =~ s{ \\(\\|\$) | \$ (\d+) }{
225 $1 ? $1
226 : ($2 > $#$start || $2 == 0 || !defined $start->[$2]) ? ''
227 : substr($text, $start->[$2], $end->[$2] - $start->[$2]);
228 }exg;
229 $chunk;
230 };
231 if ($global) {
232 my $prev_end = -1;
233 $text =~ s{$pattern}{
234 my $s = $-[0];
235 my $e = $+[0];
236 if ($s == $e && $s == $prev_end) {
237 '';
238 } else {
239 $prev_end = $e;
240 &$expand($replace, [@-], [@+]);
241 }
242 }eg;
243 }
244 else {
245 $text =~ s{$pattern}{ &$expand($replace, [@-], [@+]) }e;
246 }
247 }
248 else {
249 if ($global) {
250 my $prev_end = -1;
251 $text =~ s{$pattern}{
252 my $s = $-[0];
253 my $e = $+[0];
254 if ($s == $e && $s == $prev_end) {
255 '';
256 } else {
257 $prev_end = $e;
258 $replace;
259 }
260 }eg;
261 }
262 else {
263 $text =~ s/$pattern/$replace/;
264 }
265 }
266 return $text;
267 }
268  
269 sub text_remove {
270 my ($str, $search) = @_;
271 return $str unless defined $str and defined $search;
272 $str =~ s/$search//g;
273 return $str;
274 }
275  
276 sub text_split {
277 my ($str, $split, $limit) = @_;
278 $str //= '';
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 if ($] < 5.018) {
287 if (defined $limit) {
288 return [
289 defined $split
290 ? split($split, $str, $limit)
291 : split(' ', $str, $limit)
292 ];
293 }
294 else {
295 return [
296 defined $split
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 my $split_re;
310 if (defined $split) {
311 eval {
312 $split_re = qr/$split/;
313 };
314 }
315 $split_re //= ' ';
316 $limit ||= 0;
317 return [split($split_re, $str, $limit)];
318 }
319  
320 sub text_chunk {
321 my ($string, $size) = @_;
322 my @list;
323 $size ||= 1;
324 if ($size < 0) {
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 $string = reverse $string;
329 $size = -$size;
330 unshift(@list, scalar reverse $1)
331 while ($string =~ /((.{$size})|(.+))/g);
332 }
333 else {
334 push(@list, $1) while ($string =~ /((.{$size})|(.+))/g);
335 }
336 return \@list;
337 }
338  
339 sub text_substr {
340 my ($text, $offset, $length, $replacement) = @_;
341 $offset ||= 0;
342  
343 if(defined $length) {
344 if (defined $replacement) {
345 substr( $text, $offset, $length, $replacement );
346 return $text;
347 }
348 else {
349 return substr( $text, $offset, $length );
350 }
351 }
352 else {
353 return substr( $text, $offset );
354 }
355 }
356  
357 sub text_squote {
358 my $text = shift;
359 for ($text) {
360 s/(['\\])/\\$1/g;
361 }
362 return $text;
363 }
364  
365 sub text_dquote {
366 my $text = shift;
367 for ($text) {
368 s/(["\\])/\\$1/g;
369 s/\n/\\n/g;
370 }
371 return $text;
372 }
373  
374 #========================================================================
375 # hash virtual methods
376 #========================================================================
377  
378  
379 sub hash_item {
380 my ($hash, $item) = @_;
381 $item //= '';
382 return if $PRIVATE && $item =~ /$PRIVATE/;
383 $hash->{ $item };
384 }
385  
386 sub hash_hash {
387 $_[0];
388 }
389  
390 sub hash_size {
391 scalar keys %{$_[0]};
392 }
393  
394 sub hash_empty {
395 return 0 == hash_size($_[0]) ? 1 : 0;
396 }
397  
398 sub hash_each {
399 # this will be changed in TT3 to do what hash_pairs() does
400 [ %{ $_[0] } ];
401 }
402  
403 sub hash_keys {
404 [ keys %{ $_[0] } ];
405 }
406  
407 sub hash_values {
408 [ values %{ $_[0] } ];
409 }
410  
411 sub hash_items {
412 [ %{ $_[0] } ];
413 }
414  
415 sub hash_pairs {
416 [ map {
417 { key => $_ , value => $_[0]->{ $_ } }
418 }
419 sort keys %{ $_[0] }
420 ];
421 }
422  
423 sub hash_list {
424 my ($hash, $what) = @_;
425 $what ||= '';
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 map { { key => $_ , value => $hash->{ $_ } } }
434 sort keys %$hash
435 ];
436 }
437  
438 sub hash_exists {
439 exists $_[0]->{ $_[1] };
440 }
441  
442 sub hash_defined {
443 # return the item requested, or 1 if no argument
444 # to indicate that the hash itself is defined
445 my $hash = shift;
446 return @_ ? defined $hash->{ $_[0] } : 1;
447 }
448  
449 sub hash_delete {
450 my $hash = shift;
451 delete $hash->{ $_ } for @_;
452 }
453  
454 sub hash_import {
455 my ($hash, $imp) = @_;
456 if (ref $imp eq 'HASH') {
457 @$hash{ keys %$imp } = values %$imp;
458 }
459 return '';
460 }
461  
462 sub hash_sort {
463 my ($hash) = @_;
464 [ sort { lc $hash->{$a} cmp lc $hash->{$b} } (keys %$hash) ];
465 }
466  
467 sub hash_nsort {
468 my ($hash) = @_;
469 [ sort { $hash->{$a} <=> $hash->{$b} } (keys %$hash) ];
470 }
471  
472  
473 #========================================================================
474 # list virtual methods
475 #========================================================================
476  
477  
478 sub list_item {
479 $_[0]->[ $_[1] || 0 ];
480 }
481  
482 sub list_list {
483 $_[0];
484 }
485  
486 sub list_hash {
487 my $list = shift;
488 if (@_) {
489 my $n = shift || 0;
490 return { map { ($n++, $_) } @$list };
491 }
492 no warnings;
493 return { @$list };
494 }
495  
496 sub list_push {
497 my $list = shift;
498 push(@$list, @_);
499 return '';
500 }
501  
502 sub list_pop {
503 my $list = shift;
504 pop(@$list);
505 }
506  
507 sub list_unshift {
508 my $list = shift;
509 unshift(@$list, @_);
510 return '';
511 }
512  
513 sub list_shift {
514 my $list = shift;
515 shift(@$list);
516 }
517  
518 sub list_max {
519 no warnings;
520 my $list = shift;
521 $#$list;
522 }
523  
524 sub list_size {
525 no warnings;
526 my $list = shift;
527 $#$list + 1;
528 }
529  
530 sub list_empty {
531 return 0 == list_size($_[0]) ? 1 : 0;
532 }
533  
534 sub list_defined {
535 # return the item requested, or 1 if no argument to
536 # indicate that the hash itself is defined
537 my $list = shift;
538 return 1 unless @_; # list.defined is always true
539 return unless looks_like_number $_[0]; # list.defined('bah') is always false
540 return defined $list->[$_[0]]; # list.defined(n)
541 }
542  
543 sub list_first {
544 my $list = shift;
545 return $list->[0] unless @_;
546 return [ @$list[0..$_[0]-1] ];
547 }
548  
549 sub list_last {
550 my $list = shift;
551 return $list->[-1] unless @_;
552 return [ @$list[-$_[0]..-1] ];
553 }
554  
555 sub list_reverse {
556 my $list = shift;
557 [ reverse @$list ];
558 }
559  
560 sub list_grep {
561 my ($list, $pattern) = @_;
562 $pattern = '' unless defined $pattern;
563 return [ grep /$pattern/, @$list ];
564 }
565  
566 sub list_join {
567 my ($list, $joint) = @_;
568 join($joint // ' ', map { $_ // '' } @$list);
569 }
570  
571 sub _list_sort_make_key {
572 my ($item, $fields) = @_;
573 my @keys;
574  
575 if (ref($item) eq 'HASH') {
576 @keys = map { $item->{ $_ } } @$fields;
577 }
578 elsif (blessed $item) {
579 @keys = map { $item->can($_) ? $item->$_() : $item } @$fields;
580 }
581 else {
582 @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 return lc join('/*^UNLIKELY^*/', map { $_ // '' } @keys);
588 }
589  
590 sub list_sort {
591 my ($list, @fields) = @_;
592 return $list unless @$list > 1; # no need to sort 1 item lists
593 return [
594 @fields # Schwartzian Transform
595 ? map { $_->[0] } # for case insensitivity
596 sort { $a->[1] cmp $b->[1] }
597 map { [ $_, _list_sort_make_key($_, \@fields) ] }
598 @$list
599 : map { $_->[0] }
600 sort { $a->[1] cmp $b->[1] }
601 map { [ $_, lc $_ ] }
602 @$list,
603 ];
604 }
605  
606 sub list_nsort {
607 my ($list, @fields) = @_;
608 return $list unless @$list > 1; # no need to sort 1 item lists
609  
610 my $sort = sub {
611 my $cmp;
612  
613 if(@fields) {
614 # compare each field individually
615 for my $field (@fields) {
616 my $A = _list_sort_make_key($a, [ $field ]);
617 my $B = _list_sort_make_key($b, [ $field ]);
618 ($cmp = $A <=> $B) and last;
619 }
620 }
621 else {
622 my $A = _list_sort_make_key($a);
623 my $B = _list_sort_make_key($b);
624 $cmp = $A <=> $B;
625 }
626  
627 $cmp;
628 };
629  
630 return [ sort $sort @{ $list } ];
631 }
632  
633 sub list_unique {
634 my %u;
635 [ grep { ++$u{$_} == 1 } @{$_[0]} ];
636 }
637  
638 sub list_import {
639 my $list = shift;
640 push(@$list, grep defined, map ref eq 'ARRAY' ? @$_ : undef, @_);
641 return $list;
642 }
643  
644 sub list_merge {
645 my $list = shift;
646 return [ @$list, grep defined, map ref eq 'ARRAY' ? @$_ : undef, @_ ];
647 }
648  
649 sub list_slice {
650 my ($list, $from, $to) = @_;
651 $from ||= 0;
652 $to //= $#$list;
653 $from += @$list if $from < 0;
654 $to += @$list if $to < 0;
655 return [ @$list[$from..$to] ];
656 }
657  
658 sub list_splice {
659 my ($list, $offset, $length, @replace) = @_;
660 if (@replace) {
661 # @replace can contain a list of multiple replace items, or
662 # be a single reference to a list
663 @replace = @{ $replace[0] }
664 if @replace == 1 && ref $replace[0] eq 'ARRAY';
665 return [ splice @$list, $offset, $length, @replace ];
666 }
667 elsif (defined $length) {
668 return [ splice @$list, $offset, $length ];
669 }
670 elsif (defined $offset) {
671 return [ splice @$list, $offset ];
672 }
673 else {
674 return [ splice(@$list) ];
675 }
676 }
677  
678 1;
679  
680 __END__