blib/lib/Template/VMethods.pm untested

filter: Statementstmt 0.0% 0 / 304 Branchbran 0.0% 0 / 120 Conditioncond 0.0% 0 / 33 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 75 Podpod 0.0% 0 / 65 totaltotal 0.0% 0 / 597 CC 60 SCAR 28.0
CC
60
Cov
0%
text_replace
72.0
text_split
72.0
_list_sort_make_key
30.0
CRAP 16.5
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.100';
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 for ($_[0]) {
179 s/^\s+//;
180 s/\s+$//;
181 }
182 return $_[0];
183 }
184  
185 sub text_collapse {
186 for ($_[0]) {
187 s/^\s+//;
188 s/\s+$//;
189 s/\s+/ /g
190 }
191 return $_[0];
192 }
193  
194 sub text_match {
195 my ($str, $search, $global) = @_;
196 return $str unless defined $str and defined $search;
197 my @matches = $global ? ($str =~ /$search/g)
198 : ($str =~ /$search/);
199 return @matches ? \@matches : '';
200 }
201  
202 sub text_search {
203 my ($str, $pattern) = @_;
204 return $str unless defined $str and defined $pattern;
205 return $str =~ /$pattern/;
206 }
207  
208 sub text_repeat {
209 my ($str, $count) = @_;
210 $str = '' unless defined $str;
211 return '' unless $count;
212 $count ||= 1;
213 return $str x $count;
214 }
215  
216 sub text_replace {
217 my ($text, $pattern, $replace, $global) = @_;
218 $text = '' unless defined $text;
219 $pattern = '' unless defined $pattern;
220 $replace = '' unless defined $replace;
221 $global = 1 unless defined $global;
222  
223 if ($replace =~ /\$\d+/) {
224 # replacement string may contain backrefs
225 my $expand = sub {
226 my ($chunk, $start, $end) = @_;
227 $chunk =~ s{ \\(\\|\$) | \$ (\d+) }{
228 $1 ? $1
229 : ($2 > $#$start || $2 == 0 || !defined $start->[$2]) ? ''
230 : substr($text, $start->[$2], $end->[$2] - $start->[$2]);
231 }exg;
232 $chunk;
233 };
234 if ($global) {
235 $text =~ s{$pattern}{ &$expand($replace, [@-], [@+]) }eg;
236 }
237 else {
238 $text =~ s{$pattern}{ &$expand($replace, [@-], [@+]) }e;
239 }
240 }
241 else {
242 if ($global) {
243 $text =~ s/$pattern/$replace/g;
244 }
245 else {
246 $text =~ s/$pattern/$replace/;
247 }
248 }
249 return $text;
250 }
251  
252 sub text_remove {
253 my ($str, $search) = @_;
254 return $str unless defined $str and defined $search;
255 $str =~ s/$search//g;
256 return $str;
257 }
258  
259 sub text_split {
260 my ($str, $split, $limit) = @_;
261 $str = '' unless defined $str;
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 if ($] < 5.018) {
270 if (defined $limit) {
271 return [
272 defined $split
273 ? split($split, $str, $limit)
274 : split(' ', $str, $limit)
275 ];
276 }
277 else {
278 return [
279 defined $split
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 my $split_re;
293 if (defined $split) {
294 eval {
295 $split_re = qr/$split/;
296 };
297 }
298 $split_re = ' ' unless defined $split_re;
299 $limit ||= 0;
300 return [split($split_re, $str, $limit)];
301 }
302  
303 sub text_chunk {
304 my ($string, $size) = @_;
305 my @list;
306 $size ||= 1;
307 if ($size < 0) {
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 $string = reverse $string;
312 $size = -$size;
313 unshift(@list, scalar reverse $1)
314 while ($string =~ /((.{$size})|(.+))/g);
315 }
316 else {
317 push(@list, $1) while ($string =~ /((.{$size})|(.+))/g);
318 }
319 return \@list;
320 }
321  
322 sub text_substr {
323 my ($text, $offset, $length, $replacement) = @_;
324 $offset ||= 0;
325  
326 if(defined $length) {
327 if (defined $replacement) {
328 substr( $text, $offset, $length, $replacement );
329 return $text;
330 }
331 else {
332 return substr( $text, $offset, $length );
333 }
334 }
335 else {
336 return substr( $text, $offset );
337 }
338 }
339  
340 sub text_squote {
341 my $text = shift;
342 for ($text) {
343 s/(['\\])/\\$1/g;
344 }
345 return $text;
346 }
347  
348 sub text_dquote {
349 my $text = shift;
350 for ($text) {
351 s/(["\\])/\\$1/g;
352 s/\n/\\n/g;
353 }
354 return $text;
355 }
356  
357 #========================================================================
358 # hash virtual methods
359 #========================================================================
360  
361  
362 sub hash_item {
363 my ($hash, $item) = @_;
364 $item = '' unless defined $item;
365 return if $PRIVATE && $item =~ /$PRIVATE/;
366 $hash->{ $item };
367 }
368  
369 sub hash_hash {
370 $_[0];
371 }
372  
373 sub hash_size {
374 scalar keys %{$_[0]};
375 }
376  
377 sub hash_empty {
378 return 0 == hash_size($_[0]) ? 1 : 0;
379 }
380  
381 sub hash_each {
382 # this will be changed in TT3 to do what hash_pairs() does
383 [ %{ $_[0] } ];
384 }
385  
386 sub hash_keys {
387 [ keys %{ $_[0] } ];
388 }
389  
390 sub hash_values {
391 [ values %{ $_[0] } ];
392 }
393  
394 sub hash_items {
395 [ %{ $_[0] } ];
396 }
397  
398 sub hash_pairs {
399 [ map {
400 { key => $_ , value => $_[0]->{ $_ } }
401 }
402 sort keys %{ $_[0] }
403 ];
404 }
405  
406 sub hash_list {
407 my ($hash, $what) = @_;
408 $what ||= '';
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 map { { key => $_ , value => $hash->{ $_ } } }
417 sort keys %$hash
418 ];
419 }
420  
421 sub hash_exists {
422 exists $_[0]->{ $_[1] };
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 my $hash = shift;
429 return @_ ? defined $hash->{ $_[0] } : 1;
430 }
431  
432 sub hash_delete {
433 my $hash = shift;
434 delete $hash->{ $_ } for @_;
435 }
436  
437 sub hash_import {
438 my ($hash, $imp) = @_;
439 if (ref $imp eq 'HASH') {
440 @$hash{ keys %$imp } = values %$imp;
441 }
442 return '';
443 }
444  
445 sub hash_sort {
446 my ($hash) = @_;
447 [ sort { lc $hash->{$a} cmp lc $hash->{$b} } (keys %$hash) ];
448 }
449  
450 sub hash_nsort {
451 my ($hash) = @_;
452 [ sort { $hash->{$a} <=> $hash->{$b} } (keys %$hash) ];
453 }
454  
455  
456 #========================================================================
457 # list virtual methods
458 #========================================================================
459  
460  
461 sub list_item {
462 $_[0]->[ $_[1] || 0 ];
463 }
464  
465 sub list_list {
466 $_[0];
467 }
468  
469 sub list_hash {
470 my $list = shift;
471 if (@_) {
472 my $n = shift || 0;
473 return { map { ($n++, $_) } @$list };
474 }
475 no warnings;
476 return { @$list };
477 }
478  
479 sub list_push {
480 my $list = shift;
481 push(@$list, @_);
482 return '';
483 }
484  
485 sub list_pop {
486 my $list = shift;
487 pop(@$list);
488 }
489  
490 sub list_unshift {
491 my $list = shift;
492 unshift(@$list, @_);
493 return '';
494 }
495  
496 sub list_shift {
497 my $list = shift;
498 shift(@$list);
499 }
500  
501 sub list_max {
502 no warnings;
503 my $list = shift;
504 $#$list;
505 }
506  
507 sub list_size {
508 no warnings;
509 my $list = shift;
510 $#$list + 1;
511 }
512  
513 sub list_empty {
514 return 0 == list_size($_[0]) ? 1 : 0;
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 my $list = shift;
521 return 1 unless @_; # list.defined is always true
522 return unless looks_like_number $_[0]; # list.defined('bah') is always false
523 return defined $list->[$_[0]]; # list.defined(n)
524 }
525  
526 sub list_first {
527 my $list = shift;
528 return $list->[0] unless @_;
529 return [ @$list[0..$_[0]-1] ];
530 }
531  
532 sub list_last {
533 my $list = shift;
534 return $list->[-1] unless @_;
535 return [ @$list[-$_[0]..-1] ];
536 }
537  
538 sub list_reverse {
539 my $list = shift;
540 [ reverse @$list ];
541 }
542  
543 sub list_grep {
544 my ($list, $pattern) = @_;
545 $pattern = '' unless defined $pattern;
546 return [ grep /$pattern/, @$list ];
547 }
548  
549 sub list_join {
550 my ($list, $joint) = @_;
551 join(defined $joint ? $joint : ' ',
552 map { defined $_ ? $_ : '' } @$list);
553 }
554  
555 sub _list_sort_make_key {
556 my ($item, $fields) = @_;
557 my @keys;
558  
559 if (ref($item) eq 'HASH') {
560 @keys = map { $item->{ $_ } } @$fields;
561 }
562 elsif (blessed $item) {
563 @keys = map { $item->can($_) ? $item->$_() : $item } @$fields;
564 }
565 else {
566 @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 return lc join('/*^UNLIKELY^*/', map { defined $_ ? $_ : '' } @keys);
572 }
573  
574 sub list_sort {
575 my ($list, @fields) = @_;
576 return $list unless @$list > 1; # no need to sort 1 item lists
577 return [
578 @fields # Schwartzian Transform
579 ? map { $_->[0] } # for case insensitivity
580 sort { $a->[1] cmp $b->[1] }
581 map { [ $_, _list_sort_make_key($_, \@fields) ] }
582 @$list
583 : map { $_->[0] }
584 sort { $a->[1] cmp $b->[1] }
585 map { [ $_, lc $_ ] }
586 @$list,
587 ];
588 }
589  
590 sub list_nsort {
591 my ($list, @fields) = @_;
592 return $list unless @$list > 1; # no need to sort 1 item lists
593  
594 my $sort = sub {
595 my $cmp;
596  
597 if(@fields) {
598 # compare each field individually
599 for my $field (@fields) {
600 my $A = _list_sort_make_key($a, [ $field ]);
601 my $B = _list_sort_make_key($b, [ $field ]);
602 ($cmp = $A <=> $B) and last;
603 }
604 }
605 else {
606 my $A = _list_sort_make_key($a);
607 my $B = _list_sort_make_key($b);
608 $cmp = $A <=> $B;
609 }
610  
611 $cmp;
612 };
613  
614 return [ sort $sort @{ $list } ];
615 }
616  
617 sub list_unique {
618 my %u;
619 [ grep { ++$u{$_} == 1 } @{$_[0]} ];
620 }
621  
622 sub list_import {
623 my $list = shift;
624 push(@$list, grep defined, map ref eq 'ARRAY' ? @$_ : undef, @_);
625 return $list;
626 }
627  
628 sub list_merge {
629 my $list = shift;
630 return [ @$list, grep defined, map ref eq 'ARRAY' ? @$_ : undef, @_ ];
631 }
632  
633 sub list_slice {
634 my ($list, $from, $to) = @_;
635 $from ||= 0;
636 $to = $#$list unless defined $to;
637 $from += @$list if $from < 0;
638 $to += @$list if $to < 0;
639 return [ @$list[$from..$to] ];
640 }
641  
642 sub list_splice {
643 my ($list, $offset, $length, @replace) = @_;
644 if (@replace) {
645 # @replace can contain a list of multiple replace items, or
646 # be a single reference to a list
647 @replace = @{ $replace[0] }
648 if @replace == 1 && ref $replace[0] eq 'ARRAY';
649 return [ splice @$list, $offset, $length, @replace ];
650 }
651 elsif (defined $length) {
652 return [ splice @$list, $offset, $length ];
653 }
654 elsif (defined $offset) {
655 return [ splice @$list, $offset ];
656 }
657 else {
658 return [ splice(@$list) ];
659 }
660 }
661  
662 1;
663  
664 __END__