line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
7
|
|
5283
|
use overload; |
|
7
|
|
|
|
|
17
|
|
|
7
|
|
|
|
|
52
|
|
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
|
50
|
|
264
|
|
3846
|
if (wantarray) { |
29
|
0
|
|
|
|
|
0
|
($data,$count) = Dumper($_[0]); |
30
|
0
|
|
|
|
|
0
|
$data =~ s/(\b\d+)/sprintf("0x%x",$1)/ge; |
|
0
|
|
|
|
|
0
|
|
31
|
0
|
|
|
|
|
0
|
return ($data,$count); |
32
|
|
|
|
|
|
|
} |
33
|
264
|
|
|
|
|
545
|
(my $x = Dumper($_[0])) =~ s/(\b\d+)/sprintf("0x%x",$1)/ge; |
|
1403
|
|
|
|
|
5310
|
|
34
|
264
|
|
|
|
|
774
|
$x; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub Dumper { |
38
|
277
|
50
|
|
277
|
|
802
|
unless (defined $_[0]) { |
39
|
0
|
0
|
|
|
|
0
|
return ("undef\n",'undef') if wantarray; |
40
|
0
|
|
|
|
|
0
|
return "undef\n"; |
41
|
|
|
|
|
|
|
} |
42
|
277
|
|
|
|
|
465
|
my $ref = ref $_[0]; |
43
|
277
|
50
|
|
|
|
518
|
return "not a reference\n" unless $ref; |
44
|
277
|
100
|
66
|
|
|
1421
|
unless ($ref eq 'HASH' or $ref eq 'ARRAY' or $ref eq 'SCALAR') { |
|
|
|
66
|
|
|
|
|
45
|
11
|
|
|
|
|
39
|
($ref) = (overload::StrVal($_[0]) =~ /^(?:.*\=)?([^=]*)\(/); |
46
|
|
|
|
|
|
|
} |
47
|
277
|
|
|
|
|
831
|
my $p = { |
48
|
|
|
|
|
|
|
depth => 0, |
49
|
|
|
|
|
|
|
elements => 0, |
50
|
|
|
|
|
|
|
}; |
51
|
277
|
|
|
|
|
2093
|
(my $pkg = (caller(0))[3]) =~ s/(.+)::Dumper/$1/; |
52
|
277
|
|
|
|
|
590
|
bless $p,$pkg; |
53
|
277
|
|
|
|
|
315
|
my $data; |
54
|
277
|
50
|
|
|
|
751
|
if ($ref eq 'HASH') { |
|
|
50
|
|
|
|
|
|
55
|
0
|
|
|
|
|
0
|
$data = $p->hash_recurse($_[0],"\n"); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
elsif ($ref eq 'ARRAY') { |
58
|
277
|
|
|
|
|
765
|
$data = $p->array_recurse($_[0]); |
59
|
|
|
|
|
|
|
} else { |
60
|
|
|
|
|
|
|
# return $ref ." unsupported\n"; |
61
|
0
|
|
|
|
|
0
|
$data = $p->scalar_recurse($_[0]); |
62
|
|
|
|
|
|
|
} |
63
|
277
|
|
|
|
|
1018
|
$data =~ s/,\n$/;\n/; |
64
|
277
|
50
|
|
|
|
610
|
return ($data,$p->{elements}) if wantarray; |
65
|
277
|
|
|
|
|
1824
|
return $p->{elements} ."\t= ". $data; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# input: pointer to scalar, terminator |
69
|
|
|
|
|
|
|
# returns data |
70
|
|
|
|
|
|
|
# |
71
|
|
|
|
|
|
|
sub scalar_recurse { |
72
|
0
|
|
|
0
|
|
0
|
my($p,$ptr,$n) = @_; |
73
|
0
|
0
|
|
|
|
0
|
$n = '' unless $n; |
74
|
0
|
|
|
|
|
0
|
my $data = "\\"; |
75
|
0
|
|
|
|
|
0
|
$data .= _dump($p,$$ptr); |
76
|
0
|
|
|
|
|
0
|
$data .= "\n"; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# input: pointer to hash, terminator |
80
|
|
|
|
|
|
|
# returns: data |
81
|
|
|
|
|
|
|
# |
82
|
|
|
|
|
|
|
sub hash_recurse { |
83
|
0
|
|
|
0
|
|
0
|
my($p,$ptr,$n) = @_; |
84
|
0
|
0
|
|
|
|
0
|
$n = '' unless $n; |
85
|
0
|
|
|
|
|
0
|
my $data = "{\n"; |
86
|
0
|
|
|
|
|
0
|
foreach my $key (sort keys %$ptr) { |
87
|
0
|
|
|
|
|
0
|
$data .= "\t'". $key ."'\t=> "; |
88
|
0
|
|
|
|
|
0
|
$data .= _dump($p,$ptr->{$key},"\n"); |
89
|
|
|
|
|
|
|
} |
90
|
0
|
|
|
|
|
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
|
|
|
277
|
|
416
|
my($p,$ptr,$n) = @_; |
99
|
277
|
50
|
|
|
|
603
|
$n = '' unless $n; |
100
|
277
|
|
|
|
|
376
|
my $data = '['; |
101
|
277
|
|
|
|
|
455
|
foreach my $item (@$ptr) { |
102
|
1308
|
|
|
|
|
2464
|
$data .= _dump($p,$item); |
103
|
|
|
|
|
|
|
} |
104
|
277
|
|
|
|
|
662
|
$data .= "],\n"; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# input: self, item, append |
108
|
|
|
|
|
|
|
# return: data |
109
|
|
|
|
|
|
|
# |
110
|
|
|
|
|
|
|
sub _dump { |
111
|
1308
|
|
|
1308
|
|
1794
|
my($p,$item,$n) = @_; |
112
|
1308
|
|
|
|
|
2078
|
$p->{elements}++; |
113
|
1308
|
50
|
|
|
|
2554
|
$n = '' unless $n; |
114
|
1308
|
|
|
|
|
1707
|
my $ref = ref $item; |
115
|
1308
|
50
|
|
|
|
4114
|
if ($ref eq 'HASH') { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
116
|
0
|
|
|
|
|
0
|
return tabout($p->hash_recurse($item,"\n")); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
elsif($ref eq 'ARRAY') { |
119
|
0
|
|
|
|
|
0
|
return $p->array_recurse($item,$n); |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
elsif($ref eq 'SCALAR') { |
122
|
|
|
|
|
|
|
# return q|\$SCALAR,|.$n; |
123
|
0
|
|
|
|
|
0
|
return($p->scalar_recurse($item,$n)); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
elsif ($ref eq 'GLOB') { |
126
|
0
|
|
|
|
|
0
|
my $g = *{$item}; |
|
0
|
|
|
|
|
0
|
|
127
|
0
|
|
|
|
|
0
|
return "\\$g" .','.$n; |
128
|
|
|
|
|
|
|
} |
129
|
1308
|
|
|
|
|
1658
|
elsif(do {my $g = \$item; ref $g eq 'GLOB'}) { |
|
1308
|
|
|
|
|
4691
|
|
130
|
0
|
|
|
|
|
0
|
return "$item" .','.$n; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
elsif($ref eq 'CODE') { |
133
|
0
|
|
|
|
|
0
|
return q|sub {'DUMMY'},|.$n; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
elsif (defined $item) { |
136
|
1308
|
|
|
|
|
2325
|
return wrap_data($item) .','.$n; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
else { |
139
|
0
|
|
|
|
|
0
|
return 'undef,'.$n; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub tabout { |
144
|
0
|
|
|
0
|
|
0
|
my @data = split(/\n/,shift); |
145
|
0
|
|
|
|
|
0
|
my $data = shift @data; |
146
|
0
|
|
|
|
|
0
|
$data .= "\n"; |
147
|
0
|
|
|
|
|
0
|
foreach(@data) { |
148
|
0
|
|
|
|
|
0
|
$data .= "\t$_\n"; |
149
|
|
|
|
|
|
|
} |
150
|
0
|
|
|
|
|
0
|
$data; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub wrap_data { |
154
|
1308
|
|
|
1308
|
|
1732
|
my $data = shift; |
155
|
1308
|
100
|
66
|
|
|
9735
|
return ($data =~ /\D/ || $data =~ /^$/) |
156
|
|
|
|
|
|
|
? q|'|. $data .q|'| |
157
|
|
|
|
|
|
|
: $data; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
1; |