blib/lib/PPrint.pm

filter: Statementstmt 72.1% 119 / 165 Branchbran 72.9% 35 / 48 Conditioncond 56.6% 17 / 30 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 68.9% 20 / 29 Podpod 57.1% 8 / 14 totaltotal 68.8% 208 / 302 CC 39 SCAR 25.2
CC
39
Cov
70%
A
72.0
__ANON__
8.1
R
8.1
CRAP 12.4
↑ Summary
1 package PPrint;
2 1 require 5.005_62;
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Data::Dumper; # need this for the A directive
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
11 1 our $VERSION = "0.1";
12 1 @ISA = qw( Exporter );
13 1 @EXPORT = qw( &pprint );
14 1 %EXPORT_TAGS = qw( );
15 1 @EXPORT_OK = qw( );
16 }
17  
18 3 ▶sub tilde {
Subroutine tilde: called CC=2 SCAR=6.9
Pod undocumented
19 3 my @params = @{ $_[0] };
20 3 ▶ my $repeat = $params[0] || 1;
Condition100%2/2
$params[0] || 1
l!l
21
MC/DC100%1/1
$params[0] || 1
$params[0]
Truth table100%2/2
A
A= $params[0]
Aresult
11
00
21 3 ▶ return sub { '~' x $repeat };
Subroutine __ANON__: called CC=1 SCAR=0.0
22 }
23  
24 4 ▶sub R {
Subroutine R: called CC=8 SCAR=20.9
Pod documented
25 4 my ($params, $flags) = @_;
26 4 ▶ my $radix = $params->[0] || 10;
Condition100%2/2
$params->[0] || 10
l!l
22
MC/DC100%1/1
$params->[0] || 10
$params->[0]
Truth table100%2/2
A
A= $params->[0]
Aresult
11
00
27 4 ▶ carp "Nonsense radix: $radix" if $radix < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $radix < 1 0 4 4
28 4 ▶ my $mincol = $params->[1] || 0;
Condition100%2/2
$params->[1] || 0
l!l
22
MC/DC100%1/1
$params->[1] || 0
$params->[1]
Truth table100%2/2
A
A= $params->[1]
Aresult
11
00
29 4 ▶ carp "Invalid minimum numbers of columns: $mincol" if $mincol < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $mincol < 0 0 4 4
30 4 ▶ my $padchar = defined $params->[2] ? $params->[2] : " ";
Branch: 1 branch
BranchTrueFalseTotal
defined $params->[2] ? : 1 3 4
31 4 ▶ my $commachar = $params->[3] || ',';
Condition100%2/2
$params->[3] || ','
l!l
13
MC/DC100%1/1
$params->[3] || ','
$params->[3]
Truth table100%2/2
A
A= $params->[3]
Aresult
11
00
32 4 ▶ my $commainterval = $params->[4] || 3;
Condition100%2/2
$params->[4] || 3
l!l
13
MC/DC100%1/1
$params->[4] || 3
$params->[4]
Truth table100%2/2
A
A= $params->[4]
Aresult
11
00
33  
34 4 ▶ return sub {
Subroutine __ANON__: called CC=8 SCAR=20.9
35 4 my @args = @{ $_[0] };
36 4 my $num = shift @args;
37 4 my $str = toStringRadix(abs $num, $radix);
38 4 ▶ if ($flags->{":"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags->{":"}) 1 3 4
39 # add in commas
40 1 ▶ $str = reverse join $commachar, grep { defined $_ && $_ ne '' } split /(.{$commainterval})/, reverse $str;
Condition66%2/3
defined $_ && $_ ne ''
!ll&&!rl&&r
022
MC/DC50%1/2
defined $_ && $_ ne ''
defined $_ $_ ne ''
Truth table66%2/3
A && B
A= defined $_B= $_ ne ''
ABresult
0X0
100
111
41 }
42 4 ▶ $str = "-" . $str if $num < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num < 0 1 3 4
43 4 ▶ $str = "+" . $str if ($num > 0) && (defined($flags->{";"}));
Branch: 1 branch
BranchTrueFalseTotal
if $num > 0 and defined $flags->{";"} 1 3 4
Condition100%3/3
$num > 0 and defined $flags->{";"}
!ll&&!rl&&r
121
MC/DC100%2/2
$num > 0 and defined $flags->{";"}
$num > 0 defined $flags->{";"}
Truth table100%3/3
A and B
A= $num > 0B= defined $flags->{";"}
ABresult
0X0
100
111
44 4 ▶ if (length($str) < $mincol) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str < $mincol) 2 2 4
45 2 my $padding = $padchar x ($mincol - length($str));
46 2 ▶ if ($flags->{"!"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags->{'!'}) { } 0 2 2
47 0 $str = $str . $padding;
48 } else {
49 2 $str = $padding . $str;
50 }
51 }
52 4 return $str;
53 }
54 4 }
55  
56 0 ▶sub D {
Subroutine D: not called CC=1 SCAR=6.9
Pod documented
57 0 my ($params, $flags) = @_;
58 0 unshift @{ $params }, 10;
59 0 return R(@_);
60 }
61  
62 0 ▶sub O {
Subroutine O: not called CC=1 SCAR=6.9
Pod documented
63 0 my ($params, $flags) = @_;
64 0 unshift @{ $params }, 8;
65 0 return R(@_);
66 }
67  
68 0 ▶sub X {
Subroutine X: not called CC=1 SCAR=6.9
Pod documented
69 0 my ($params, $flags) = @_;
70 0 unshift @{ $params }, 16;
71 0 return R(@_);
72 }
73  
74 0 ▶sub B {
Subroutine B: not called CC=1 SCAR=6.9
Pod documented
75 0 my ($params, $flags) = @_;
76 0 unshift @{ $params }, 2;
77 0 return R(@_);
78 }
79  
80 0 ▶sub S {
Subroutine S: not called CC=1 SCAR=6.9
Pod documented
81 0 my ($params, $flags) = @_;
82 0 ▶ return sub { sprintf("\%s", shift @{ $_[0] } ); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 }
84  
85 0 ▶sub A {
Subroutine A: not called CC=8 SCAR=42.8
Pod documented
86 0 my ($params, $flags) = @_;
87 my ($indent_style, $purity, $useqq, $terse, $deepcopy,
88 0 $quotekeys, $max_depth) = @{ $params };
89 0 ▶ $indent_style = 2 unless defined $indent_style;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $indent_style 0 0 0
90 0 ▶ $purity ||= 0;
Condition0%0/2
$purity ||= 0
l!l
00
MC/DC0%0/1
$purity ||= 0
$purity
Truth table0%0/2
A
A= $purity
Aresult
11
00
91 0 ▶ $useqq ||= 0;
Condition0%0/2
$useqq ||= 0
l!l
00
MC/DC0%0/1
$useqq ||= 0
$useqq
Truth table0%0/2
A
A= $useqq
Aresult
11
00
92 0 ▶ $terse ||= 0;
Condition0%0/2
$terse ||= 0
l!l
00
MC/DC0%0/1
$terse ||= 0
$terse
Truth table0%0/2
A
A= $terse
Aresult
11
00
93 0 ▶ $deepcopy ||= 0;
Condition0%0/2
$deepcopy ||= 0
l!l
00
MC/DC0%0/1
$deepcopy ||= 0
$deepcopy
Truth table0%0/2
A
A= $deepcopy
Aresult
11
00
94 0 ▶ $quotekeys ||= 0;
Condition0%0/2
$quotekeys ||= 0
l!l
00
MC/DC0%0/1
$quotekeys ||= 0
$quotekeys
Truth table0%0/2
A
A= $quotekeys
Aresult
11
00
95 0 ▶ $max_depth ||= 0;
Condition0%0/2
$max_depth ||= 0
l!l
00
MC/DC0%0/1
$max_depth ||= 0
$max_depth
Truth table0%0/2
A
A= $max_depth
Aresult
11
00
96  
97 0 my $dumper = Data::Dumper->new([])
98 ->Indent($indent_style)
99 ->Purity($purity)
100 ->Useqq($useqq)
101 ->Terse($terse)
102 ->Deepcopy($deepcopy)
103 ->Quotekeys($quotekeys)
104 ->Maxdepth($max_depth);
105  
106 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
107 0 $dumper->Values([ shift @{ $_[0] } ]);
108 0 $dumper->Dump;
109 }
110 0 }
111  
112 3 ▶sub n {
Subroutine n: called CC=6 SCAR=18.7
Pod documented
113 3 my ($params, $flags) = @_;
114 3 ▶ my $repeats = $params->[0] || 1;
Condition100%2/2
$params->[0] || 1
l!l
21
MC/DC100%1/1
$params->[0] || 1
$params->[0]
Truth table100%2/2
A
A= $params->[0]
Aresult
11
00
115 3 my $type = $params->[1];
116 3 my $new_line = "\n";
117 3 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 1 2 3
118 1 ▶ if ($type eq 'm') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'm') { } 0 1 1
elsif ($type eq 'u') { } 0 1 1
elsif ($type eq 'd') { } 1 0 1
119 0 $new_line = chr(0x0D);
120 } elsif ($type eq 'u') {
121 0 $new_line = chr(0x0A);
122 } elsif ($type eq 'd') {
123 1 $new_line = chr(0x0D) . chr(0x0A);
124 }
125 }
126 3 ▶ return sub { "$new_line" x $repeats; };
Subroutine __ANON__: called CC=1 SCAR=0.0
127 }
128  
129 3 ▶sub J {
Subroutine J: called CC=4 SCAR=13.9
Pod undocumented
130 3 my ($params, $flags) = @_;
131 3 my ($join_char, $pre_char, $post_char) = @{ $params };
132 3 ▶ $join_char = ' ' unless defined $join_char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $join_char 1 2 3
133 3 ▶ $pre_char = '' unless defined $pre_char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pre_char 2 1 3
134 3 ▶ $post_char = '' unless defined $post_char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $post_char 2 1 3
135 3 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
136 3 my @to_join = @{ shift @{ $_[0] } };
137 3 return $pre_char . join($join_char, @to_join) . $post_char;
138 }
139 3 }
140  
141 ######################################################################
142 # utilities
143  
144 # take a positive integer, return it's string representation in radix n
145 4 ▶sub toStringRadix {
Subroutine toStringRadix: called CC=4 SCAR=14.9
Pod undocumented
146 4 my ($num, $radix) = @_;
147 4 ▶ if ($radix == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($radix == 0) 0 4 4
148 0 carp "0 is a sensless value for a radix, what are you thinking?";
149 0 return;
150 }
151 4 ▶ if ($radix < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($radix < 0) 0 4 4
152 0 carp "what am i supposed to do with a negative radix?";
153 0 return;
154 }
155 4 my @alphabet = ( "0" .. "9", "a" .. "z" );
156 4 my $string = "";
157 4 ▶ while ($num != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num != 0) 16 4 20
158 16 my $rem = $num % $radix;
159 16 $num = int($num/$radix);
160 16 $string = $alphabet[$rem] . $string;
161 }
162 4 return $string;
163 }
164  
165 ######################################################################
166 # directive table
167  
168 1 my %standard_directives = ( 'n' => \&n,
169 '~' => \&tilde,
170 'r' => \&R,
171 'd' => \&D,
172 'o' => \&O,
173 'x' => \&X,
174 'b' => \&B,
175 'a' => \&A,
176 'j' => \&J,
177 );
178  
179 1 our %directives = %standard_directives;
180  
181 #####################################################################
182 # do it!
183  
184 1 my $flags_class = "[:!@|?;]";
185  
186 # build_directive takes a dirctive string as an arg and returns a sub
187 # which takes the argument list as an arg
188 13 ▶sub build_directive {
Subroutine build_directive: called CC=3 SCAR=11.0
Pod undocumented
189 13 my $directive_string = shift;
190 # the type of directive is the last char in the string
191 13 my $directive_type = substr ($directive_string, -1);
192 # remove leading '~' and last char (directive type)
193 13 $directive_string = substr ($directive_string, 1);
194 13 $directive_string = substr ($directive_string, 0, -1);
195 13 my %flags;
196 13 ▶ if ($directive_string =~ s/((?<!')($flags_class+))$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($directive_string =~ s/((?<!')($flags_class+))$//) 1 12 13
197 1 %flags = map { $_ => 1 } split //, $2;
198 }
199 13 my @params = map { s/^'//; $_ }
200 split /(?<!'),/, $directive_string;
201 13 ▶ if (grep { $_ eq "v" } @params) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq 'v';} @params) { } 1 12 13
202 # v arg, we have to build the directive function at ever
203 # invocation:
204 1 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
205 1 my @args = @{ shift() };
206 @params = map {
207 1 ▶ if ($_ eq "v") {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq 'v') { } 1 0 1
208 1 shift(@args);
209 } else {
210 0 $_;
211 }
212 } @params;
213 1 $directives{$directive_type}->(\@params, \%flags)->(@args);
214 }
215 1 } else {
216 12 return $directives{$directive_type}->(\@params, \%flags);
217 }
218 }
219  
220 # we go through $string and build up a list of subs to call
221 13 ▶sub compile_control_string {
Subroutine compile_control_string: called CC=2 SCAR=7.1
Pod undocumented
222 13 my $directive_class = join('', keys %directives);
223 13 my $directive_regexp =
224 qr/(~ # start with a '~'
225 (?:(?:[,0-9]|'.)*?) # followed by a sequence of nu\mbers or quoted chars or co\m\mas
226 $flags_class* # then the flags
227 (?<!')(?:[$directive_class])) # finally ter\minated by a non quoted directive char
228 /x;
229 13 my $control = shift;
230 my @pieces =
231 map {
232 # build up the sub
233 13 ▶ if (/$directive_regexp/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$directive_regexp/x) { } 13 0 13
234 13 build_directive($_);
235 } else {
236 0 ▶ sub { $_ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
237 }
238 13 } grep { $_ } split $directive_regexp, $control;
239 13 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
240 13 my @args = @_;
241 13 join '', map { $_->(\@args) } @pieces;
242 }
243 13 }
244  
245 13 ▶sub pprint {
Subroutine pprint: called CC=2 SCAR=7.6
Pod undocumented
246 13 my ($control, @args) = @_;
247 13 ▶ if (ref $control eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $control eq 'CODE') { } 0 13 13
248 0 return $control->(@args);
249 } else {
250 13 return compile_control_string($control)->(@args);
251 }
252 }
253  
254 1 1;
255 __END__;
↑ Summary