line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
6
|
|
|
6
|
|
3433
|
use overload; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
33
|
|
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
|
17
|
50
|
|
17
|
|
7664
|
return "undef\n" unless defined $_[0]; |
22
|
17
|
|
|
|
|
41
|
my $ref = ref $_[0]; |
23
|
17
|
50
|
|
|
|
52
|
return "not a reference\n" unless $ref; |
24
|
17
|
50
|
66
|
|
|
77
|
unless ($ref eq 'HASH' or $ref eq 'ARRAY') { |
25
|
0
|
|
|
|
|
0
|
($ref) = (overload::StrVal($_[0]) =~ /^(?:.*\=)?([^=]*)\(/); |
26
|
|
|
|
|
|
|
} |
27
|
17
|
|
|
|
|
66
|
my $p = { |
28
|
|
|
|
|
|
|
depth => 0, |
29
|
|
|
|
|
|
|
elements => 0, |
30
|
|
|
|
|
|
|
}; |
31
|
17
|
|
|
|
|
36
|
bless $p,__PACKAGE__; |
32
|
17
|
|
|
|
|
17
|
my $data; |
33
|
17
|
100
|
|
|
|
43
|
if ($ref eq 'HASH') { |
|
|
50
|
|
|
|
|
|
34
|
9
|
|
|
|
|
42
|
$data = $p->hash_recurse($_[0],"\n"); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
elsif ($ref eq 'ARRAY') { |
37
|
8
|
|
|
|
|
28
|
$data = $p->array_recurse($_[0]); |
38
|
|
|
|
|
|
|
} else { |
39
|
0
|
|
|
|
|
0
|
return $ref ." unsupported\n"; |
40
|
|
|
|
|
|
|
} |
41
|
17
|
|
|
|
|
124
|
$data =~ s/,\n$/;\n/; |
42
|
17
|
|
|
|
|
167
|
return $p->{elements} ."\t= ". $data; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# input: pointer to hash, terminator |
46
|
|
|
|
|
|
|
# returns: data |
47
|
|
|
|
|
|
|
# |
48
|
|
|
|
|
|
|
sub hash_recurse { |
49
|
40
|
|
|
40
|
|
50
|
my($p,$ptr,$n) = @_; |
50
|
40
|
50
|
|
|
|
64
|
$n = '' unless $n; |
51
|
40
|
|
|
|
|
43
|
my $data = "{\n"; |
52
|
40
|
|
|
|
|
124
|
foreach my $key (sort keys %$ptr) { |
53
|
106
|
|
|
|
|
224
|
$data .= "\t'". $key ."'\t=> "; |
54
|
106
|
|
|
|
|
330
|
$data .= _dump($p,$ptr->{$key},"\n"); |
55
|
|
|
|
|
|
|
} |
56
|
40
|
|
|
|
|
93
|
$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
|
8
|
|
|
8
|
|
8
|
my($p,$ptr,$n) = @_; |
65
|
8
|
50
|
|
|
|
17
|
$n = '' unless $n; |
66
|
8
|
|
|
|
|
8
|
my $data = '['; |
67
|
8
|
|
|
|
|
9
|
foreach my $item (@$ptr) { |
68
|
184
|
|
|
|
|
191
|
$data .= _dump($p,$item); |
69
|
|
|
|
|
|
|
} |
70
|
8
|
|
|
|
|
12
|
$data .= "],\n"; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# input: self, item, append |
74
|
|
|
|
|
|
|
# return: data |
75
|
|
|
|
|
|
|
# |
76
|
|
|
|
|
|
|
sub _dump { |
77
|
290
|
|
|
290
|
|
259
|
my($p,$item,$n) = @_; |
78
|
290
|
|
|
|
|
374
|
$p->{elements}++; |
79
|
290
|
100
|
|
|
|
400
|
$n = '' unless $n; |
80
|
290
|
|
|
|
|
214
|
my $ref = ref $item; |
81
|
290
|
100
|
|
|
|
599
|
if ($ref eq 'HASH') { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
82
|
31
|
|
|
|
|
55
|
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
|
259
|
|
|
|
|
196
|
elsif ($ref eq 'GLOB') { |
91
|
0
|
|
|
|
|
0
|
my $g = *{$item}; |
|
0
|
|
|
|
|
0
|
|
92
|
0
|
|
|
|
|
0
|
return "\\$g" .','.$n; |
93
|
|
|
|
|
|
|
} |
94
|
259
|
|
|
|
|
611
|
elsif(do {my $g = \$item; ref $g eq 'GLOB'}) { |
95
|
0
|
|
|
|
|
0
|
return "$item" .','.$n; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
elsif($ref eq 'CODE') { |
98
|
0
|
|
|
|
|
0
|
return q|sub {'DUMMY'},|.$n; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
elsif (defined $item) { |
101
|
259
|
|
|
|
|
286
|
return wrap_data($item) .','.$n; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
else { |
104
|
0
|
|
|
|
|
0
|
return 'undef,'.$n; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub tabout { |
109
|
31
|
|
|
31
|
|
70
|
my @data = split(/\n/,shift); |
110
|
31
|
|
|
|
|
29
|
my $data = shift @data; |
111
|
31
|
|
|
|
|
28
|
$data .= "\n"; |
112
|
31
|
|
|
|
|
30
|
foreach(@data) { |
113
|
82
|
|
|
|
|
93
|
$data .= "\t$_\n"; |
114
|
|
|
|
|
|
|
} |
115
|
31
|
|
|
|
|
67
|
$data; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub wrap_data { |
119
|
259
|
|
|
259
|
|
200
|
my $data = shift; |
120
|
259
|
100
|
66
|
|
|
1225
|
return ($data =~ /\D/ || $data =~ /^$/) |
121
|
|
|
|
|
|
|
? q|'|. $data .q|'| |
122
|
|
|
|
|
|
|
: $data; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
1; |