Dump.pm

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