recurse2txt

filter: Statementstmt 69.6% 39 / 56 Branchbran 56.2% 18 / 32 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 85.7% 6 / 7 Podpod n/a totaltotal 63.8% 67 / 105 CC 22 SCAR 21.9
CC
22
Cov
64%
_dump
15.2
Dumper
10.3
tabout
6.0
CRAP 8.9
« blib/lib/Web/Scaffold.pm ↑ Summary
1 #!/usr/bin/perl
2 #
3 # recurse2txt routines
4 #
5 # version 1.02, 8-13-05, michael@bizsystems.com
6 #
7 #use strict;
8 #use diagnostics;
9 3 ▶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 8 ▶sub Dumper {
Subroutine Dumper: called CC=7 SCAR=23.3
21 8 ▶ return "undef\n" unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 8 8
22 8 my $ref = ref $_[0];
23 8 ▶ return "not a reference\n" unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 0 8 8
24 8 ▶ unless ($ref eq 'HASH' or $ref eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref eq "HASH" or $ref eq "ARRAY") 0 8 8
Condition33%1/3
$ref eq "HASH" or $ref eq "ARRAY"
l!l&&r!l&&!r
800
MC/DC0%0/2
$ref eq "HASH" or $ref eq "ARRAY"
$ref eq "HASH" $ref eq "ARRAY"
Truth table33%1/3
A or B
A= $ref eq "HASH"B= $ref eq "ARRAY"
ABresult
1X1
011
000
25 0 ($ref) = (overload::StrVal($_[0]) =~ /^(?:.*\=)?([^=]*)\(/);
26 }
27 8 my $p = {
28 depth => 0,
29 elements => 0,
30 };
31 8 bless $p,__PACKAGE__;
32 8 my $data;
33 8 ▶ if ($ref eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 8 0 8
elsif ($ref eq 'ARRAY') { } 0 0 0
34 8 $data = $p->hash_recurse($_[0],"\n");
35 }
36 elsif ($ref eq 'ARRAY') {
37 0 $data = $p->array_recurse($_[0]);
38 } else {
39 0 return $ref ." unsupported\n";
40 }
41 8 $data =~ s/,\n$/;\n/;
42 8 return $p->{elements} ."\t = ". $data;
43 }
44
45 # input: pointer to hash, terminator
46 # returns: data
47 #
48 8 ▶sub hash_recurse {
Subroutine hash_recurse: called CC=3 SCAR=11.0
49 8 my($p,$ptr,$n) = @_;
50 8 ▶ $n = '' unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 8 8
51 8 my $data = "{\n";
52 8 foreach my $key (sort keys %$ptr) {
53 76 $data .= "\t'". $key ."'\t=> ";
54 76 $data .= _dump($p,$ptr->{$key},"\n");
55 }
56 8 $data .= '},'.$n;
57 }
58  
59 # generate a unique signature for a particular array
60 #
61 # input: pointer to array, terminator
62 # returns: data
63 2 ▶sub array_recurse {
Subroutine array_recurse: called CC=3 SCAR=11.0
64 2 my($p,$ptr,$n) = @_;
65 2 ▶ $n = '' unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 2 2
66 2 my $data = '[';
67 2 foreach my $item (@$ptr) {
68 2 $data .= _dump($p,$item);
69 }
70 2 $data .= "],\n";
71 }
72  
73 # input: self, item, append
74 # return: data
75 #
76 78 ▶sub _dump {
Subroutine _dump: called CC=9 SCAR=27.2
77 78 my($p,$item,$n) = @_;
78 78 $p->{elements}++;
79 78 ▶ $n = '' unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 2 76 78
80 78 my $ref = ref $item;
81 78 ▶ if ($ref eq 'HASH') {
Branch: 7 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 0 78 78
elsif ($ref eq 'ARRAY') { } 2 76 78
elsif ($ref eq 'SCALAR') { } 0 76 76
elsif ($ref eq 'GLOB') { } 0 76 76
elsif (do { my $g = \$item; ref $g eq 'GLOB' }) { } 0 76 76
elsif ($ref eq 'CODE') { } 0 76 76
elsif (defined $item) { } 76 0 76
82 0 return tabout($p->hash_recurse($item,"\n"));
83 }
84 elsif($ref eq 'ARRAY') {
85 2 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 76 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 76 return wrap_data($item) .','.$n;
102 }
103 else {
104 0 return 'undef,'.$n;
105 }
106 }
107  
108 0 ▶sub tabout {
Subroutine tabout: not called CC=2 SCAR=17.9
109 0 my @data = split(/\n/,shift);
110 0 my $data = shift @data;
111 0 $data .= "\n";
112 0 foreach(@data) {
113 0 $data .= "\t$_\n";
114 }
115 0 $data;
116 }
117  
118 76 ▶sub wrap_data {
Subroutine wrap_data: called CC=3 SCAR=11.1
119 76 my $data = shift;
120 76 ▶ return ($data =~ /\D/ || $data =~ /^$/)
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /\D/ || $data =~ /^$/ ? : 74 2 76
Condition66%2/3
$data =~ /\D/ or $data =~ /^$/
l!l&&r!l&&!r
7402
MC/DC50%1/2
$data =~ /\D/ or $data =~ /^$/
$data =~ /\D/ $data =~ /^$/
Truth table66%2/3
A or B
A= $data =~ /\D/B= $data =~ /^$/
ABresult
1X1
011
000
121 ? q|'|. $data .q|'|
122 : $data;
123 }
124  
125 3 1;
« blib/lib/Web/Scaffold.pm ↑ Summary