| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # recurse2txt routines | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # version 1.08, 12-20-12, michael@bizsystems.com | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # 10-3-11 updated to bless into calling package | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # 10-10-11 add SCALAR ref support | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # 1.06 12-16-12 add hexDumper | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # 1.07 12-19-12 added wantarray return of data and elements | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # 1.08 12-20-12 add wantarray to hexDumper | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||||||||
| 13 | #use strict; | ||||||||||||||||||||||||||||||||||||||||||
| 14 | #use diagnostics; | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 7 | ▶ | use overload; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | # generate a unique signature for a particular hash | ||||||||||||||||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # Data::Dumper actually does much more than this, however, it | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # does not stringify hash's in a consistent manner. i.e. no SORT | ||||||||||||||||||||||||||||||||||||||||||
| 22 | # | ||||||||||||||||||||||||||||||||||||||||||
| 23 | # The routine below, while not covering recursion loops, non ascii | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # characters, etc.... does produce text that can be eval'd and is | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # consistent with each rendering. | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # | ||||||||||||||||||||||||||||||||||||||||||
| 27 | sub hexDumper { | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 264 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hexDumper: called CC=2 SCAR=9.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ($data,$count) = Dumper($_[0]); | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | $data =~ s/(\b\d+)/sprintf("0x%x",$1)/ge; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | return ($data,$count); | |||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 264 | (my $x = Dumper($_[0])) =~ s/(\b\d+)/sprintf("0x%x",$1)/ge; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 264 | $x; | |||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | sub Dumper { | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 277 | ▶ | unless (defined $_[0]) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Dumper: called CC=10 SCAR=26.4
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | return ("undef\n",'undef') if wantarray; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | return "undef\n"; | |||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 277 | my $ref = ref $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 277 | ▶ | return "not a reference\n" unless $ref; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 277 | ▶ | unless ($ref eq 'HASH' or $ref eq 'ARRAY' or $ref eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$ref eq "HASH" or $ref eq "ARRAY" or $ref eq "SCALAR"
$ref eq "HASH" or $ref eq "ARRAY"
MC/DC33%1/3
$ref eq "HASH" or $ref eq "ARRAY" or $ref eq "SCALAR"
$ref eq "HASH"
$ref eq "ARRAY"
$ref eq "SCALAR"
Truth table50%2/4
A or B or C
A= $ref eq "HASH"B= $ref eq "ARRAY"C= $ref eq "SCALAR"
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 11 | ($ref) = (overload::StrVal($_[0]) =~ /^(?:.*\=)?([^=]*)\(/); | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 277 | my $p = { | |||||||||||||||||||||||||||||||||||||||||
| 48 | depth => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | elements => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 50 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 277 | (my $pkg = (caller(0))[3]) =~ s/(.+)::Dumper/$1/; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 277 | bless $p,$pkg; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 277 | my $data; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 277 | ▶ | if ($ref eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | $data = $p->hash_recurse($_[0],"\n"); | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | elsif ($ref eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 277 | $data = $p->array_recurse($_[0]); | |||||||||||||||||||||||||||||||||||||||||
| 59 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 60 | # return $ref ." unsupported\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | $data = $p->scalar_recurse($_[0]); | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 277 | $data =~ s/,\n$/;\n/; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 277 | ▶ | return ($data,$p->{elements}) if wantarray; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 277 | return $p->{elements} ."\t= ". $data; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | # input: pointer to scalar, terminator | ||||||||||||||||||||||||||||||||||||||||||
| 69 | # returns data | ||||||||||||||||||||||||||||||||||||||||||
| 70 | # | ||||||||||||||||||||||||||||||||||||||||||
| 71 | sub scalar_recurse { | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | my($p,$ptr,$n) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
scalar_recurse: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | $n = '' unless $n; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my $data = "\\"; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $data .= _dump($p,$$ptr); | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $data .= "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | # input: pointer to hash, terminator | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # returns: data | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # | ||||||||||||||||||||||||||||||||||||||||||
| 82 | sub hash_recurse { | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my($p,$ptr,$n) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hash_recurse: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | $n = '' unless $n; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | my $data = "{\n"; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | foreach my $key (sort keys %$ptr) { | |||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $data .= "\t'". $key ."'\t=> "; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | $data .= _dump($p,$ptr->{$key},"\n"); | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $data .= '},'.$n; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | # generate a unique signature for a particular array | ||||||||||||||||||||||||||||||||||||||||||
| 94 | # | ||||||||||||||||||||||||||||||||||||||||||
| 95 | # input: pointer to array, terminator | ||||||||||||||||||||||||||||||||||||||||||
| 96 | # returns: data | ||||||||||||||||||||||||||||||||||||||||||
| 97 | sub array_recurse { | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 277 | ▶ | my($p,$ptr,$n) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
array_recurse: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 277 | ▶ | $n = '' unless $n; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 277 | my $data = '['; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 277 | foreach my $item (@$ptr) { | |||||||||||||||||||||||||||||||||||||||||
| 102 | 1308 | $data .= _dump($p,$item); | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 277 | $data .= "],\n"; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | # input: self, item, append | ||||||||||||||||||||||||||||||||||||||||||
| 108 | # return: data | ||||||||||||||||||||||||||||||||||||||||||
| 109 | # | ||||||||||||||||||||||||||||||||||||||||||
| 110 | sub _dump { | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 1308 | ▶ | my($p,$item,$n) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dump: called CC=9 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 1308 | $p->{elements}++; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 1308 | ▶ | $n = '' unless $n; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 1308 | my $ref = ref $item; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 1308 | ▶ | if ($ref eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | return tabout($p->hash_recurse($item,"\n")); | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | elsif($ref eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | return $p->array_recurse($item,$n); | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | elsif($ref eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # return q|\$SCALAR,|.$n; | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | return($p->scalar_recurse($item,$n)); | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | elsif ($ref eq 'GLOB') { | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | my $g = *{$item}; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | return "\\$g" .','.$n; | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 1308 | elsif(do {my $g = \$item; ref $g eq 'GLOB'}) { | |||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | return "$item" .','.$n; | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | elsif($ref eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | return q|sub {'DUMMY'},|.$n; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | elsif (defined $item) { | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 1308 | return wrap_data($item) .','.$n; | |||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | return 'undef,'.$n; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | sub tabout { | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | my @data = split(/\n/,shift); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tabout: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | my $data = shift @data; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | $data .= "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | foreach(@data) { | |||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | $data .= "\t$_\n"; | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | $data; | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | sub wrap_data { | ||||||||||||||||||||||||||||||||||||||||||
| 154 | 1308 | ▶ | my $data = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
wrap_data: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 1308 | ▶ | return ($data =~ /\D/ || $data =~ /^$/) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$data =~ /\D/ or $data =~ /^$/
MC/DC50%1/2
$data =~ /\D/ or $data =~ /^$/
$data =~ /\D/
$data =~ /^$/
Truth table66%2/3
A or B
A= $data =~ /\D/B= $data =~ /^$/
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | ? q|'|. $data .q|'| | ||||||||||||||||||||||||||||||||||||||||||
| 157 | : $data; | ||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||