Dumper.pm

filter: Statementstmt 89.8% 169 / 188 Branchbran 72.5% 58 / 80 Conditioncond 73.3% 11 / 15 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 89.4% 17 / 19 Podpod 71.4% 5 / 7 totaltotal 82.9% 263 / 317 CC 57 SCAR 18.9
CC
57
Cov
84%
__analyse_hash
11.8
open
10.5
__analyse_array
8.4
CRAP 6.6
↑ Summary Dumper/String.pm »
1 # $Id: Dumper.pm 456 2009-04-15 12:20:59Z fil $
2 package Data::Tabular::Dumper;
3  
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use vars qw( $VERSION @ISA @EXPORT_OK );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 $VERSION="0.08";
10  
11 5 require Exporter;
12 5 @ISA = qw( Exporter );
13 5 @EXPORT_OK = qw( Dump );
14  
15  
16 ###########################################################
17 sub open
18 13 ▶{
Subroutine open: called CC=10 SCAR=23.5
Pod documented
19 13 my($package, %writers)=@_;
20 13 my $self=bless {writers=>{}, fields=>[]}, $package;
21  
22 13 $self->{master_key} = delete $writers{master_key};
23 13 ▶ $self->{master_key} = '' unless defined $self->{master_key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"master_key"} 11 2 13
24  
25 13 my($object, $one);
26 WRITER:
27 13 foreach my $p1 (keys %writers) {
28 13 foreach my $p2 ($p1, __PACKAGE__.'::'.$p1) {
29 26 ▶ if($p2->can('open') and $p2->can('close') and $p2->can('write')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p2->can("open") and $p2->can("close") and $p2->can("write")) 13 13 26
Condition66%4/6
$p2->can("open") and $p2->can("close") and $p2->can("write")
!ll&&!rl&&r
13013
$p2->can("open") and $p2->can("close")
!ll&&!rl&&r
13013
MC/DC33%1/3
$p2->can("open") and $p2->can("close") and $p2->can("write")
$p2->can("open") $p2->can("close") $p2->can("write")
Truth table50%2/4
A and B and C
A= $p2->can("open")B= $p2->can("close")C= $p2->can("write")
ABCresult
0XX0
10X0
1100
1111
30 13 $package=$p2 ;
31 13 eval {
32 13 $object=$package->open($writers{$p1});
33 };
34 13 ▶ carp $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 13 13
35 13 ▶ if($object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object) 13 0 13
36 13 $self->{writers}{$package}=$object;
37 13 $one=1;
38 }
39 13 next WRITER;
40 }
41 }
42 0 carp "Could not find a valid package for $p1 (".__PACKAGE__."::$p1)";
43 }
44 13 ▶ return unless $one;
Branch: 1 branch
BranchTrueFalseTotal
unless $one 0 13 13
45 13 return $self;
46 }
47  
48 ###########################################################
49 sub master_key
50 0 ▶{
Subroutine master_key: not called CC=2 SCAR=17.9
Pod documented
51 0 my( $self, $new_master ) = @_;
52 0 my $ret = $self->{master_key};
53 0 ▶ $self->{master_key} = $new_master if 2 == @_;
Branch: 1 branch
BranchTrueFalseTotal
if 2 == @_ 0 0 0
54 0 return $ret;
55 }
56  
57 ###########################################################
58 # Perform $name->() on all the writers.
59 sub _doall
60 20 ▶{
Subroutine _doall: called CC=1 SCAR=0.1
61 20 my($name)=@_;
62 68 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.7
63 68 my $self=shift @_;
64 68 my $n;
65 68 foreach my $o (values %{$self->{writers}}) {
66 68 my $code=$o->can($name);
67 68 ▶ if($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) { } 68 0 68
68 68 $code->($o, @_);
69 68 ▶ $n++ unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 68 0 68
70 } else {
71 0 carp "Object $o can not do $name";
72 }
73 68 ▶ carp $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 68 68
74 }
75 68 return $n;
76 20 };
77 }
78  
79 ###########################################################
80 5 *fields=_doall('fields');
81 5 *write=_doall('write');
82 5 *page_start=_doall('page_start');
83 5 *page_end=_doall('page_end');
84  
85 ###########################################################
86 sub close
87 26 ▶{
Subroutine close: called CC=3 SCAR=11.0
Pod documented
88 26 my( $self )= @_;
89  
90 26 my @ret;
91 26 foreach my $o ( values %{$self->{writers}} ) {
92 26 ▶ next unless $o->can( 'close' );
Branch: 1 branch
BranchTrueFalseTotal
unless $o->can("close") 0 26 26
93 26 push @ret, $o->close();
94 }
95 26 return @ret;
96 }
97  
98  
99 ###########################################################
100 sub DESTROY
101 13 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
102 13 $_[0]->close;
103 }
104  
105  
106 ###########################################################
107 sub available
108 4 ▶{
Subroutine available: called CC=5 SCAR=17.0
Pod undocumented
109 4 my($package)=@_;
110  
111 4 my(%res, $yes);
112 4 foreach my $p (qw(CSV XML Excel)) {
113 12 $yes=0;
114 12 ▶ $yes=1 if exists $INC{"Data/Tabular/Dumper/$p.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $INC{"Data/Tabular/Dumper/$p.pm"} 0 12 12
115 12 ▶ unless($yes) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($yes) 12 0 12
116 12 local $SIG{__DIE__}='DEFAULT';
117 12 local $SIG{__WARN__}='IGNORE';
118 12 $yes=eval "require Data::Tabular::Dumper::$p; 1;";
119 # warn $@ if $@ and $ENV{PERL_DL_NONLAZY};
120 };
121 12 $res{$p}=$yes;
122 }
123 4 ▶ return \%res unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 4 0 4
124 0 return grep {$res{$_}} keys %res;
125 }
126  
127 ###########################################################
128 sub Dump
129 0 ▶{
Subroutine Dump: not called CC=1 SCAR=6.9
Pod documented
130 0 return __PACKAGE__->dump( @_ );
131 }
132  
133 ###########################################################
134 sub dump
135 11 ▶{
Subroutine dump: called CC=5 SCAR=16.2
Pod documented
136 11 my( $self, $data ) = @_;
137  
138 11 my $ret;
139 11 ▶ unless( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 11 11
140 0 require Data::Tabular::Dumper::String;
141 0 $self = $self->open( String => \$ret, master_key=>'KEY' );
142 }
143  
144 11 my $state = $self->analyse( $data );
145 11 ▶ unless( $state->{pages} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state->{"pages"}) 6 5 11
146 6 $self->__dump( $state );
147 }
148  
149 11 my $q1=1;
150 11 foreach my $p ( @{ $state->{pages} } ) {
151 10 my $name = "Page $q1";
152 10 $q1++;
153 10 ▶ $name = $p->{name} if exists $p->{name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p->{"name"} 6 4 10
154 10 $self->page_start( $name );
155 10 $self->__dump( $p );
156 10 $self->page_end( $name );
157 }
158 11 return $ret;
159 }
160  
161 ###########################################################
162 sub __dump
163 16 ▶{
Subroutine __dump: called CC=3 SCAR=11.0
164 16 my( $self, $data ) = @_;
165  
166 16 ▶ $self->fields( $data->{fields} ) if $data->{fields};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"fields"} 10 6 16
167 16 foreach my $d ( @{ $data->{data} } ) {
168 33 $self->write( $d->{data} );
169 }
170 }
171  
172  
173 ###########################################################
174 # Convert a 2- or 3-dimensional data structure into something we can
175 # easily use.
176 # Lowest-level structure is {data=>[ ...scalars...], fields=>[ ...names...]}
177 # Other possible : maxdepth, depth (internal use)
178 # We either have an array of those in {data} (2-D)
179 # { data=>[ ...lower-level ], fields=>[....names...] }
180 # Otehr possible keys : name (if it's a part of {pages})
181 # OR we have an array of 2-D structures in {pages}
182 sub analyse
183 21 ▶{
Subroutine analyse: called CC=3 SCAR=11.0
Pod undocumented
184 21 my( $self, $data ) = @_;
185  
186 21 my $master = { maxdepth=>0, depth=>0 };
187 21 my $state = $self->__analyse( $master, $data);
188  
189 21 ▶ if( $master->{maxdepth} == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($master->{"maxdepth"} == 4) 11 10 21
190 11 $state->{pages} = delete $state->{data};
191 }
192 21 ▶ die "ARG!" if $master->{__fields};
Branch: 1 branch
BranchTrueFalseTotal
if $master->{"__fields"} 0 21 21
193  
194 21 return $state;
195 }
196  
197 ###########################################################
198 # Do the heavy lifting.
199 # Recurse over a data structure
200 sub __analyse
201 289 ▶{
Subroutine __analyse: called CC=6 SCAR=18.0
202 289 my( $self, $parent, $data ) = @_;
203 289 my $r = ref $data;
204 289 ▶ return $data unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 180 109 289
205  
206 109 ▶ die "Only 2-d and 3-d data is supported" if $parent->{depth} > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $parent->{"depth"} > 2 0 109 109
207  
208  
209 109 my $state = { depth=>$parent->{depth}+1 };
210 109 $state->{maxdepth} = $state->{depth};
211  
212 109 ▶ if( $r eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq 'ARRAY') { } 53 56 109
elsif ($r eq 'HASH') { } 56 0 56
213 53 $self->__analyse_array( $parent, $data, $state );
214 }
215 elsif( $r eq 'HASH' ) {
216 56 $self->__analyse_hash( $parent, $data, $state );
217 }
218 else {
219 0 die "Don't know how to handle $r at level $state->{depth}";
220 }
221  
222 109 ▶ $self->__analyse_rehash( $state, $data, $parent ) if $state->{__fields};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"__fields"} 18 91 109
223 109 $self->__analyse_depth( $state, $parent );
224  
225 109 return $state;
226 }
227  
228 ###########################################################
229 # Turns out $data was a HoH or LoH. So we have to change all the
230 # sub-hashes.
231 sub __analyse_rehash
232 18 ▶{
Subroutine __analyse_rehash: called CC=6 SCAR=17.9
233 18 my( $self, $state, $data, $parent ) = @_;
234 ## If we are here, $data is a LoH...
235 18 my @fields = sort keys %{ delete $state->{__fields} };
236  
237 # use Data::Denter;
238 # warn "Rehashing ", Denter $data, $state->{data};
239 18 my $first_name;
240 18 ▶ unless( 'ARRAY' eq ref $data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("ARRAY" eq ref $data) 7 11 18
241 7 my @names;
242 7 ▶ if( $state->{data}[0]{name} ) { # 3-D
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{'data'}[0]{'name'}) { } 4 3 7
243 4 @names = map { $_->{name} } @{ $state->{data} };
244 }
245 else { # HoH
246 3 @names = map { $_->{data}[0] } @{ $state->{data} };
247 }
248 7 $data = [ map { { %{$data->{$_}} } } @names ];
249 7 $first_name = 1;
250  
251 7 unshift @fields, 'HONK__TITLE__HONK';
252 7 ▶ for( my $q=0; $q <= $#$data ; $q++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q <= $data->$#*) 14 7 21
253 14 $data->[$q]{$fields[0]} = $names[$q];
254 }
255 }
256  
257 18 $state->{data} = [];
258 18 foreach my $hash ( @$data ) {
259 38 push @{ $state->{data} },
260 38 { depth=>$parent->{depth}+2, data=>[ @{$hash}{@fields} ] };
261 }
262  
263 18 ▶ $fields[0] = $self->{master_key} if $first_name;
Branch: 1 branch
BranchTrueFalseTotal
if $first_name 7 11 18
264 18 $state->{fields} = \@fields;
265 18 return;
266 }
267  
268 ###########################################################
269 # Make sure {maxdepth} of the parent is as big as can be
270 sub __analyse_depth
271 109 ▶{
Subroutine __analyse_depth: called CC=3 SCAR=11.0
272 109 my( $self, $state, $parent ) = @_;
273 109 ▶ if( $state->{depth} > $parent->{maxdepth} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"depth"} > $parent->{"maxdepth"}) 32 77 109
274 32 $parent->{maxdepth} = $state->{depth};
275 }
276  
277 109 ▶ if( $state->{maxdepth} > $parent->{maxdepth} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"maxdepth"} > $parent->{"maxdepth"}) 32 77 109
278 32 $parent->{maxdepth} = $state->{maxdepth};
279 }
280 }
281  
282  
283 ###########################################################
284 # Recurse over an arrayref
285 sub __analyse_array
286 53 ▶{
Subroutine __analyse_array: called CC=8 SCAR=21.3
287 53 my( $self, $parent, $data, $state ) = @_;
288 53 $state->{data} = [];
289  
290 53 foreach my $s ( @$data ) {
291 148 my $sub = $self->__analyse( $state, $s );
292  
293 148 ▶ if( @{ $state->{data} } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$state->{'data'};}) { } 95 53 148
elsif (ref $sub) { } 25 28 53
294 95 my $err = (!!ref $state->{data}[0] ^ !!ref $sub);
295 $err = 1 if not $err and
296 ref $state->{data}[0] and
297 ref $state->{data}[0]{data} ne
298 95 ▶ ref $sub->{data};
Branch: 1 branch
BranchTrueFalseTotal
if not $err and ref $state->{"data"}[0] and ref $state->{"data"}[0]{"data"} ne ref $sub->{"data"} 0 95 95
Condition66%4/6
not $err and ref $state->{"data"}[0] and ref $state->{"data"}[0]{"data"} ne ref $sub->{"data"}
!ll&&!rl&&r
68270
not $err and ref $state->{"data"}[0]
!ll&&!rl&&r
06827
MC/DC0%0/3
not $err and ref $state->{"data"}[0] and ref $state->{"data"}[0]{"data"} ne ref $sub->{"data"}
not $err ref $state->{"data"}[0] ref $state->{"data"}[0]{"data"} ne ref $sub->{"data"}
Truth table50%2/4
not(A) and B and C
A= not $errB= ref $state->{"data"}[0]C= ref $state->{"data"}[0]{"data"} ne ref $sub->{"data"}
ABCresult
0XX0
10X0
1100
1111
299 # $err = 1 if $state->{fields};
300 95 ▶ if( $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 95 95
301 0 die "Non-uniform data references at a level $state->{depth}";
302 }
303 }
304 elsif( ref $sub ) {
305 25 $parent->{maxdepth}++;
306 }
307 148 push @{ $state->{data} }, $sub
308 }
309 }
310  
311 ###########################################################
312 # Recurse over a hashref
313 sub __analyse_hash
314 56 ▶{
Subroutine __analyse_hash: called CC=10 SCAR=24.7
315 56 my( $self, $parent, $data, $state ) = @_;
316 56 $state->{data} = [];
317  
318 56 ▶ if( $parent->{fields} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->{"fields"}) 0 56 56
319 0 foreach my $k ( @{ $parent->{fields} } ) {
320 0 push @{ $state->{data} }, $data->{$k};
321 }
322 0 return;
323 }
324  
325 56 foreach my $k ( sort keys %$data ) {
326 120 my $sub = $self->__analyse( $state, $data->{$k} );
327  
328 120 ▶ unless( ref $sub ) { # $data is a hash
Branch: 2 branches
BranchTrueFalseTotal
if (ref $sub) { } 36 84 120
if (do { $sub->{'maxdepth'} == 3 }) { } 26 10 36
329 84 $parent->{__fields}{$k} = 1;
330 # $fields{$k}=1;
331 }
332 elsif( $sub->{maxdepth}==3 ) { # $data is a HoLoL
333 26 $sub->{name} = $k;
334 }
335 else {
336 10 my $r = ref $sub->{data};
337 10 ▶ if( $r eq 'ARRAY' ) { # $data is a HoL
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq 'ARRAY') { } 10 0 10
elsif ($r eq 'HASH') { } 0 0 0
338 10 unshift @{$sub->{data}}, $k;
339 }
340 elsif( $r eq 'HASH' ) { # $data is a HoH
341 0 $sub->{name}=$k;
342 }
343 }
344 120 ▶ if( 0== @{ $state->{data} } and ref $sub ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == @{$state->{"data"};} and ref $sub) 18 102 120
Condition100%3/3
0 == @{$state->{"data"};} and ref $sub
!ll&&!rl&&r
643818
MC/DC100%2/2
0 == @{$state->{"data"};} and ref $sub
0 == @{$state->{"data"};} ref $sub
Truth table100%3/3
A and B
A= 0 == @{$state->{"data"};}B= ref $sub
ABresult
0X0
100
111
345 18 $parent->{maxdepth}++;
346 }
347 120 push @{$state->{data}}, $sub;
348 }
349 }
350  
351 5 1;
352 __END__
↑ Summary Dumper/String.pm »