line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
3
|
|
2119
|
use overload; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
32
|
|
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
|
|
|
|
|
|
|
sub Dumper { |
21
|
129
|
50
|
|
129
|
|
686
|
return "undef\n" unless defined $_[0]; |
22
|
129
|
|
|
|
|
218
|
my $ref = ref $_[0]; |
23
|
129
|
50
|
|
|
|
277
|
return "not a reference\n" unless $ref; |
24
|
129
|
50
|
66
|
|
|
362
|
unless ($ref eq 'HASH' or $ref eq 'ARRAY') { |
25
|
0
|
|
|
|
|
0
|
($ref) = (overload::StrVal($_[0]) =~ /^(?:.*\=)?([^=]*)\(/); |
26
|
|
|
|
|
|
|
} |
27
|
129
|
|
|
|
|
427
|
my $p = { |
28
|
|
|
|
|
|
|
depth => 0, |
29
|
|
|
|
|
|
|
elements => 0, |
30
|
|
|
|
|
|
|
}; |
31
|
129
|
|
|
|
|
259
|
bless $p,__PACKAGE__; |
32
|
129
|
|
|
|
|
143
|
my $data; |
33
|
129
|
100
|
|
|
|
258
|
if ($ref eq 'HASH') { |
|
|
50
|
|
|
|
|
|
34
|
112
|
|
|
|
|
292
|
$data = $p->hash_recurse($_[0],"\n"); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
elsif ($ref eq 'ARRAY') { |
37
|
17
|
|
|
|
|
42
|
$data = $p->array_recurse($_[0]); |
38
|
|
|
|
|
|
|
} else { |
39
|
0
|
|
|
|
|
0
|
return $ref ." unsupported\n"; |
40
|
|
|
|
|
|
|
} |
41
|
129
|
|
|
|
|
611
|
$data =~ s/,\n$/;\n/; |
42
|
129
|
|
|
|
|
1048
|
return $p->{elements} ."\t = ". $data; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# input: pointer to hash, terminator |
46
|
|
|
|
|
|
|
# returns: data |
47
|
|
|
|
|
|
|
# |
48
|
|
|
|
|
|
|
sub hash_recurse { |
49
|
220
|
|
|
220
|
|
413
|
my($p,$ptr,$n) = @_; |
50
|
220
|
50
|
|
|
|
459
|
$n = '' unless $n; |
51
|
220
|
|
|
|
|
267
|
my $data = "{\n"; |
52
|
220
|
|
|
|
|
933
|
foreach my $key (sort keys %$ptr) { |
53
|
1034
|
|
|
|
|
1781
|
$data .= "\t'". $key ."'\t=> "; |
54
|
1034
|
|
|
|
|
2167
|
$data .= _dump($p,$ptr->{$key},"\n"); |
55
|
|
|
|
|
|
|
} |
56
|
220
|
|
|
|
|
794
|
$data .= '},'.$n; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# generate a unique signature for a particular array |
60
|
|
|
|
|
|
|
# |
61
|
|
|
|
|
|
|
# input: pointer to array, terminator |
62
|
|
|
|
|
|
|
# returns: data |
63
|
|
|
|
|
|
|
sub array_recurse { |
64
|
17
|
|
|
17
|
|
26
|
my($p,$ptr,$n) = @_; |
65
|
17
|
50
|
|
|
|
31
|
$n = '' unless $n; |
66
|
17
|
|
|
|
|
19
|
my $data = '['; |
67
|
17
|
|
|
|
|
27
|
foreach my $item (@$ptr) { |
68
|
10
|
|
|
|
|
20
|
$data .= _dump($p,$item); |
69
|
|
|
|
|
|
|
} |
70
|
17
|
|
|
|
|
46
|
$data .= "],\n"; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# input: self, item, append |
74
|
|
|
|
|
|
|
# return: data |
75
|
|
|
|
|
|
|
# |
76
|
|
|
|
|
|
|
sub _dump { |
77
|
1044
|
|
|
1044
|
|
1543
|
my($p,$item,$n) = @_; |
78
|
1044
|
|
|
|
|
1610
|
$p->{elements}++; |
79
|
1044
|
100
|
|
|
|
2137
|
$n = '' unless $n; |
80
|
1044
|
|
|
|
|
1409
|
my $ref = ref $item; |
81
|
1044
|
100
|
|
|
|
3098
|
if ($ref eq 'HASH') { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
82
|
108
|
|
|
|
|
257
|
return tabout($p->hash_recurse($item,"\n")); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
elsif($ref eq 'ARRAY') { |
85
|
0
|
|
|
|
|
0
|
return $p->array_recurse($item,$n); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
elsif($ref eq 'SCALAR') { |
88
|
0
|
|
|
|
|
0
|
return q|\$SCALAR,|.$n; |
89
|
|
|
|
|
|
|
} |
90
|
936
|
|
|
|
|
1529
|
elsif ($ref eq 'GLOB') { |
91
|
0
|
|
|
|
|
0
|
my $g = *{$item}; |
|
0
|
|
|
|
|
0
|
|
92
|
0
|
|
|
|
|
0
|
return "\\$g" .','.$n; |
93
|
|
|
|
|
|
|
} |
94
|
936
|
|
|
|
|
3820
|
elsif(do {my $g = \$item; ref $g eq 'GLOB'}) { |
95
|
30
|
|
|
|
|
227
|
return "$item" .','.$n; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
elsif($ref eq 'CODE') { |
98
|
130
|
|
|
|
|
495
|
return q|sub {'DUMMY'},|.$n; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
elsif (defined $item) { |
101
|
726
|
|
|
|
|
1688
|
return wrap_data($item) .','.$n; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
else { |
104
|
50
|
|
|
|
|
162
|
return 'undef,'.$n; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub tabout { |
109
|
108
|
|
|
108
|
|
582
|
my @data = split(/\n/,shift); |
110
|
108
|
|
|
|
|
194
|
my $data = shift @data; |
111
|
108
|
|
|
|
|
143
|
$data .= "\n"; |
112
|
108
|
|
|
|
|
238
|
foreach(@data) { |
113
|
366
|
|
|
|
|
868
|
$data .= "\t$_\n"; |
114
|
|
|
|
|
|
|
} |
115
|
108
|
|
|
|
|
499
|
$data; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub wrap_data { |
119
|
726
|
|
|
726
|
|
879
|
my $data = shift; |
120
|
726
|
100
|
100
|
|
|
5122
|
return ($data =~ /\D/ || $data =~ /^$/) |
121
|
|
|
|
|
|
|
? q|'|. $data .q|'| |
122
|
|
|
|
|
|
|
: $data; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
1; |