Dump.pm

filter: Statementstmt 97.9% 339 / 346 Branchbran 90.1% 193 / 214 Conditioncond 85.0% 74 / 87 MC/DCmcdc 75.5% 37 / 49 Subroutinesub 100% 17 / 17 Podpod 50.0% 4 / 8 totaltotal 92.0% 664 / 721 CC 149 SCAR 40.2
CC
149
Cov
94%
_dump
92.4
format_list
16.2
str
15.2
CRAP 55.8
↑ Summary Dump/Trace.pm »
1 package Data::Dump;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use vars qw(@EXPORT @EXPORT_OK $VERSION $DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use subs qq(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 15 require Exporter;
8 15 *import = \&Exporter::import;
9 15 @EXPORT = qw(dd ddx);
10 15 @EXPORT_OK = qw(dump pp dumpf quote);
11  
12 15 $VERSION = "1.25";
13 15 $DEBUG = 0;
14  
15 15 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 15 ▶use vars qw(%seen %refcnt @dump @fixup %require $TRY_BASE64 @FILTERS $INDENT $LINEWIDTH);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 15 ▶$TRY_BASE64 = 50 unless defined $TRY_BASE64;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Data::Dump::TRY_BASE64 15 0 15
19 15 ▶$INDENT = " " unless defined $INDENT;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Data::Dump::INDENT 15 0 15
20 15 ▶$LINEWIDTH = 60 unless defined $LINEWIDTH;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Data::Dump::LINEWIDTH 15 0 15
21  
22 sub dump
23 111 ▶{
Subroutine dump: called CC=15 SCAR=27.1
24 111 local %seen;
25 111 local %refcnt;
26 111 local %require;
27 111 local @fixup;
28  
29 111 ▶ require Data::Dump::FilterContext if @FILTERS;
Branch: 1 branch
BranchTrueFalseTotal
if @Data::Dump::FILTERS 8 103 111
30  
31 111 my $name = "a";
32 111 my @dump;
33  
34 111 for my $v (@_) {
35 159 my $val = _dump($v, $name, [], tied($v));
36 159 push(@dump, [$name, $val]);
37 } continue {
38 159 $name++;
39 }
40  
41 111 my $out = "";
42 111 ▶ if (%require) {
Branch: 1 branch
BranchTrueFalseTotal
if (%Data::Dump::require) 4 107 111
43 4 for (sort keys %require) {
44 4 $out .= "require $_;\n";
45 }
46 }
47 111 ▶ if (%refcnt) {
Branch: 1 branch
BranchTrueFalseTotal
if (%Data::Dump::refcnt) 10 101 111
48 # output all those with refcounts first
49 10 for (@dump) {
50 27 my $name = $_->[0];
51 27 ▶ if ($refcnt{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Data::Dump::refcnt{$name}) 10 17 27
52 10 $out .= "my \$$name = $_->[1];\n";
53 10 undef $_->[1];
54 }
55 }
56 10 for (@fixup) {
57 17 $out .= "$_;\n";
58 }
59 }
60  
61 111 my $paren = (@dump != 1);
62 111 ▶ $out .= "(" if $paren;
Branch: 1 branch
BranchTrueFalseTotal
if $paren 14 97 111
63 $out .= format_list($paren, undef,
64 111 ▶ map {defined($_->[1]) ? $_->[1] : "\$".$_->[0]}
Branch: 1 branch
BranchTrueFalseTotal
defined $_->[1] ? : 149 10 159
65 @dump
66 );
67 111 ▶ $out .= ")" if $paren;
Branch: 1 branch
BranchTrueFalseTotal
if $paren 14 97 111
68  
69 111 ▶ if (%refcnt || %require) {
Branch: 1 branch
BranchTrueFalseTotal
if (%Data::Dump::refcnt or %Data::Dump::require) 13 98 111
Condition100%3/3
%Data::Dump::refcnt or %Data::Dump::require
l!l&&r!l&&!r
10398
MC/DC100%2/2
%Data::Dump::refcnt or %Data::Dump::require
%Data::Dump::refcnt %Data::Dump::require
Truth table100%3/3
A or B
A= %Data::Dump::refcntB= %Data::Dump::require
ABresult
1X1
011
000
70 13 $out .= ";\n";
71 13 $out =~ s/^/$INDENT/gm;
72 13 $out = "do {\n$out}";
73 }
74  
75 111 ▶ print STDERR "$out\n" unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 111 111
76 111 $out;
77 }
78  
79 15 *pp = \&dump;
80  
81 1 ▶sub dd {
Subroutine dd: called CC=1 SCAR=0.0
Pod documented
82 1 print dump(@_), "\n";
83 }
84  
85 2 ▶sub ddx {
Subroutine ddx: called CC=1 SCAR=0.0
Pod documented
86 2 my(undef, $file, $line) = caller;
87 2 $file =~ s,.*[\\/],,;
88 2 my $out = "$file:$line: " . dump(@_) . "\n";
89 2 $out =~ s/^/# /gm;
90 2 print $out;
91 }
92  
93 8 ▶sub dumpf {
Subroutine dumpf: called CC=1 SCAR=0.0
Pod documented
94 8 require Data::Dump::Filtered;
95 8 goto &Data::Dump::Filtered::dump_filtered;
96 }
97  
98 sub _dump
99 1313 ▶{
Subroutine _dump: called CC=90 SCAR=45.3
100 1313 my $ref = ref $_[0];
101 1313 ▶ my $rval = $ref ? $_[0] : \$_[0];
Branch: 1 branch
BranchTrueFalseTotal
$ref ? : 132 1181 1313
102 1313 shift;
103  
104 1313 my($name, $idx, $dont_remember, $pclass, $pidx) = @_;
105  
106 1313 my($class, $type, $id);
107 1313 my $strval = overload::StrVal($rval);
108 # Parse $strval without using regexps, in order not to clobber $1, $2,...
109 1313 ▶ if ((my $i = rindex($strval, "=")) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $i = rindex($strval, "=")) >= 0) 26 1287 1313
110 26 $class = substr($strval, 0, $i);
111 26 $strval = substr($strval, $i+1);
112 }
113 1313 ▶ if ((my $i = index($strval, "(0x")) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $i = index($strval, '(0x')) >= 0) { } 1313 0 1313
114 1313 $type = substr($strval, 0, $i);
115 1313 $id = substr($strval, $i + 2, -1);
116 }
117 else {
118 0 die "Can't parse " . overload::StrVal($rval);
119 }
120 1313 ▶ if ($] < 5.008 && $type eq "SCALAR") {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.008" and $type eq "SCALAR") 0 1313 1313
Condition33%1/3
$] < "5.008" and $type eq "SCALAR"
!ll&&!rl&&r
131300
MC/DC0%0/2
$] < "5.008" and $type eq "SCALAR"
$] < "5.008" $type eq "SCALAR"
Truth table33%1/3
A and B
A= $] < "5.008"B= $type eq "SCALAR"
ABresult
0X0
100
111
121 0 ▶ $type = "REF" if $ref eq "REF";
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "REF" 0 0 0
122 }
123 1313 ▶ warn "\$$name(@$idx) $class $type $id ($ref)" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Data::Dump::DEBUG 0 1313 1313
124  
125 1313 my $out;
126 my $comment;
127 1313 my $hide_keys;
128 1313 ▶ if (@FILTERS) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Data::Dump::FILTERS) 11 1302 1313
129 11 my $pself = "";
130 11 ▶ $pself = fullname("self", [@$idx[$pidx..(@$idx - 1)]]) if $pclass;
Branch: 1 branch
BranchTrueFalseTotal
if $pclass 2 9 11
131 11 my $ctx = Data::Dump::FilterContext->new($rval, $class, $type, $ref, $pclass, $pidx, $idx);
132 11 my @bless;
133 11 for my $filter (@FILTERS) {
134 11 ▶ if (my $f = $filter->($ctx, $rval)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = &$filter($ctx, $rval)) 6 5 11
135 6 ▶ if (my $v = $f->{object}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $v = $f->{"object"}) 1 5 6
136 1 local @FILTERS;
137 1 $out = _dump($v, $name, $idx, 1);
138 1 $dont_remember++;
139 }
140 6 ▶ if (defined(my $c = $f->{bless})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $c = $f->{"bless"})) 1 5 6
141 1 push(@bless, $c);
142 }
143 6 ▶ if (my $c = $f->{comment}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $f->{"comment"}) 1 5 6
144 1 $comment = $c;
145 }
146 6 ▶ if (defined(my $c = $f->{dump})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $c = $f->{"dump"})) 1 5 6
147 1 $out = $c;
148 1 $dont_remember++;
149 }
150 6 ▶ if (my $h = $f->{hide_keys}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = $f->{"hide_keys"}) 2 4 6
151 2 ▶ if (ref($h) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $h eq "ARRAY") 2 0 2
152 2 ▶ $hide_keys = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
153 2 for my $k (@$h) {
154 2 ▶ return 1 if $k eq $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $k eq $_[0] 1 1 2
155 }
156 1 return 0;
157 2 };
158 }
159 }
160 }
161 }
162 11 ▶ push(@bless, "") if defined($out) && !@bless;
Branch: 1 branch
BranchTrueFalseTotal
if defined $out and not @bless 2 9 11
Condition66%2/3
defined $out and not @bless
!ll&&!rl&&r
902
MC/DC50%1/2
defined $out and not @bless
defined $out not @bless
Truth table66%2/3
A and not(B)
A= defined $outB= not @bless
ABresult
0X0
100
111
163 11 ▶ if (@bless) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bless) 3 8 11
164 3 $class = shift(@bless);
165 3 ▶ warn "More than one filter callback tried to bless object" if @bless;
Branch: 1 branch
BranchTrueFalseTotal
if @bless 0 3 3
166 }
167 }
168  
169 1313 ▶ unless ($dont_remember) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dont_remember) 1298 15 1313
170 1298 ▶ if (my $s = $seen{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $Data::Dump::seen{$id}) 19 1279 1298
171 19 my($sname, $sidx) = @$s;
172 19 $refcnt{$sname}++;
173 19 ▶ my $sref = fullname($sname, $sidx,
Condition100%3/3
$ref && $type eq 'SCALAR'
!ll&&!rl&&r
289
MC/DC100%2/2
$ref && $type eq 'SCALAR'
$ref $type eq 'SCALAR'
Truth table100%3/3
A && B
A= $refB= $type eq 'SCALAR'
ABresult
0X0
100
111
174 ($ref && $type eq "SCALAR"));
175 19 ▶ warn "SEEN: [\$$name(@$idx)] => [\$$sname(@$sidx)] ($ref,$sref)" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Data::Dump::DEBUG 0 19 19
176 19 ▶ return $sref unless $sname eq $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $sname eq $name 15 4 19
177 4 $refcnt{$name}++;
178 4 push(@fixup, fullname($name,$idx)." = $sref");
179 4 ▶ return "do{my \$fix}" if @$idx && $idx->[-1] eq '$';
Branch: 1 branch
BranchTrueFalseTotal
if @$idx and $idx->[-1] eq "\$" 1 3 4
Condition66%2/3
@$idx and $idx->[-1] eq "\$"
!ll&&!rl&&r
031
MC/DC50%1/2
@$idx and $idx->[-1] eq "\$"
@$idx $idx->[-1] eq "\$"
Truth table66%2/3
A and B
A= @$idxB= $idx->[-1] eq "\$"
ABresult
0X0
100
111
180 3 return "'fix'";
181 }
182 1279 $seen{$id} = [$name, $idx];
183 }
184  
185 1294 ▶ if ($class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class) 27 1267 1294
186 27 $pclass = $class;
187 27 $pidx = @$idx;
188 }
189  
190 1294 ▶ if (defined $out) {
Branch: 7 branches
BranchTrueFalseTotal
if (defined $out) { } 2 1292 1294
elsif ($type eq 'SCALAR' or $type eq 'REF' or $type eq 'REGEXP') { } 1167 125 1292
elsif ($type eq 'GLOB') { } 39 86 125
elsif ($type eq 'ARRAY') { } 18 68 86
elsif ($type eq 'HASH') { } 56 12 68
elsif ($type eq 'CODE') { } 1 11 12
elsif ($type eq 'VSTRING') { } 11 0 11
191 # keep it
192 }
193 ▶ elsif ($type eq "SCALAR" || $type eq "REF" || $type eq "REGEXP") {
Condition100%6/6
$type eq "SCALAR" or $type eq "REF" or $type eq "REGEXP"
l!l&&r!l&&!r
11589125
$type eq "SCALAR" or $type eq "REF"
l!l&&r!l&&!r
114612134
MC/DC100%3/3
$type eq "SCALAR" or $type eq "REF" or $type eq "REGEXP"
$type eq "SCALAR" $type eq "REF" $type eq "REGEXP"
Truth table100%4/4
A or B or C
A= $type eq "SCALAR"B= $type eq "REF"C= $type eq "REGEXP"
ABCresult
1XX1
01X1
0011
0000
194 1167 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 32 1135 1167
195 32 ▶ if ($class && $class eq "Regexp") {
Branch: 1 branch
BranchTrueFalseTotal
if ($class and $class eq 'Regexp') { } 9 23 32
Condition100%3/3
$class and $class eq "Regexp"
!ll&&!rl&&r
1949
MC/DC100%2/2
$class and $class eq "Regexp"
$class $class eq "Regexp"
Truth table100%3/3
A and B
A= $classB= $class eq "Regexp"
ABresult
0X0
100
111
196 9 my $v = "$rval";
197  
198 9 my $mod = "";
199 9 ▶ if ($v =~ /^\(\?\^?([msix-]*):([\x00-\xFF]*)\)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /^\(\?\^?([msix-]*):([\x00-\xFF]*)\)\z/) 9 0 9
200 9 $mod = $1;
201 9 $v = $2;
202 9 $mod =~ s/-.*//;
203 }
204  
205 9 my $sep = '/';
206 9 my $sep_count = ($v =~ tr/\///);
207 9 ▶ if ($sep_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sep_count) 4 5 9
208 # see if we can find a better one
209 4 for ('|', ',', ':', '#') {
210 10 my $c = eval "\$v =~ tr/\Q$_\E//";
211 #print "SEP $_ $c $sep_count\n";
212 10 ▶ if ($c < $sep_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c < $sep_count) 3 7 10
213 3 $sep = $_;
214 3 $sep_count = $c;
215 3 ▶ last if $sep_count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $sep_count == 0 3 0 3
216 }
217 }
218 }
219 9 $v =~ s/\Q$sep\E/\\$sep/g;
220  
221 9 $out = "qr$sep$v$sep$mod";
222 9 undef($class);
223 }
224 else {
225 23 ▶ delete $seen{$id} if $type eq "SCALAR"; # will be seen again shortly
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "SCALAR" 11 12 23
226 23 my $val = _dump($$rval, $name, [@$idx, "\$"], 0, $pclass, $pidx);
227 23 ▶ $out = $class ? "do{\\(my \$o = $val)}" : "\\$val";
Branch: 1 branch
BranchTrueFalseTotal
$class ? : 4 19 23
228 }
229 } else {
230 1135 ▶ if (!defined $$rval) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $$rval) { } 3 1132 1135
elsif ($$rval =~ /^-?(?:nan|inf)/i) { } 4 1128 1132
elsif (do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} $$rval + 0 eq $$rval }) { } 817 311 1128
231 3 $out = "undef";
232 }
233 elsif ($$rval =~ /^-?(?:nan|inf)/i) {
234 4 $out = str($$rval);
235 }
236 15 ▶ elsif (do {no warnings 'numeric'; $$rval + 0 eq $$rval}) {
Subroutine BEGIN: called CC=1 SCAR=0.0
237 817 $out = $$rval;
238 }
239 else {
240 311 $out = str($$rval);
241 }
242 1135 ▶ if ($class && !@$idx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class and not @$idx) 1 1134 1135
Condition100%3/3
$class and not @$idx
!ll&&!rl&&r
113221
MC/DC100%2/2
$class and not @$idx
$class not @$idx
Truth table100%3/3
A and not(B)
A= $classB= not @$idx
ABresult
0X0
100
111
243 # Top is an object, not a reference to one as perl needs
244 1 $refcnt{$name}++;
245 1 my $obj = fullname($name, $idx);
246 1 my $cl = quote($class);
247 1 push(@fixup, "bless \\$obj, $cl");
248 }
249 }
250 }
251 elsif ($type eq "GLOB") {
252 39 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 5 34 39
253 5 delete $seen{$id};
254 5 my $val = _dump($$rval, $name, [@$idx, "*"], 0, $pclass, $pidx);
255 5 $out = "\\$val";
256 5 ▶ if ($out =~ /^\\\*Symbol::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out =~ /^\\\*Symbol::/) 3 2 5
257 3 $require{Symbol}++;
258 3 $out = "Symbol::gensym()";
259 }
260 } else {
261 34 my $val = "$$rval";
262 34 $out = "$$rval";
263  
264 34 for my $k (qw(SCALAR ARRAY HASH)) {
265 102 my $gval = *$$rval{$k};
266 102 ▶ next unless defined $gval;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gval 61 41 102
267 41 ▶ next if $k eq "SCALAR" && ! defined $$gval; # always there
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "SCALAR" and not defined $$gval 29 12 41
Condition100%3/3
$k eq "SCALAR" and not defined $$gval
!ll&&!rl&&r
7529
MC/DC100%2/2
$k eq "SCALAR" and not defined $$gval
$k eq "SCALAR" not defined $$gval
Truth table100%3/3
A and not(B)
A= $k eq "SCALAR"B= not defined $$gval
ABresult
0X0
100
111
268 12 my $f = scalar @fixup;
269 12 push(@fixup, "RESERVED"); # overwritten after _dump() below
270 12 $gval = _dump($gval, $name, [@$idx, "*{$k}"], 0, $pclass, $pidx);
271 12 $refcnt{$name}++;
272 12 my $gname = fullname($name, $idx);
273 12 $fixup[$f] = "$gname = $gval"; #XXX indent $gval
274 }
275 }
276 }
277 elsif ($type eq "ARRAY") {
278 18 my @vals;
279 18 my $tied = tied_str(tied(@$rval));
280 18 my $i = 0;
281 18 for my $v (@$rval) {
282 291 push(@vals, _dump($v, $name, [@$idx, "[$i]"], $tied, $pclass, $pidx));
283 291 $i++;
284 }
285 18 $out = "[" . format_list(1, $tied, @vals) . "]";
286 }
287 elsif ($type eq "HASH") {
288 56 my(@keys, @vals);
289 56 my $tied = tied_str(tied(%$rval));
290  
291 # statistics to determine variation in key lengths
292 56 my $kstat_max = 0;
293 56 my $kstat_sum = 0;
294 56 my $kstat_sum2 = 0;
295  
296 56 my @orig_keys = keys %$rval;
297 56 ▶ if ($hide_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hide_keys) 1 55 56
298 1 @orig_keys = grep !$hide_keys->($_), @orig_keys;
299 }
300 56 my $text_keys = 0;
301 56 for (@orig_keys) {
302 46 ▶ $text_keys++, last unless /^[-+]?(?:0|[1-9]\d*)(?:\.\d+)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^[-+]?(?:0|[1-9]\d*)(?:\.\d+)?\z/ 39 7 46
303 }
304  
305 56 ▶ if ($text_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text_keys) { } 39 17 56
306 39 @orig_keys = sort { lc($a) cmp lc($b) } @orig_keys;
307 }
308 else {
309 17 @orig_keys = sort { $a <=> $b } @orig_keys;
310 }
311  
312 56 my $quote;
313 56 for my $key (@orig_keys) {
314 342 ▶ next if $key =~ /^-?[a-zA-Z_]\w*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^-?[a-zA-Z_]\w*\z/ 332 10 342
315 10 ▶ next if $key =~ /^-?[1-9]\d{0,8}\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^-?[1-9]\d{0,8}\z/ 4 6 10
316 6 $quote++;
317 6 last;
318 }
319  
320 56 for my $key (@orig_keys) {
321 822 my $val = \$rval->{$key}; # capture value before we modify $key
322 822 ▶ $key = quote($key) if $quote;
Branch: 1 branch
BranchTrueFalseTotal
if $quote 502 320 822
323 822 ▶ $kstat_max = length($key) if length($key) > $kstat_max;
Branch: 1 branch
BranchTrueFalseTotal
if length $key > $kstat_max 92 730 822
324 822 $kstat_sum += length($key);
325 822 $kstat_sum2 += length($key)*length($key);
326  
327 822 push(@keys, $key);
328 822 push(@vals, _dump($$val, $name, [@$idx, "{$key}"], $tied, $pclass, $pidx));
329 }
330 56 my $nl = "";
331 56 my $klen_pad = 0;
332 56 my $tmp = "@keys @vals";
333 56 ▶ if (length($tmp) > $LINEWIDTH || $tmp =~ /\n/ || $tied) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tmp > $Data::Dump::LINEWIDTH or $tmp =~ /\n/ or $tied) 17 39 56
Condition83%5/6
length $tmp > $Data::Dump::LINEWIDTH or $tmp =~ /\n/ or $tied
l!l&&r!l&&!r
16139
length $tmp > $Data::Dump::LINEWIDTH or $tmp =~ /\n/
l!l&&r!l&&!r
16040
MC/DC66%2/3
length $tmp > $Data::Dump::LINEWIDTH or $tmp =~ /\n/ or $tied
length $tmp > $Data::Dump::LINEWIDTH $tmp =~ /\n/ $tied
Truth table75%3/4
A or B or C
A= length $tmp > $Data::Dump::LINEWIDTHB= $tmp =~ /\n/C= $tied
ABCresult
1XX1
01X1
0011
0000
334 17 $nl = "\n";
335  
336 # Determine what padding to add
337 17 ▶ if ($kstat_max < 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($kstat_max < 4) { } 5 12 17
elsif (@keys >= 2) { } 12 0 12
338 5 $klen_pad = $kstat_max;
339 }
340 elsif (@keys >= 2) {
341 12 my $n = @keys;
342 12 my $avg = $kstat_sum/$n;
343 12 my $stddev = sqrt(($kstat_sum2 - $n * $avg * $avg) / ($n - 1));
344  
345 # I am not actually very happy with this heuristics
346 12 ▶ if ($stddev / $kstat_max < 0.25) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stddev / $kstat_max < "0.25") 8 4 12
347 8 $klen_pad = $kstat_max;
348 }
349 12 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Data::Dump::DEBUG) 0 12 12
350 0 push(@keys, "__S");
351 0 push(@vals, sprintf("%.2f (%d/%.1f/%.1f)",
352 $stddev / $kstat_max,
353 $kstat_max, $avg, $stddev));
354 }
355 }
356 }
357 56 $out = "{$nl";
358 56 ▶ $out .= "$INDENT# $tied$nl" if $tied;
Branch: 1 branch
BranchTrueFalseTotal
if $tied 1 55 56
359 56 ▶ while (@keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys) 822 56 878
360 822 my $key = shift @keys;
361 822 my $val = shift @vals;
362 822 ▶ my $vpad = $INDENT . (" " x ($klen_pad ? $klen_pad + 4 : 0));
Branch: 1 branch
BranchTrueFalseTotal
$klen_pad ? : 739 83 822
363 822 $val =~ s/\n/\n$vpad/gm;
364 822 ▶ my $kpad = $nl ? $INDENT : " ";
Branch: 1 branch
BranchTrueFalseTotal
$nl ? : 764 58 822
365 822 ▶ $key .= " " x ($klen_pad - length($key)) if $nl && $klen_pad > length($key);
Branch: 1 branch
BranchTrueFalseTotal
if $nl and $klen_pad > length $key 711 111 822
Condition100%3/3
$nl and $klen_pad > length $key
!ll&&!rl&&r
5853711
MC/DC100%2/2
$nl and $klen_pad > length $key
$nl $klen_pad > length $key
Truth table100%3/3
A and B
A= $nlB= $klen_pad > length $key
ABresult
0X0
100
111
366 822 $out .= "$kpad$key => $val,$nl";
367 }
368 56 ▶ $out =~ s/,$/ / unless $nl;
Branch: 1 branch
BranchTrueFalseTotal
unless $nl 39 17 56
369 56 $out .= "}";
370 }
371 elsif ($type eq "CODE") {
372 1 $out = 'sub { ... }';
373 }
374 elsif ($type eq "VSTRING") {
375 11 ▶ $out = sprintf +($ref ? '\v%vd' : 'v%vd'), $$rval;
Branch: 1 branch
BranchTrueFalseTotal
$ref ? : 3 8 11
376 }
377 else {
378 0 warn "Can't handle $type data";
379 0 $out = "'#$type#'";
380 }
381  
382 1294 ▶ if ($class && $ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class and $ref) 14 1280 1294
Condition100%3/3
$class and $ref
!ll&&!rl&&r
1276414
MC/DC100%2/2
$class and $ref
$class $ref
Truth table100%3/3
A and B
A= $classB= $ref
ABresult
0X0
100
111
383 14 $out = "bless($out, " . quote($class) . ")";
384 }
385 1294 ▶ if ($comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comment) 1 1293 1294
386 1 $comment =~ s/^/# /gm;
387 1 ▶ $comment .= "\n" unless $comment =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $comment =~ /\n\z/ 1 0 1
388 1 $comment =~ s/^#[ \t]+\n/\n/;
389 1 $out = "$comment$out";
390 }
391 1294 return $out;
392 }
393  
394 74 ▶sub tied_str {
Subroutine tied_str: called CC=3 SCAR=11.2
Pod undocumented
395 74 my $tied = shift;
396 74 ▶ if ($tied) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tied) 2 72 74
397 2 ▶ if (my $tied_ref = ref($tied)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tied_ref = ref $tied) { } 2 0 2
398 2 $tied = "tied $tied_ref";
399 }
400 else {
401 0 $tied = "tied";
402 }
403 }
404 74 return $tied;
405 }
406  
407 sub fullname
408 41 ▶{
Subroutine fullname: called CC=12 SCAR=24.8
Pod undocumented
409 41 my($name, $idx, $ref) = @_;
410 41 substr($name, 0, 0) = "\$";
411  
412 41 my @i = @$idx; # need copy in order to not modify @$idx
413 41 ▶ if ($ref && @i && $i[0] eq "\$") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref and @i and $i[0] eq "\$") 2 39 41
Condition100%6/6
$ref and @i and $i[0] eq "\$"
!ll&&!rl&&r
3542
$ref and @i
!ll&&!rl&&r
3236
MC/DC100%3/3
$ref and @i and $i[0] eq "\$"
$ref @i $i[0] eq "\$"
Truth table100%4/4
A and B and C
A= $refB= @iC= $i[0] eq "\$"
ABCresult
0XX0
10X0
1100
1111
414 2 shift(@i); # remove one deref
415 2 $ref = 0;
416 }
417 41 ▶ while (@i && $i[0] eq "\$") {
Branch: 1 branch
BranchTrueFalseTotal
if (@i and $i[0] eq "\$") 5 41 46
Condition100%3/3
@i and $i[0] eq "\$"
!ll&&!rl&&r
13285
MC/DC100%2/2
@i and $i[0] eq "\$"
@i $i[0] eq "\$"
Truth table100%3/3
A and B
A= @iB= $i[0] eq "\$"
ABresult
0X0
100
111
418 5 shift @i;
419 5 $name = "\$$name";
420 }
421  
422 41 my $last_was_index;
423 41 for my $i (@i) {
424 35 ▶ if ($i eq "*" || $i eq "\$") {
Branch: 2 branches
BranchTrueFalseTotal
if ($i eq '*' or $i eq '$') { } 7 28 35
elsif ($i =~ s/^\*//) { } 2 26 28
Condition100%3/3
$i eq "*" or $i eq "\$"
l!l&&r!l&&!r
4328
MC/DC100%2/2
$i eq "*" or $i eq "\$"
$i eq "*" $i eq "\$"
Truth table100%3/3
A or B
A= $i eq "*"B= $i eq "\$"
ABresult
1X1
011
000
425 7 $last_was_index = 0;
426 7 $name = "$i\{$name}";
427 } elsif ($i =~ s/^\*//) {
428 2 $name .= $i;
429 2 $last_was_index++;
430 } else {
431 26 ▶ $name .= "->" unless $last_was_index++;
Branch: 1 branch
BranchTrueFalseTotal
unless $last_was_index++ 24 2 26
432 26 $name .= $i;
433 }
434 }
435 41 ▶ $name = "\\$name" if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 7 34 41
436 41 $name;
437 }
438  
439 sub format_list
440 129 ▶{
Subroutine format_list: called CC=16 SCAR=27.8
Pod undocumented
441 129 my $paren = shift;
442 129 my $comment = shift;
443 129 ▶ my $indent_lim = $paren ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$paren ? : 32 97 129
444 129 ▶ if (@_ > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 3) 12 117 129
445 # can we use range operator to shorten the list?
446 12 my $i = 0;
447 12 ▶ while ($i < @_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 40 12 52
448 40 my $j = $i + 1;
449 40 my $v = $_[$i];
450 40 ▶ while ($j < @_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @_) 316 13 329
451 # XXX allow string increment too?
452 316 ▶ if ($v eq "0" || $v =~ /^-?[1-9]\d{0,9}\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v eq '0' or $v =~ /^-?[1-9]\d{0,9}\z/) { } 37 279 316
elsif ($v =~ /^"([A-Za-z]{1,3}\d*)"\z/) { } 266 13 279
Condition100%3/3
$v eq 0 or $v =~ /^-?[1-9]\d{0,9}\z/
l!l&&r!l&&!r
235279
MC/DC100%2/2
$v eq 0 or $v =~ /^-?[1-9]\d{0,9}\z/
$v eq 0 $v =~ /^-?[1-9]\d{0,9}\z/
Truth table100%3/3
A or B
A= $v eq 0B= $v =~ /^-?[1-9]\d{0,9}\z/
ABresult
1X1
011
000
453 37 $v++;
454 }
455 elsif ($v =~ /^"([A-Za-z]{1,3}\d*)"\z/) {
456 266 $v = $1;
457 266 $v++;
458 266 $v = qq("$v");
459 }
460 else {
461 13 last;
462 }
463 303 ▶ last if $_[$j] ne $v;
Branch: 1 branch
BranchTrueFalseTotal
if $_[$j] ne $v 14 289 303
464 289 $j++;
465 }
466 40 ▶ if ($j - $i > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j - $i > 3) 9 31 40
467 9 splice(@_, $i, $j - $i, "$_[$i] .. $_[$j-1]");
468 }
469 40 $i++;
470 }
471 }
472 129 my $tmp = "@_";
473 129 ▶ if ($comment || (@_ > $indent_lim && (length($tmp) > $LINEWIDTH || $tmp =~ /\n/))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comment or @_ > $indent_lim and length $tmp > $Data::Dump::LINEWIDTH || $tmp =~ /\n/) { } 1 128 129
Condition55%5/9
$comment or @_ > $indent_lim and length $tmp > $Data::Dump::LINEWIDTH || $tmp =~ /\n/
l!l&&r!l&&!r
10128
@_ > $indent_lim and length $tmp > $Data::Dump::LINEWIDTH || $tmp =~ /\n/
!ll&&!rl&&r
104240
length $tmp > $Data::Dump::LINEWIDTH or $tmp =~ /\n/
l!l&&r!l&&!r
0024
MC/DC25%1/4
$comment or @_ > $indent_lim and length $tmp > $Data::Dump::LINEWIDTH || $tmp =~ /\n/
$comment @_ > $indent_lim length $tmp > $Data::Dump::LINEWIDTH $tmp =~ /\n/
Truth table60%3/5
A or B and C or D
A= $commentB= @_ > $indent_limC= length $tmp > $Data::Dump::LINEWIDTHD= $tmp =~ /\n/
ABCDresult
1XXX1
011X1
01011
00XX0
01000
474 1 my @elem = @_;
475 1 for (@elem) { s/^/$INDENT/gm; }
476 1 ▶ return "\n" . ($comment ? "$INDENT# $comment\n" : "") .
Branch: 1 branch
BranchTrueFalseTotal
$comment ? : 1 0 1
477 join(",\n", @elem, "");
478 } else {
479 128 return join(", ", @_);
480 }
481 }
482  
483 315 ▶sub str {
Subroutine str: called CC=15 SCAR=27.2
Pod undocumented
484 315 ▶ if (length($_[0]) > 20) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_[0] > 20) 14 301 315
485 14 for ($_[0]) {
486 # Check for repeated string
487 14 ▶ if (/^(.)\1\1\1/s) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(.)\1\1\1/s) 4 10 14
488 # seems to be a repeating sequence, let's check if it really is
489 # without backtracking
490 4 ▶ unless (/[^\Q$1\E]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/[^\Q$1\E]/) 4 0 4
491 4 my $base = quote($1);
492 4 my $repeat = length;
493 4 return "($base x $repeat)"
494 }
495 }
496 # Length protection because the RE engine will blow the stack [RT#33520]
497 10 ▶ if (length($_) < 16 * 1024 && /^(.{2,5}?)\1*\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ < 16384 and /^(.{2,5}?)\1*\z/s) 2 8 10
Condition66%2/3
length $_ < 16384 and /^(.{2,5}?)\1*\z/s
!ll&&!rl&&r
082
MC/DC50%1/2
length $_ < 16384 and /^(.{2,5}?)\1*\z/s
length $_ < 16384 /^(.{2,5}?)\1*\z/s
Truth table66%2/3
A and B
A= length $_ < 16384B= /^(.{2,5}?)\1*\z/s
ABresult
0X0
100
111
498 2 my $base = quote($1);
499 2 my $repeat = length($_)/length($1);
500 2 return "($base x $repeat)";
501 }
502 }
503 }
504  
505 309 local $_ = &quote;
506  
507 309 ▶ if (length($_) > 40 && !/\\x\{/ && length($_) > (length($_[0]) * 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > 40 and not /\\x\{/ and length $_ > length($_[0]) * 2) 3 306 309
Condition100%6/6
length $_ > 40 and not /\\x\{/ and length $_ > length($_[0]) * 2
!ll&&!rl&&r
30243
length $_ > 40 and not /\\x\{/
!ll&&!rl&&r
30117
MC/DC100%3/3
length $_ > 40 and not /\\x\{/ and length $_ > length($_[0]) * 2
length $_ > 40 not /\\x\{/ length $_ > length($_[0]) * 2
Truth table100%4/4
A and not(B) and C
A= length $_ > 40B= not /\\x\{/C= length $_ > length($_[0]) * 2
ABCresult
0XX0
10X0
1100
1111
508 # too much binary data, better to represent as a hex/base64 string
509  
510 # Base64 is more compact than hex when string is longer than
511 # 17 bytes (not counting any require statement needed).
512 # But on the other hand, hex is much more readable.
513 3 ▶ if ($TRY_BASE64 && length($_[0]) > $TRY_BASE64 &&
Branch: 1 branch
BranchTrueFalseTotal
if ($Data::Dump::TRY_BASE64 and length $_[0] > $Data::Dump::TRY_BASE64 and (defined &utf8::is_utf8 and not utf8::is_utf8($_[0])) and eval { do { require MIME::Base64 } }) 1 2 3
Condition75%9/12
$Data::Dump::TRY_BASE64 and length $_[0] > $Data::Dump::TRY_BASE64 and (defined &utf8::is_utf8 and not utf8::is_utf8($_[0])) and eval { do { require MIME::Base64 } }
!ll&&!rl&&r
201
$Data::Dump::TRY_BASE64 and length $_[0] > $Data::Dump::TRY_BASE64 and (defined &utf8::is_utf8 and not utf8::is_utf8($_[0]))
!ll&&!rl&&r
111
$Data::Dump::TRY_BASE64 and length $_[0] > $Data::Dump::TRY_BASE64
!ll&&!rl&&r
102
defined &utf8::is_utf8 and not utf8::is_utf8($_[0])
!ll&&!rl&&r
011
MC/DC40%2/5
$Data::Dump::TRY_BASE64 and length $_[0] > $Data::Dump::TRY_BASE64 and (defined &utf8::is_utf8 and not utf8::is_utf8($_[0])) and eval { do { require MIME::Base64 } }
$Data::Dump::TRY_BASE64 length $_[0] > $Data::Dump::TRY_BASE64 defined &utf8::is_utf8 not utf8::is_utf8($_[0]) eval { do { require MIME::Base64 } }
Truth table50%3/6
A and B and C and not(D) and E
A= $Data::Dump::TRY_BASE64B= length $_[0] > $Data::Dump::TRY_BASE64C= defined &utf8::is_utf8D= not utf8::is_utf8($_[0])E= eval { do { require MIME::Base64 } }
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
514 (defined &utf8::is_utf8 && !utf8::is_utf8($_[0])) &&
515 1 eval { require MIME::Base64 })
516 {
517 1 $require{"MIME::Base64"}++;
518 1 return "MIME::Base64::decode(\"" .
519 MIME::Base64::encode($_[0],"") .
520 "\")";
521 }
522 2 return "pack(\"H*\",\"" . unpack("H*", $_[0]) . "\")";
523 }
524  
525 306 return $_;
526 }
527  
528 15 my %esc = (
529 "\a" => "\\a",
530 "\b" => "\\b",
531 "\t" => "\\t",
532 "\n" => "\\n",
533 "\f" => "\\f",
534 "\r" => "\\r",
535 "\e" => "\\e",
536 );
537  
538 # put a string value in double quotes
539 837 ▶sub quote {
Subroutine quote: called CC=2 SCAR=6.9
Pod documented
540 837 local($_) = $_[0];
541 # If there are many '"' we might want to use qq() instead
542 837 s/([\\\"\@\$])/\\$1/g;
543 837 ▶ return qq("$_") unless /[^\040-\176]/; # fast exit
Branch: 1 branch
BranchTrueFalseTotal
unless /[^\040-\176]/ 821 16 837
544  
545 16 s/([\a\b\t\n\f\r\e])/$esc{$1}/g;
546  
547 # no need for 3 digits in escape for these
548 16 s/([\0-\037])(?!\d)/sprintf('\\%o',ord($1))/eg;
549  
550 16 s/([\0-\037\177-\377])/sprintf('\\x%02X',ord($1))/eg;
551 16 s/([^\040-\176])/sprintf('\\x{%X}',ord($1))/eg;
552  
553 16 return qq("$_");
554 }
555  
556 15 1;
557  
558 __END__
↑ Summary Dump/Trace.pm »