lib/Template/Plugin/String.pm

filter: Statementstmt 98.8% 167 / 169 Branchbran 80.0% 40 / 50 Conditioncond 72.9% 27 / 37 MC/DCmcdc 47.6% 10 / 21 Subroutinesub 97.1% 34 / 35 Podpod 83.3% 25 / 30 totaltotal 88.6% 303 / 342 CC 44 SCAR 13.7
CC
44
Cov
91%
new
9.0
output_filter
9.0
text
4.0
CRAP 3.9
« lib/Template/Base.pm ↑ Summary lib/Template/VMethods.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Plugin::String
4 #
5 # DESCRIPTION
6 # Template Toolkit plugin to implement a basic String object.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 # COPYRIGHT
12 # Copyright (C) 2001-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::Plugin::String;
20  
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use base 'Template::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Template::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 use overload q|""| => "text",
27 1 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 our $VERSION = '3.100';
30 1 our $ERROR = '';
31  
32 1 *centre = \*center;
33 1 *append = \*push;
34 1 *prepend = \*unshift;
35  
36 #------------------------------------------------------------------------
37  
38 sub new {
39 71 ▶ my ($class, @args) = @_;
Subroutine new: called CC=9 SCAR=22.0
Pod documented
40 71 ▶ my $context = ref $class ? undef : shift(@args);
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 25 46 71
41 71 ▶ my $config = @args && ref $args[-1] eq 'HASH' ? pop(@args) : { };
Branch: 1 branch
BranchTrueFalseTotal
@args && ref $args[-1] eq 'HASH' ? : 13 58 71
Condition100%3/3
@args and ref $args[-1] eq "HASH"
!ll&&!rl&&r
45413
MC/DC100%2/2
@args and ref $args[-1] eq "HASH"
@args ref $args[-1] eq "HASH"
Truth table100%3/3
A and B
A= @argsB= ref $args[-1] eq "HASH"
ABresult
0X0
100
111
42  
43 71 ▶ $class = ref($class) || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
25460
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
44  
45 my $text = defined $config->{ text }
46 ? $config->{ text }
47 71 ▶ : (@args ? shift(@args) : '');
Branch: 2 branches
BranchTrueFalseTotal
defined $config->{'text'} ? : 5 66 71
@args ? : 62 4 66
48  
49 # print STDERR "text: [$text]\n";
50 # print STDERR "class: [$class]\n";
51  
52 71 my $self = bless {
53 text => $text,
54 filters => [ ],
55 _CONTEXT => $context,
56 }, $class;
57  
58 71 ▶ my $filter = $config->{ filter } || $config->{ filters };
Condition100%3/3
$config->{'filter'} || $config->{'filters'}
l!l&&r!l&&!r
3563
MC/DC100%2/2
$config->{'filter'} || $config->{'filters'}
$config->{'filter'} $config->{'filters'}
Truth table100%3/3
A || B
A= $config->{'filter'}B= $config->{'filters'}
ABresult
1X1
011
000
59  
60 # install any output filters specified as 'filter' or 'filters' option
61 71 ▶ $self->output_filter($filter)
Branch: 1 branch
BranchTrueFalseTotal
if $filter 8 63 71
62 if $filter;
63  
64 71 return $self;
65 }
66  
67  
68 sub text {
69 70 ▶ my $self = shift;
Subroutine text: called CC=4 SCAR=13.9
Pod documented
70 70 ▶ return $self->{ text } unless @{ $self->{ filters } };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"filters"};} 60 10 70
71  
72 10 my $text = $self->{ text };
73 10 my $context = $self->{ _CONTEXT };
74  
75 10 foreach my $dispatch (@{ $self->{ filters } }) {
76 18 my ($name, $args) = @$dispatch;
77 18 ▶ my $code = $context->filter($name, $args)
Condition33%1/3
$context->filter($name, $args) || $self->throw($context->error)
l!l&&r!l&&!r
1800
MC/DC0%0/2
$context->filter($name, $args) || $self->throw($context->error)
$context->filter($name, $args) $self->throw($context->error)
Truth table33%1/3
A || B
A= $context->filter($name, $args)B= $self->throw($context->error)
ABresult
1X1
011
000
78 || $self->throw($context->error());
79 18 $text = &$code($text);
80 }
81 10 return $text;
82 }
83  
84  
85 sub copy {
86 14 ▶ my $self = shift;
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
87 14 $self->new($self->{ text });
88 }
89  
90  
91 sub throw {
92 0 ▶ my $self = shift;
Subroutine throw: not called CC=1 SCAR=6.9
Pod undocumented
93  
94 0 die (Template::Exception->new('String', join('', @_)));
95 }
96  
97  
98 #------------------------------------------------------------------------
99 # output_filter($filter)
100 #
101 # Install automatic output filter(s) for the string. $filter can a list:
102 # [ 'name1', 'name2' => [ ..args.. ], name4 => { ..args.. } ] or a hash
103 # { name1 => '', name2 => [ args ], name3 => { args } }
104 #------------------------------------------------------------------------
105  
106 sub output_filter {
107 10 ▶ my ($self, $filter) = @_;
Subroutine output_filter: called CC=9 SCAR=22.0
Pod undocumented
108 10 my ($name, $args, $dispatch);
109 10 my $filters = $self->{ filters };
110 10 my $count = 0;
111  
112 10 ▶ if (ref $filter eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $filter eq 'HASH') { } 2 8 10
elsif (ref $filter ne 'ARRAY') { } 6 2 8
113 2 $filter = [ %$filter ];
114 }
115 elsif (ref $filter ne 'ARRAY') {
116 6 $filter = [ split(/\s*\W+\s*/, $filter) ];
117 }
118  
119 10 ▶ while (@$filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$filter) 17 10 27
120 17 $name = shift @$filter;
121  
122 # args may follow as a reference (or empty string, e.g. { foo => '' }
123 17 ▶ if (@$filter && (ref($filter->[0]) || ! length $filter->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$filter and ref $filter->[0] || !CORE::length($filter->[0])) { } 5 12 17
Condition100%6/6
@$filter and ref $filter->[0] || !CORE::length($filter->[0])
!ll&&!rl&&r
845
ref $filter->[0] or not CORE::length $filter->[0]
l!l&&r!l&&!r
324
MC/DC100%3/3
@$filter and ref $filter->[0] || !CORE::length($filter->[0])
@$filter ref $filter->[0] not CORE::length $filter->[0]
Truth table100%4/4
A and B or not(C)
A= @$filterB= ref $filter->[0]C= not CORE::length $filter->[0]
ABCresult
0XX0
1000
11X1
1011
124 5 $args = shift @$filter;
125 5 ▶ if ($args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) { } 3 2 5
126 3 ▶ $args = [ $args ] unless ref $args eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "ARRAY" 0 3 3
127 }
128 else {
129 2 $args = [ ];
130 }
131 }
132 else {
133 12 $args = [ ];
134 }
135  
136 # $self->DEBUG("adding output filter $name(@$args)\n");
137  
138 17 push(@$filters, [ $name, $args ]);
139 17 $count++;
140 }
141  
142 10 return '';
143 }
144  
145  
146 #------------------------------------------------------------------------
147  
148 sub push {
149 6 ▶ my $self = shift;
Subroutine push: called CC=1 SCAR=0.0
Pod documented
150 6 $self->{ text } .= join('', @_);
151 6 return $self;
152 }
153  
154  
155 sub unshift {
156 2 ▶ my $self = shift;
Subroutine unshift: called CC=1 SCAR=0.0
Pod documented
157 2 $self->{ text } = join('', @_) . $self->{ text };
158 2 return $self;
159 }
160  
161  
162 sub pop {
163 1 ▶ my $self = shift;
Subroutine pop: called CC=2 SCAR=7.0
Pod documented
164 1 ▶ my $strip = shift || return $self;
Condition50%1/2
CORE::shift() || (return $self)
l!l
10
MC/DC0%0/1
CORE::shift() || (return $self)
CORE::shift()
Truth table50%1/2
A
A= CORE::shift()
Aresult
11
00
165 1 $self->{ text } =~ s/$strip$//;
166 1 return $self;
167 }
168  
169  
170 sub shift {
171 1 ▶ my $self = shift;
Subroutine shift: called CC=2 SCAR=7.0
Pod documented
172 1 ▶ my $strip = shift || return $self;
Condition50%1/2
CORE::shift() || (return $self)
l!l
10
MC/DC0%0/1
CORE::shift() || (return $self)
CORE::shift()
Truth table50%1/2
A
A= CORE::shift()
Aresult
11
00
173 1 $self->{ text } =~ s/^$strip//;
174 1 return $self;
175 }
176  
177 #------------------------------------------------------------------------
178  
179 sub center {
180 2 ▶ my ($self, $width) = @_;
Subroutine center: called CC=3 SCAR=11.1
Pod documented
181 2 my $text = $self->{ text };
182 2 my $len = length $text;
183 2 ▶ $width ||= 0;
Condition50%1/2
$width ||= 0
l!l
20
MC/DC0%0/1
$width ||= 0
$width
Truth table50%1/2
A
A= $width
Aresult
11
00
184  
185 2 ▶ if ($len < $width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < $width) 2 0 2
186 2 my $lpad = int(($width - $len) / 2);
187 2 my $rpad = $width - $len - $lpad;
188 2 $self->{ text } = (' ' x $lpad) . $self->{ text } . (' ' x $rpad);
189 }
190  
191 2 return $self;
192 }
193  
194  
195 sub left {
196 1 ▶ my ($self, $width) = @_;
Subroutine left: called CC=3 SCAR=11.3
Pod documented
197 1 my $len = length $self->{ text };
198 1 ▶ $width ||= 0;
Condition50%1/2
$width ||= 0
l!l
10
MC/DC0%0/1
$width ||= 0
$width
Truth table50%1/2
A
A= $width
Aresult
11
00
199  
200 1 ▶ $self->{ text } .= (' ' x ($width - $len))
Branch: 1 branch
BranchTrueFalseTotal
if $width > $len 1 0 1
201 if $width > $len;
202  
203 1 return $self;
204 }
205  
206  
207 sub right {
208 1 ▶ my ($self, $width) = @_;
Subroutine right: called CC=3 SCAR=11.3
Pod documented
209 1 my $len = length $self->{ text };
210 1 ▶ $width ||= 0;
Condition50%1/2
$width ||= 0
l!l
10
MC/DC0%0/1
$width ||= 0
$width
Truth table50%1/2
A
A= $width
Aresult
11
00
211  
212 $self->{ text } = (' ' x ($width - $len)) . $self->{ text }
213 1 ▶ if $width > $len;
Branch: 1 branch
BranchTrueFalseTotal
if $width > $len 1 0 1
214  
215 1 return $self;
216 }
217  
218  
219 sub format {
220 1 ▶ my ($self, $format) = @_;
Subroutine format: called CC=2 SCAR=7.0
Pod documented
221 1 ▶ $format = '%s' unless defined $format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 0 1 1
222 1 $self->{ text } = sprintf($format, $self->{ text });
223 1 return $self;
224 }
225  
226  
227 sub filter {
228 2 ▶ my ($self, $name, @args) = @_;
Subroutine filter: called CC=2 SCAR=7.4
Pod undocumented
229  
230 2 my $context = $self->{ _CONTEXT };
231  
232 2 ▶ my $code = $context->filter($name, \@args)
Condition33%1/3
$context->filter($name, \@args) || $self->throw($context->error)
l!l&&r!l&&!r
200
MC/DC0%0/2
$context->filter($name, \@args) || $self->throw($context->error)
$context->filter($name, \@args) $self->throw($context->error)
Truth table33%1/3
A || B
A= $context->filter($name, \@args)B= $self->throw($context->error)
ABresult
1X1
011
000
233 || $self->throw($context->error());
234 2 return &$code($self->{ text });
235 }
236  
237  
238 #------------------------------------------------------------------------
239  
240 sub upper {
241 1 ▶ my $self = CORE::shift;
Subroutine upper: called CC=1 SCAR=0.0
Pod documented
242 1 $self->{ text } = uc $self->{ text };
243 1 return $self;
244 }
245  
246  
247 sub lower {
248 5 ▶ my $self = CORE::shift;
Subroutine lower: called CC=1 SCAR=0.0
Pod documented
249 5 $self->{ text } = lc $self->{ text };
250 5 return $self;
251 }
252  
253  
254 sub capital {
255 1 ▶ my $self = CORE::shift;
Subroutine capital: called CC=1 SCAR=0.0
Pod documented
256 1 $self->{ text } =~ s/^(.)/\U$1/;
257 1 return $self;
258 }
259  
260 #------------------------------------------------------------------------
261  
262 sub chop {
263 2 ▶ my $self = CORE::shift;
Subroutine chop: called CC=1 SCAR=0.0
Pod documented
264 2 chop $self->{ text };
265 2 return $self;
266 }
267  
268  
269 sub chomp {
270 1 ▶ my $self = CORE::shift;
Subroutine chomp: called CC=1 SCAR=0.0
Pod documented
271 1 chomp $self->{ text };
272 1 return $self;
273 }
274  
275  
276 sub trim {
277 1 ▶ my $self = CORE::shift;
Subroutine trim: called CC=2 SCAR=6.9
Pod documented
278 1 for ($self->{ text }) {
279 1 s/^\s+//;
280 1 s/\s+$//;
281 }
282 1 return $self;
283 }
284  
285  
286 sub collapse {
287 1 ▶ my $self = CORE::shift;
Subroutine collapse: called CC=2 SCAR=6.9
Pod documented
288 1 for ($self->{ text }) {
289 1 s/^\s+//;
290 1 s/\s+$//;
291 1 s/\s+/ /g
292 }
293 1 return $self;
294  
295 }
296  
297 #------------------------------------------------------------------------
298  
299 sub length {
300 1 ▶ my $self = CORE::shift;
Subroutine length: called CC=1 SCAR=0.0
Pod documented
301 1 return length $self->{ text };
302 }
303  
304  
305 sub truncate {
306 3 ▶ my ($self, $length, $suffix) = @_;
Subroutine truncate: called CC=4 SCAR=13.9
Pod documented
307 3 ▶ return $self unless defined $length;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $length 0 3 3
308 3 ▶ $suffix ||= '';
Condition100%2/2
$suffix ||= ""
l!l
21
MC/DC100%1/1
$suffix ||= ""
$suffix
Truth table100%2/2
A
A= $suffix
Aresult
11
00
309 3 ▶ return $self if CORE::length $self->{ text } <= $length;
Branch: 1 branch
BranchTrueFalseTotal
if CORE::length $self->{"text"} <= $length 1 2 3
310 2 $self->{ text } = CORE::substr($self->{ text }, 0,
311 $length - CORE::length($suffix)) . $suffix;
312 2 return $self;
313 }
314  
315  
316 sub substr {
317 6 ▶ my ($self, $offset, $length, $replacement) = @_;
Subroutine substr: called CC=4 SCAR=13.9
Pod undocumented
318 6 ▶ $offset ||= 0;
Condition100%2/2
$offset ||= 0
l!l
33
MC/DC100%1/1
$offset ||= 0
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
319  
320 6 ▶ if(defined $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $length) { } 4 2 6
321 4 ▶ if (defined $replacement) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replacement) { } 2 2 4
322 2 my $removed = CORE::substr( $self->{text}, $offset, $length );
323 2 CORE::substr( $self->{text}, $offset, $length ) = $replacement;
324 2 return $removed;
325 }
326 else {
327 2 return CORE::substr( $self->{text}, $offset, $length );
328 }
329 }
330 else {
331 2 return CORE::substr( $self->{text}, $offset );
332 }
333 }
334  
335  
336 sub repeat {
337 1 ▶ my ($self, $n) = @_;
Subroutine repeat: called CC=2 SCAR=7.0
Pod documented
338 1 ▶ return $self unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 1 1
339 1 $self->{ text } = $self->{ text } x $n;
340 1 return $self;
341 }
342  
343  
344 sub replace {
345 1 ▶ my ($self, $search, $replace) = @_;
Subroutine replace: called CC=3 SCAR=11.3
Pod documented
346 1 ▶ return $self unless defined $search;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $search 0 1 1
347 1 ▶ $replace = '' unless defined $replace;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $replace 0 1 1
348 1 $self->{ text } =~ s/$search/$replace/g;
349 1 return $self;
350 }
351  
352  
353 sub remove {
354 2 ▶ my ($self, $search) = @_;
Subroutine remove: called CC=2 SCAR=7.0
Pod documented
355 2 ▶ $search = '' unless defined $search;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $search 0 2 2
356 2 $self->{ text } =~ s/$search//g;
357 2 return $self;
358 }
359  
360  
361 sub split {
362 5 ▶ my $self = CORE::shift;
Subroutine split: called CC=3 SCAR=11.0
Pod documented
363 5 my $split = CORE::shift;
364 5 ▶ my $limit = CORE::shift || 0;
Condition100%2/2
CORE::shift() || 0
l!l
14
MC/DC100%1/1
CORE::shift() || 0
CORE::shift()
Truth table100%2/2
A
A= CORE::shift()
Aresult
11
00
365 5 ▶ $split = '\s+' unless defined $split;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $split 1 4 5
366 5 return [ split($split, $self->{ text }, $limit) ];
367 }
368  
369  
370 sub search {
371 4 ▶ my ($self, $pattern) = @_;
Subroutine search: called CC=1 SCAR=0.0
Pod documented
372 4 return $self->{ text } =~ /$pattern/;
373 }
374  
375  
376 sub equals {
377 1 ▶ my ($self, $comparison) = @_;
Subroutine equals: called CC=1 SCAR=0.0
Pod undocumented
378 1 return $self->{ text } eq $comparison;
379 }
380  
381  
382 1 1;
383  
384 __END__
« lib/Template/Base.pm ↑ Summary lib/Template/VMethods.pm »