blib/lib/Data/Dump/SortKeys.pm

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