| 1 | package Data::Hash::Flatten; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | require 5.005_62; | |||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | require Exporter; | |||||||||
| 9 | |||||||||||
| 10 | 1 | 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 | 1 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||
| 20 | |||||||||||
| 21 | ) ] ); | ||||||||||
| 22 | |||||||||||
| 23 | 1 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||
| 24 | |||||||||||
| 25 | 1 | our @EXPORT = qw( | |||||||||
| 26 | |||||||||||
| 27 | ); | ||||||||||
| 28 | 1 | our $VERSION = '0.03'; | |||||||||
| 29 | |||||||||||
| 30 | 1 | our @flattened; | |||||||||
| 31 | |||||||||||
| 32 | 11 | ▶ | sub this { | ||||||||
|
Subroutine
this: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 33 | 11 | my (undef, $href, $field, $depth, $flat_rec) = @_; | |||||||||
| 34 | |||||||||||
| 35 | 11 | ▶ | @flattened = () unless defined($depth); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | |||||||||||
| 37 | 11 | ▶ | if (ref $href) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 5 | my @key = keys %$href; | |||||||||
| 39 | # warn "ref $href succeeded. depth: $depth keys: @key, the ref:", Dumper($href); | ||||||||||
| 40 | 5 | for my $key_i (0..$#key) { | |||||||||
| 41 | |||||||||||
| 42 | 10 | my $key = $key[$key_i]; | |||||||||
| 43 | |||||||||||
| 44 | # warn "KEY: $key"; | ||||||||||
| 45 | 10 | $flat_rec->{$field->[$depth]} = $key; | |||||||||
| 46 | # warn "(depth $depth) flat_rec->{$field->[$depth]} = $key"; | ||||||||||
| 47 | |||||||||||
| 48 | 10 | Data::Hash::Flatten->this($href->{$key}, $field, $depth+1, $flat_rec, @flattened); | |||||||||
| 49 | } | ||||||||||
| 50 | } else { | ||||||||||
| 51 | |||||||||||
| 52 | 6 | $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 | ▶ | use Storable qw(dclone); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 56 | |||||||||||
| 57 | 6 | my $new_rec = dclone $flat_rec; | |||||||||
| 58 | 6 | push @flattened, $new_rec; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 11 | @flattened; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 1 | 1; | |||||||||
| 65 | __END__ | ||||||||||