recurse2txt

filter: Statementstmt 82.1% 46 / 56 Branchbran 62.5% 20 / 32 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 76.1% 80 / 105 CC 22 SCAR 20.8
CC
22
Cov
76%
_dump
15.2
Dumper
7.9
array_recurse
3.0
CRAP 8.0
« blib/lib/Net/DNSBL/Monitor.pm ↑ Summary local/test.conf »
1 #!/usr/bin/perl
2 #
3 # recurse2txt routines
4 #
5 # version 1.03, 5-19-06, michael@bizsystems.com
6 #
7 #use strict;
8 #use diagnostics;
9 7 ▶use overload;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # generate a unique signature for a particular hash
12 #
13 # Data::Dumper actually does much more than this, however, it
14 # does not stringify hash's in a consistent manner. i.e. no SORT
15 #
16 # The routine below, while not covering recursion loops, non ascii
17 # characters, etc.... does produce text that can be eval'd and is
18 # consistent with each rendering.
19 #
20 18 ▶sub Dumper {
Subroutine Dumper: called CC=7 SCAR=20.6
21 18 ▶ return "undef\n" unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 18 18
22 18 my $ref = ref $_[0];
23 18 ▶ return "not a reference\n" unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 0 18 18
24 18 ▶ unless ($ref eq 'HASH' or $ref eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref eq "HASH" or $ref eq "ARRAY") 0 18 18
Condition66%2/3
$ref eq "HASH" or $ref eq "ARRAY"
l!l&&r!l&&!r
1080
MC/DC0%0/2
$ref eq "HASH" or $ref eq "ARRAY"
$ref eq "HASH" $ref eq "ARRAY"
Truth table66%2/3
A or B
A= $ref eq "HASH"B= $ref eq "ARRAY"
ABresult
1X1
011
000
25 0 ($ref) = (overload::StrVal($_[0]) =~ /^(?:.*\=)?([^=]*)\(/);
26 }
27 18 my $p = {
28 depth => 0,
29 elements => 0,
30 };
31 18 bless $p,__PACKAGE__;
32 18 my $data;
33 18 ▶ if ($ref eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 10 8 18
elsif ($ref eq 'ARRAY') { } 8 0 8
34 10 $data = $p->hash_recurse($_[0],"\n");
35 }
36 elsif ($ref eq 'ARRAY') {
37 8 $data = $p->array_recurse($_[0]);
38 } else {
39 0 return $ref ." unsupported\n";
40 }
41 18 $data =~ s/,\n$/;\n/;
42 18 return $p->{elements} ."\t= ". $data;
43 }
44
45 # input: pointer to hash, terminator
46 # returns: data
47 #
48 45 ▶sub hash_recurse {
Subroutine hash_recurse: called CC=3 SCAR=11.0
49 45 my($p,$ptr,$n) = @_;
50 45 ▶ $n = '' unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 45 45
51 45 my $data = "{\n";
52 45 foreach my $key (sort keys %$ptr) {
53 121 $data .= "\t'". $key ."'\t=> ";
54 121 $data .= _dump($p,$ptr->{$key},"\n");
55 }
56 45 $data .= '},'.$n;
57 }
58  
59 # generate a unique signature for a particular array
60 #
61 # input: pointer to array, terminator
62 # returns: data
63 8 ▶sub array_recurse {
Subroutine array_recurse: called CC=3 SCAR=11.0
64 8 my($p,$ptr,$n) = @_;
65 8 ▶ $n = '' unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 8 0 8
66 8 my $data = '[';
67 8 foreach my $item (@$ptr) {
68 148 $data .= _dump($p,$item);
69 }
70 8 $data .= "],\n";
71 }
72  
73 # input: self, item, append
74 # return: data
75 #
76 269 ▶sub _dump {
Subroutine _dump: called CC=9 SCAR=27.2
77 269 my($p,$item,$n) = @_;
78 269 $p->{elements}++;
79 269 ▶ $n = '' unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 148 121 269
80 269 my $ref = ref $item;
81 269 ▶ if ($ref eq 'HASH') {
Branch: 7 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 35 234 269
elsif ($ref eq 'ARRAY') { } 0 234 234
elsif ($ref eq 'SCALAR') { } 0 234 234
elsif ($ref eq 'GLOB') { } 0 234 234
elsif (do { my $g = \$item; ref $g eq 'GLOB' }) { } 0 234 234
elsif ($ref eq 'CODE') { } 0 234 234
elsif (defined $item) { } 234 0 234
82 35 return tabout($p->hash_recurse($item,"\n"));
83 }
84 elsif($ref eq 'ARRAY') {
85 0 return $p->array_recurse($item,$n);
86 }
87 elsif($ref eq 'SCALAR') {
88 0 return q|\$SCALAR,|.$n;
89 }
90 elsif ($ref eq 'GLOB') {
91 0 my $g = *{$item};
92 0 return "\\$g" .','.$n;
93 }
94 234 elsif(do {my $g = \$item; ref $g eq 'GLOB'}) {
95 0 return "$item" .','.$n;
96 }
97 elsif($ref eq 'CODE') {
98 0 return q|sub {'DUMMY'},|.$n;
99 }
100 elsif (defined $item) {
101 234 return wrap_data($item) .','.$n;
102 }
103 else {
104 0 return 'undef,'.$n;
105 }
106 }
107  
108 35 ▶sub tabout {
Subroutine tabout: called CC=2 SCAR=6.9
109 35 my @data = split(/\n/,shift);
110 35 my $data = shift @data;
111 35 $data .= "\n";
112 35 foreach(@data) {
113 97 $data .= "\t$_\n";
114 }
115 35 $data;
116 }
117  
118 234 ▶sub wrap_data {
Subroutine wrap_data: called CC=3 SCAR=11.0
119 234 my $data = shift;
120 234 ▶ return ($data =~ /\D/ || $data =~ /^$/)
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /\D/ || $data =~ /^$/ ? : 159 75 234
Condition100%3/3
$data =~ /\D/ or $data =~ /^$/
l!l&&r!l&&!r
157275
MC/DC100%2/2
$data =~ /\D/ or $data =~ /^$/
$data =~ /\D/ $data =~ /^$/
Truth table100%3/3
A or B
A= $data =~ /\D/B= $data =~ /^$/
ABresult
1X1
011
000
121 ? q|'|. $data .q|'|
122 : $data;
123 }
124  
125 7 1;
« blib/lib/Net/DNSBL/Monitor.pm ↑ Summary local/test.conf »