| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# recurse2txt routines |
| 4 |
# |
| 5 |
# version 1.08, 12-20-12, [email protected] |
| 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 |
use overload; |
| 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 |
if (wantarray) { |
| 29 |
($data,$count) = Dumper($_[0]); |
| 30 |
$data =~ s/(\b\d+)/sprintf("0x%x",$1)/ge; |
| 31 |
return ($data,$count); |
| 32 |
} |
| 33 |
(my $x = Dumper($_[0])) =~ s/(\b\d+)/sprintf("0x%x",$1)/ge; |
| 34 |
$x; |
| 35 |
} |
| 36 |
|
| 37 |
sub Dumper { |
| 38 |
unless (defined $_[0]) { |
| 39 |
return ("undef\n",'undef') if wantarray; |
| 40 |
return "undef\n"; |
| 41 |
} |
| 42 |
my $ref = ref $_[0]; |
| 43 |
return "not a reference\n" unless $ref; |
| 44 |
unless ($ref eq 'HASH' or $ref eq 'ARRAY' or $ref eq 'SCALAR') { |
| 45 |
($ref) = (overload::StrVal($_[0]) =~ /^(?:.*\=)?([^=]*)\(/); |
| 46 |
} |
| 47 |
my $p = { |
| 48 |
depth => 0, |
| 49 |
elements => 0, |
| 50 |
}; |
| 51 |
(my $pkg = (caller(0))[3]) =~ s/(.+)::Dumper/$1/; |
| 52 |
bless $p,$pkg; |
| 53 |
my $data; |
| 54 |
if ($ref eq 'HASH') { |
| 55 |
$data = $p->hash_recurse($_[0],"\n"); |
| 56 |
} |
| 57 |
elsif ($ref eq 'ARRAY') { |
| 58 |
$data = $p->array_recurse($_[0]); |
| 59 |
} else { |
| 60 |
# return $ref ." unsupported\n"; |
| 61 |
$data = $p->scalar_recurse($_[0]); |
| 62 |
} |
| 63 |
$data =~ s/,\n$/;\n/; |
| 64 |
return ($data,$p->{elements}) if wantarray; |
| 65 |
return $p->{elements} ."\t= ". $data; |
| 66 |
} |
| 67 |
|
| 68 |
# input: pointer to scalar, terminator |
| 69 |
# returns data |
| 70 |
# |
| 71 |
sub scalar_recurse { |
| 72 |
my($p,$ptr,$n) = @_; |
| 73 |
$n = '' unless $n; |
| 74 |
my $data = "\\"; |
| 75 |
$data .= _dump($p,$$ptr); |
| 76 |
$data .= "\n"; |
| 77 |
} |
| 78 |
|
| 79 |
# input: pointer to hash, terminator |
| 80 |
# returns: data |
| 81 |
# |
| 82 |
sub hash_recurse { |
| 83 |
my($p,$ptr,$n) = @_; |
| 84 |
$n = '' unless $n; |
| 85 |
my $data = "{\n"; |
| 86 |
foreach my $key (sort keys %$ptr) { |
| 87 |
$data .= "\t'". $key ."'\t=> "; |
| 88 |
$data .= _dump($p,$ptr->{$key},"\n"); |
| 89 |
} |
| 90 |
$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 |
my($p,$ptr,$n) = @_; |
| 99 |
$n = '' unless $n; |
| 100 |
my $data = '['; |
| 101 |
foreach my $item (@$ptr) { |
| 102 |
$data .= _dump($p,$item); |
| 103 |
} |
| 104 |
$data .= "],\n"; |
| 105 |
} |
| 106 |
|
| 107 |
# input: self, item, append |
| 108 |
# return: data |
| 109 |
# |
| 110 |
sub _dump { |
| 111 |
my($p,$item,$n) = @_; |
| 112 |
$p->{elements}++; |
| 113 |
$n = '' unless $n; |
| 114 |
my $ref = ref $item; |
| 115 |
if ($ref eq 'HASH') { |
| 116 |
return tabout($p->hash_recurse($item,"\n")); |
| 117 |
} |
| 118 |
elsif($ref eq 'ARRAY') { |
| 119 |
return $p->array_recurse($item,$n); |
| 120 |
} |
| 121 |
elsif($ref eq 'SCALAR') { |
| 122 |
# return q|\$SCALAR,|.$n; |
| 123 |
return($p->scalar_recurse($item,$n)); |
| 124 |
} |
| 125 |
elsif ($ref eq 'GLOB') { |
| 126 |
my $g = *{$item}; |
| 127 |
return "\\$g" .','.$n; |
| 128 |
} |
| 129 |
elsif(do {my $g = \$item; ref $g eq 'GLOB'}) { |
| 130 |
return "$item" .','.$n; |
| 131 |
} |
| 132 |
elsif($ref eq 'CODE') { |
| 133 |
return q|sub {'DUMMY'},|.$n; |
| 134 |
} |
| 135 |
elsif (defined $item) { |
| 136 |
return wrap_data($item) .','.$n; |
| 137 |
} |
| 138 |
else { |
| 139 |
return 'undef,'.$n; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
sub tabout { |
| 144 |
my @data = split(/\n/,shift); |
| 145 |
my $data = shift @data; |
| 146 |
$data .= "\n"; |
| 147 |
foreach(@data) { |
| 148 |
$data .= "\t$_\n"; |
| 149 |
} |
| 150 |
$data; |
| 151 |
} |
| 152 |
|
| 153 |
sub wrap_data { |
| 154 |
my $data = shift; |
| 155 |
return ($data =~ /\D/ || $data =~ /^$/) |
| 156 |
? q|'|. $data .q|'| |
| 157 |
: $data; |
| 158 |
} |
| 159 |
|
| 160 |
1; |