blib/lib/String/Format.pm

filter: Statementstmt 95.6% 44 / 46 Branchbran 81.8% 18 / 22 Conditioncond 61.5% 8 / 13 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 8 / 8 Podpod 50.0% 1 / 2 totaltotal 80.8% 80 / 99 CC 17 SCAR 20.2
CC
17
Cov
86%
_replace
12.3
stringf
6.0
stringfactory
2.0
CRAP 7.5
↑ Summary
1 package String::Format;
2  
3 # ----------------------------------------------------------------------
4 # Copyright (C) 2002,2009 darren chamberlain <darren@cpan.org>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 # 02110-1301 USA.
19 # -------------------------------------------------------------------
20  
21 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use vars qw($VERSION @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 6 $VERSION = '1.18';
27 6 @EXPORT = qw(stringf);
28  
29 31 ▶sub _replace {
Subroutine _replace: called CC=11 SCAR=25.1
30 31 my ($args, $orig, $alignment, $min_width,
31 $max_width, $passme, $formchar) = @_;
32  
33 # For unknown escapes, return the orignial
34 31 ▶ return $orig unless defined $args->{$formchar};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{$formchar} 4 27 31
35  
36 27 ▶ $alignment = '+' unless defined $alignment;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $alignment 27 0 27
37  
38 27 my $replacement = $args->{$formchar};
39 27 ▶ if (ref $replacement eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $replacement eq "CODE") 3 24 27
40 # $passme gets passed to subrefs.
41 3 ▶ $passme ||= "";
Condition100%2/2
$passme ||= ""
l!l
12
MC/DC100%1/1
$passme ||= ""
$passme
Truth table100%2/2
A
A= $passme
Aresult
11
00
42 3 $passme =~ tr/{}//d;
43 3 $replacement = $replacement->($passme);
44 }
45  
46 27 my $replength = length $replacement;
47 27 ▶ $min_width ||= $replength;
Condition66%2/3
$min_width ||= $replength
l!l&&r!l&&!r
1260
MC/DC0%0/2
$min_width ||= $replength
$min_width $replength
Truth table66%2/3
A ||= B
A= $min_widthB= $replength
ABresult
1X1
011
000
48 27 ▶ $max_width ||= $replength;
Condition66%2/3
$max_width ||= $replength
l!l&&r!l&&!r
1260
MC/DC0%0/2
$max_width ||= $replength
$max_width $replength
Truth table66%2/3
A ||= B
A= $max_widthB= $replength
ABresult
1X1
011
000
49  
50 # length of replacement is between min and max
51 27 ▶ if (($replength > $min_width) && ($replength < $max_width)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replength > $min_width and $replength < $max_width) 0 27 27
Condition33%1/3
$replength > $min_width and $replength < $max_width
!ll&&!rl&&r
2700
MC/DC0%0/2
$replength > $min_width and $replength < $max_width
$replength > $min_width $replength < $max_width
Truth table33%1/3
A and B
A= $replength > $min_widthB= $replength < $max_width
ABresult
0X0
100
111
52 0 return $replacement;
53 }
54  
55 # length of replacement is longer than max; truncate
56 27 ▶ if ($replength > $max_width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replength > $max_width) 1 26 27
57 1 return substr($replacement, 0, $max_width);
58 }
59
60 # length of replacement is less than min: pad
61 26 ▶ if ($alignment eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($alignment eq "-") 0 26 26
62 # left align; pad in front
63 0 return $replacement . " " x ($min_width - $replength);
64 }
65  
66 # right align, pad at end
67 26 return " " x ($min_width - $replength) . $replacement;
68 }
69  
70 6 my $regex = qr/
71 (% # leading '%'
72 (-)? # left-align, rather than right
73 (\d*)? # (optional) minimum field width
74 (?:\.(\d*))? # (optional) maximum field width
75 (\{.*?\})? # (optional) stuff inside
76 (\S) # actual format character
77 )/x;
78 19 ▶sub stringf {
Subroutine stringf: called CC=6 SCAR=17.9
Pod documented
79 19 ▶ my $format = shift || return;
Condition50%1/2
shift() || (return)
l!l
190
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
80 19 ▶ my $args = UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], 'HASH') ? : 12 7 19
81 19 ▶ $args->{'n'} = "\n" unless exists $args->{'n'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"n"} 16 3 19
82 19 ▶ $args->{'t'} = "\t" unless exists $args->{'t'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"t"} 18 1 19
83 19 ▶ $args->{'%'} = "%" unless exists $args->{'%'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"%"} 18 1 19
84  
85 19 $format =~ s/$regex/_replace($args, $1, $2, $3, $4, $5, $6)/ge;
86  
87 19 return $format;
88 }
89  
90 1 ▶sub stringfactory {
Subroutine stringfactory: called CC=2 SCAR=7.0
Pod undocumented
91 1 shift; # It's a class method, but we don't actually want the class
92 1 ▶ my $args = UNIVERSAL::isa($_[0], "HASH") ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], 'HASH') ? : 1 0 1
93 1 ▶ return sub { stringf($_[0], $args) };
Subroutine __ANON__: called CC=1 SCAR=0.0
94 }
95  
96 6 1;
97 __END__
↑ Summary