| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package DataStruct::Flat; |
|
2
|
2
|
|
|
2
|
|
69658
|
use Moo; |
|
|
2
|
|
|
|
|
24627
|
|
|
|
2
|
|
|
|
|
12
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
sub flatten { |
|
6
|
2
|
|
|
2
|
1
|
1071
|
my ($self, $struct) = @_; |
|
7
|
|
|
|
|
|
|
|
|
8
|
2
|
|
|
|
|
4
|
my $result = {}; |
|
9
|
2
|
|
|
|
|
6
|
_flatten($struct, undef, $result); |
|
10
|
2
|
|
|
|
|
10
|
return $result; |
|
11
|
|
|
|
|
|
|
} |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub _flatten { |
|
14
|
15
|
|
|
15
|
|
33
|
my ($struct, $prefix, $result) = @_; |
|
15
|
|
|
|
|
|
|
|
|
16
|
15
|
100
|
|
|
|
37
|
if (ref($struct) eq 'HASH') { |
|
|
|
100
|
|
|
|
|
|
|
17
|
4
|
|
|
|
|
12
|
foreach my $key (keys %$struct) { |
|
18
|
9
|
100
|
|
|
|
20
|
my $local_prefix = (defined $prefix) ? "$prefix." : ""; |
|
19
|
9
|
|
|
|
|
13
|
my $key_in_result = $key; |
|
20
|
9
|
|
|
|
|
23
|
$key_in_result =~ s/\./\\./g; |
|
21
|
9
|
|
|
|
|
34
|
_flatten($struct->{ $key }, "$local_prefix$key_in_result", $result); |
|
22
|
|
|
|
|
|
|
} |
|
23
|
|
|
|
|
|
|
} elsif (ref($struct) eq 'ARRAY') { |
|
24
|
2
|
|
|
|
|
3
|
my $i = 0; |
|
25
|
2
|
|
|
|
|
5
|
foreach my $element (@$struct) { |
|
26
|
4
|
50
|
|
|
|
9
|
my $local_prefix = (defined $prefix) ? "$prefix." : ""; |
|
27
|
4
|
|
|
|
|
11
|
_flatten($element, "$local_prefix$i", $result); |
|
28
|
4
|
|
|
|
|
9
|
$i++; |
|
29
|
|
|
|
|
|
|
} |
|
30
|
|
|
|
|
|
|
} else { |
|
31
|
9
|
50
|
|
|
|
19
|
my $local_prefix = (defined $prefix) ? $prefix : ''; |
|
32
|
9
|
|
|
|
|
25
|
$result->{ $prefix } = $struct; |
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
} |
|
35
|
|
|
|
|
|
|
1; |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
#################### main pod documentation begin ################### |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head1 NAME |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
DataStruct::Flat - Convert a data structure into a one level list of keys and values |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
use DataStruct::Flat; |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
my $flattener = DataStruct::Flat->new; |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my $flat = $flattener->flatten({ |
|
50
|
|
|
|
|
|
|
a => [ 7, 8, 9, 10 ], |
|
51
|
|
|
|
|
|
|
b => { c => d }, |
|
52
|
|
|
|
|
|
|
}); |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# $flat = { |
|
55
|
|
|
|
|
|
|
# 'a.0' => 7, |
|
56
|
|
|
|
|
|
|
# 'a.1' => 8, |
|
57
|
|
|
|
|
|
|
# 'a.2' => 9, |
|
58
|
|
|
|
|
|
|
# 'a.3' => 10, |
|
59
|
|
|
|
|
|
|
# 'b.c' => 'd' |
|
60
|
|
|
|
|
|
|
# }; |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
This module converts a nested Perl data structure into a one level hash of keys and values |
|
65
|
|
|
|
|
|
|
apt for human consumption. |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head1 METHODS |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head2 new |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Constructor. Initializes the flattener object |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head2 flatten($struct) |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Returns a hashref for $struct which contains keys with dotted "paths" to the respective |
|
76
|
|
|
|
|
|
|
values in the datastructure. |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 CONTRIBUTE |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
The source code is located here: https://github.com/pplu/datastruct-flat |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head2 SEE ALSO |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
L |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 AUTHOR |
|
87
|
|
|
|
|
|
|
Jose Luis Martinez |
|
88
|
|
|
|
|
|
|
CPAN ID: JLMARTIN |
|
89
|
|
|
|
|
|
|
CAPSiDE |
|
90
|
|
|
|
|
|
|
jlmartinez@capside.com |
|
91
|
|
|
|
|
|
|
http://www.pplusdomain.net |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 COPYRIGHT |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Copyright (c) 2019 by CAPSiDE |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 LICENSE |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Apache 2.0 |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=cut |