File Coverage

recurse2txt
Criterion Covered Total %
statement 38 55 69.0
branch 18 32 56.2
condition 3 6 50.0
subroutine 6 7 85.7
pod n/a
total 65 100 65.0


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   1348 use overload;
  3         6  
  3         23  
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 8 50   8   300 return "undef\n" unless defined $_[0];
22 8         16 my $ref = ref $_[0];
23 8 50       16 return "not a reference\n" unless $ref;
24 8 50 33     23 unless ($ref eq 'HASH' or $ref eq 'ARRAY') {
25 0         0 ($ref) = (overload::StrVal($_[0]) =~ /^(?:.*\=)?([^=]*)\(/);
26             }
27 8         41 my $p = {
28             depth => 0,
29             elements => 0,
30             };
31 8         20 bless $p,__PACKAGE__;
32 8         8 my $data;
33 8 50       15 if ($ref eq 'HASH') {
    0          
34 8         21 $data = $p->hash_recurse($_[0],"\n");
35             }
36             elsif ($ref eq 'ARRAY') {
37 0         0 $data = $p->array_recurse($_[0]);
38             } else {
39 0         0 return $ref ." unsupported\n";
40             }
41 8         34 $data =~ s/,\n$/;\n/;
42 8         78 return $p->{elements} ."\t = ". $data;
43             }
44            
45             # input: pointer to hash, terminator
46             # returns: data
47             #
48             sub hash_recurse {
49 8     8   11 my($p,$ptr,$n) = @_;
50 8 50       15 $n = '' unless $n;
51 8         19 my $data = "{\n";
52 8         60 foreach my $key (sort keys %$ptr) {
53 76         126 $data .= "\t'". $key ."'\t=> ";
54 76         172 $data .= _dump($p,$ptr->{$key},"\n");
55             }
56 8         27 $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 2     2   5 my($p,$ptr,$n) = @_;
65 2 50       5 $n = '' unless $n;
66 2         4 my $data = '[';
67 2         25 foreach my $item (@$ptr) {
68 2         7 $data .= _dump($p,$item);
69             }
70 2         8 $data .= "],\n";
71             }
72              
73             # input: self, item, append
74             # return: data
75             #
76             sub _dump {
77 78     78   114 my($p,$item,$n) = @_;
78 78         217 $p->{elements}++;
79 78 100       129 $n = '' unless $n;
80 78         95 my $ref = ref $item;
81 78 50       218 if ($ref eq 'HASH') {
    100          
    50          
    50          
    50          
    50          
    50          
82 0         0 return tabout($p->hash_recurse($item,"\n"));
83             }
84             elsif($ref eq 'ARRAY') {
85 2         4 return $p->array_recurse($item,$n);
86             }
87             elsif($ref eq 'SCALAR') {
88 0         0 return q|\$SCALAR,|.$n;
89             }
90 76         116 elsif ($ref eq 'GLOB') {
91 0         0 my $g = *{$item};
  0         0  
92 0         0 return "\\$g" .','.$n;
93             }
94 76         259 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 76         135 return wrap_data($item) .','.$n;
102             }
103             else {
104 0         0 return 'undef,'.$n;
105             }
106             }
107              
108             sub tabout {
109 0     0   0 my @data = split(/\n/,shift);
110 0         0 my $data = shift @data;
111 0         0 $data .= "\n";
112 0         0 foreach(@data) {
113 0         0 $data .= "\t$_\n";
114             }
115 0         0 $data;
116             }
117              
118             sub wrap_data {
119 76     76   93 my $data = shift;
120 76 100 66     486 return ($data =~ /\D/ || $data =~ /^$/)
121             ? q|'|. $data .q|'|
122             : $data;
123             }
124              
125             1;