File Coverage

blib/lib/Data/Hash/Flatten.pm
Criterion Covered Total %
statement 24 24 100.0
branch 4 4 100.0
condition n/a
subroutine 5 5 100.0
pod 0 1 0.0
total 33 34 97.0


line stmt bran cond sub pod time code
1             package Data::Hash::Flatten;
2              
3             require 5.005_62;
4 1     1   728 use strict;
  1         2  
  1         38  
5 1     1   4 use warnings;
  1         2  
  1         25  
6 1     1   942 use Data::Dumper;
  1         10693  
  1         373  
7              
8             require Exporter;
9              
10             our @ISA = qw(Exporter);
11              
12             # Items to export into callers namespace by default. Note: do not export
13             # names by default without a very good reason. Use EXPORT_OK instead.
14             # Do not simply export all your public functions/methods/constants.
15              
16             # This allows declaration use Data::Hash::Flatten ':all';
17             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
18             # will save memory.
19             our %EXPORT_TAGS = ( 'all' => [ qw(
20            
21             ) ] );
22              
23             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
24              
25             our @EXPORT = qw(
26            
27             );
28             our $VERSION = '0.03';
29              
30             our @flattened;
31              
32             sub this {
33 11     11 0 80 my (undef, $href, $field, $depth, $flat_rec) = @_;
34              
35 11 100       27 @flattened = () unless defined($depth);
36              
37 11 100       19 if (ref $href) {
38 5         17 my @key = keys %$href;
39             # warn "ref $href succeeded. depth: $depth keys: @key, the ref:", Dumper($href);
40 5         14 for my $key_i (0..$#key) {
41              
42 10         13 my $key = $key[$key_i];
43              
44             # warn "KEY: $key";
45 10         94 $flat_rec->{$field->[$depth]} = $key;
46             # warn "(depth $depth) flat_rec->{$field->[$depth]} = $key";
47              
48 10         66 Data::Hash::Flatten->this($href->{$key}, $field, $depth+1, $flat_rec, @flattened);
49             }
50             } else {
51              
52 6         11 $flat_rec->{$field->[$depth]} = $href;
53              
54             # warn "no more refs. we are at bottom. pushing:", Dumper($flat_rec), "here is href:", Dumper($href), "depth $depth";
55 1     1   28060 use Storable qw(dclone);
  1         9946  
  1         215  
56              
57 6         209 my $new_rec = dclone $flat_rec;
58 6         14 push @flattened, $new_rec;
59             }
60              
61 11         33 @flattened;
62             }
63              
64             1;
65             __END__