line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Kephra::Config::Tree;
|
2
|
|
|
|
|
|
|
our $VERSION = '0.02';
|
3
|
|
|
|
|
|
|
=head1 NAME
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Kephra::Config::Tree - manipulation of config data
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=cut
|
10
|
2
|
|
|
2
|
|
29722
|
use strict;
|
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
69
|
|
11
|
2
|
|
|
2
|
|
10
|
use warnings;
|
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
3085
|
|
12
|
|
|
|
|
|
|
#
|
13
|
|
|
|
|
|
|
# single node manipulation
|
14
|
|
|
|
|
|
|
#
|
15
|
|
|
|
|
|
|
sub _convert_node_2_AoH {
|
16
|
0
|
|
|
0
|
|
0
|
my $node = shift;
|
17
|
0
|
0
|
|
|
|
0
|
if (ref $$node eq 'ARRAY') {
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
18
|
0
|
0
|
|
|
|
0
|
return $$node if ref $$node->[0] eq 'HASH';
|
19
|
|
|
|
|
|
|
} elsif (ref $$node eq 'HASH') {
|
20
|
0
|
|
|
|
|
0
|
my %temp_hash = %{$$node};
|
|
0
|
|
|
|
|
0
|
|
21
|
0
|
|
|
|
|
0
|
push( my @temp_array, \%temp_hash );
|
22
|
0
|
|
|
|
|
0
|
return $$node = \@temp_array;
|
23
|
|
|
|
|
|
|
} elsif (not ref $$node) {
|
24
|
0
|
|
|
|
|
0
|
my @temp_array = ();
|
25
|
0
|
|
|
|
|
0
|
return $$node = \@temp_array;
|
26
|
|
|
|
|
|
|
}
|
27
|
|
|
|
|
|
|
}
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _convert_node_2_AoS {
|
30
|
0
|
|
|
0
|
|
0
|
my $node = shift;
|
31
|
0
|
0
|
|
|
|
0
|
if (ref $$node eq 'ARRAY') {
|
|
|
0
|
|
|
|
|
|
32
|
0
|
|
|
|
|
0
|
return $$node;
|
33
|
|
|
|
|
|
|
} elsif ( 'SCALAR' eq ref $node ) {
|
34
|
0
|
0
|
|
|
|
0
|
if ($$node) {
|
35
|
0
|
|
|
|
|
0
|
push( my @temp_array, $$node );
|
36
|
0
|
|
|
|
|
0
|
return $$node = \@temp_array;
|
37
|
|
|
|
|
|
|
} else {
|
38
|
0
|
|
|
|
|
0
|
my @temp_array = ();
|
39
|
0
|
|
|
|
|
0
|
return $$node = \@temp_array;
|
40
|
|
|
|
|
|
|
}
|
41
|
|
|
|
|
|
|
}
|
42
|
|
|
|
|
|
|
}
|
43
|
|
|
|
|
|
|
#
|
44
|
|
|
|
|
|
|
# single node manipulation
|
45
|
|
|
|
|
|
|
#
|
46
|
0
|
|
|
0
|
0
|
0
|
sub get_subtree { &subtree }
|
47
|
|
|
|
|
|
|
sub subtree {
|
48
|
1
|
|
|
1
|
0
|
13
|
my $config = shift;
|
49
|
1
|
50
|
|
|
|
5
|
return unless ref $config;
|
50
|
1
|
|
|
|
|
2
|
my $path = shift;
|
51
|
1
|
|
|
|
|
5
|
for (split '/', $path) {
|
52
|
2
|
50
|
|
|
|
10
|
$config = $config->{$_} if defined $config->{$_};
|
53
|
|
|
|
|
|
|
}
|
54
|
1
|
|
|
|
|
4
|
return $config;
|
55
|
|
|
|
|
|
|
}
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub flat_keys {
|
58
|
0
|
|
|
0
|
0
|
0
|
my $config = shift;
|
59
|
0
|
0
|
|
|
|
0
|
return unless ref $config eq 'HASH';
|
60
|
0
|
|
|
|
|
0
|
my %flathash;
|
61
|
0
|
|
|
|
|
0
|
for ( keys %$config ){
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
}
|
64
|
|
|
|
|
|
|
}
|
65
|
|
|
|
|
|
|
#sub _parse_and_copy_node {
|
66
|
|
|
|
|
|
|
#my ($parent_node, $parent_id) = @_;
|
67
|
|
|
|
|
|
|
#no strict;
|
68
|
|
|
|
|
|
|
#for ( keys %$parent_node ){
|
69
|
|
|
|
|
|
|
#$cmd_id = $parent_id . $_;
|
70
|
|
|
|
|
|
|
#$leaf_type = ref $parent_node->{$_};
|
71
|
|
|
|
|
|
|
#if (not $leaf_type) {
|
72
|
|
|
|
|
|
|
#$list{$cmd_id}{$target_leafe} = $parent_node->{$_}
|
73
|
|
|
|
|
|
|
#if $parent_node->{$_};
|
74
|
|
|
|
|
|
|
#} elsif ($leaf_type eq 'HASH'){
|
75
|
|
|
|
|
|
|
#_parse_and_copy_node($parent_node->{$_}, $cmd_id . '-')
|
76
|
|
|
|
|
|
|
#}
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
#
|
79
|
|
|
|
|
|
|
# tree operations
|
80
|
|
|
|
|
|
|
#
|
81
|
|
|
|
|
|
|
my %copy = (
|
82
|
|
|
|
|
|
|
'' => sub { $_[0] },
|
83
|
|
|
|
|
|
|
SCALAR => sub { \${$_[0]} },
|
84
|
|
|
|
|
|
|
REF => sub { \copy( ${$_[0]} ) },
|
85
|
|
|
|
|
|
|
ARRAY => sub { [map {copy($_)} @{$_[0]} ] },
|
86
|
|
|
|
|
|
|
HASH => sub { my %copy = map { copy($_) } %{$_[0]}; \%copy; },
|
87
|
|
|
|
|
|
|
);
|
88
|
|
|
|
|
|
|
my %merge = (
|
89
|
|
|
|
|
|
|
'' => sub { $_[0] },
|
90
|
|
|
|
|
|
|
SCALAR => sub { \${$_[0]} },
|
91
|
|
|
|
|
|
|
REF => sub { \merge( ${$_[0]}, ${$_[1]} ) },
|
92
|
|
|
|
|
|
|
ARRAY => sub { [map { copy($_) } ( @{$_[0]}, @{$_[1]} ) ] },
|
93
|
|
|
|
|
|
|
HASH => sub {
|
94
|
|
|
|
|
|
|
my %copy = map
|
95
|
|
|
|
|
|
|
{ $_, merge( $_[0]{$_}, $_[1]{$_} ) }
|
96
|
|
|
|
|
|
|
(keys %{$_[0]}, keys %{$_[1]} );
|
97
|
|
|
|
|
|
|
\%copy;
|
98
|
|
|
|
|
|
|
},
|
99
|
|
|
|
|
|
|
);
|
100
|
|
|
|
|
|
|
my %update = (
|
101
|
|
|
|
|
|
|
'' => sub { $_[1] },
|
102
|
|
|
|
|
|
|
SCALAR => sub { \${$_[1]} },
|
103
|
|
|
|
|
|
|
REF => sub { \update( ${$_[0]}, ${$_[1]} ) },
|
104
|
|
|
|
|
|
|
ARRAY => sub { [map { copy($_) } ( @{$_[1]} ) ] },
|
105
|
|
|
|
|
|
|
HASH => sub {
|
106
|
|
|
|
|
|
|
my %copy = map {
|
107
|
|
|
|
|
|
|
$_, exists $_[1]{$_}
|
108
|
|
|
|
|
|
|
? update( $_[0]{$_}, $_[1]{$_} )
|
109
|
|
|
|
|
|
|
: copy( $_[0]{$_} )
|
110
|
|
|
|
|
|
|
} keys %{$_[0]} ;
|
111
|
|
|
|
|
|
|
\%copy;
|
112
|
|
|
|
|
|
|
},
|
113
|
|
|
|
|
|
|
);
|
114
|
|
|
|
|
|
|
my %diff = (
|
115
|
|
|
|
|
|
|
'' => sub { $_[0] ne $_[1] ? $_[0] : undef },
|
116
|
|
|
|
|
|
|
SCALAR => sub { ${$_[0]} ne ${$_[1]} ? \${$_[0]} : undef },
|
117
|
|
|
|
|
|
|
REF => sub {
|
118
|
|
|
|
|
|
|
my $diff = diff( ${$_[0]}, ${$_[1]} );
|
119
|
|
|
|
|
|
|
defined $diff ? \$diff : undef
|
120
|
|
|
|
|
|
|
},
|
121
|
|
|
|
|
|
|
ARRAY => sub { [map { copy($_) } @{$_[0]} ] },
|
122
|
|
|
|
|
|
|
HASH => sub {
|
123
|
|
|
|
|
|
|
my %diff;
|
124
|
|
|
|
|
|
|
for ( keys %{$_[0]} ) {
|
125
|
|
|
|
|
|
|
my $diff = exists $_[1]{$_}
|
126
|
|
|
|
|
|
|
? diff( $_[0]{$_}, $_[1]{$_} )
|
127
|
|
|
|
|
|
|
: copy( $_[0]{$_} )
|
128
|
|
|
|
|
|
|
;
|
129
|
|
|
|
|
|
|
$diff{$_} = $diff if defined $diff;
|
130
|
|
|
|
|
|
|
}
|
131
|
|
|
|
|
|
|
return scalar keys %diff > 0 ? \%diff : undef;
|
132
|
|
|
|
|
|
|
},
|
133
|
|
|
|
|
|
|
);
|
134
|
21
|
|
|
21
|
0
|
1187
|
sub copy { $copy{ ref $_[0] }( $_[0] ) }
|
135
|
|
|
|
|
|
|
sub merge {
|
136
|
7
|
|
|
7
|
0
|
16
|
my ($lref, $rref) = (ref $_[0], ref $_[1]);
|
137
|
7
|
100
|
|
|
|
29
|
$lref eq $rref
|
|
|
100
|
|
|
|
|
|
138
|
|
|
|
|
|
|
? $merge{ $lref }( $_[0], $_[1] )
|
139
|
|
|
|
|
|
|
: defined $_[0]
|
140
|
|
|
|
|
|
|
? $copy{ $lref }( $_[0] )
|
141
|
|
|
|
|
|
|
: $copy{ $rref }( $_[1] )
|
142
|
|
|
|
|
|
|
;
|
143
|
|
|
|
|
|
|
}
|
144
|
|
|
|
|
|
|
sub update { # left dictates the content, right the structure
|
145
|
4
|
|
|
4
|
0
|
1713
|
my ($lref, $rref) = (ref $_[0], ref $_[1]);
|
146
|
4
|
50
|
|
|
|
18
|
$lref eq $rref
|
147
|
|
|
|
|
|
|
? $update{ $lref }( $_[0], $_[1] )
|
148
|
|
|
|
|
|
|
: $copy{ $rref }( $_[0] )
|
149
|
|
|
|
|
|
|
;
|
150
|
|
|
|
|
|
|
}
|
151
|
|
|
|
|
|
|
sub diff {
|
152
|
9
|
|
|
9
|
0
|
927
|
my ($lref, $rref) = (ref $_[0], ref $_[1]);
|
153
|
9
|
100
|
|
|
|
30
|
$lref eq $rref
|
154
|
|
|
|
|
|
|
? $diff{ $lref }( $_[0], $_[1] )
|
155
|
|
|
|
|
|
|
: $copy{ $lref }( $_[0] ) # undef
|
156
|
|
|
|
|
|
|
;
|
157
|
|
|
|
|
|
|
}
|
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
1;
|