| 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
| |||||||||||||||||||||||||||||||||||
| 22 | 18 | my $ref = ref $_[0]; | |||||||||||||||||||||||||||||||||
| 23 | 18 | ▶ | return "not a reference\n" unless $ref; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 24 | 18 | ▶ | unless ($ref eq 'HASH' or $ref eq 'ARRAY') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ref eq "HASH" or $ref eq "ARRAY"
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"
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 80 | 269 | my $ref = ref $item; | |||||||||||||||||||||||||||||||||
| 81 | 269 | ▶ | if ($ref eq 'HASH') { | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$data =~ /\D/ or $data =~ /^$/
MC/DC100%2/2
$data =~ /\D/ or $data =~ /^$/
$data =~ /\D/
$data =~ /^$/
Truth table100%3/3
A or B
A= $data =~ /\D/B= $data =~ /^$/
| |||||||||||||||||||||||||||||||||||
| 121 | ? q|'|. $data .q|'| | ||||||||||||||||||||||||||||||||||
| 122 | : $data; | ||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | 7 | 1; | |||||||||||||||||||||||||||||||||