line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of Hash-Override |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# This software is copyright (c) 2012 by Stefan Petrea. |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# This is free software; you can redistribute it and/or modify it under |
7
|
|
|
|
|
|
|
# the same terms as the Perl 5 programming language system itself. |
8
|
|
|
|
|
|
|
# |
9
|
11
|
|
|
11
|
|
475558
|
use strict; |
|
11
|
|
|
|
|
30
|
|
|
11
|
|
|
|
|
448
|
|
10
|
11
|
|
|
11
|
|
57
|
use warnings; |
|
11
|
|
|
|
|
22
|
|
|
11
|
|
|
|
|
487
|
|
11
|
|
|
|
|
|
|
package Hash::Override; |
12
|
11
|
|
|
11
|
|
60
|
use feature ':5.10'; |
|
11
|
|
|
|
|
22
|
|
|
11
|
|
|
|
|
1477
|
|
13
|
11
|
|
|
11
|
|
63
|
use Carp qw/confess/; |
|
11
|
|
|
|
|
20
|
|
|
11
|
|
|
|
|
813
|
|
14
|
|
|
|
|
|
|
# ABSTRACT: Merges nested data structures. Useful if you need to merge default config files with user-defined settings. |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
#alias merge as merge_hash_override for exporting |
18
|
|
|
|
|
|
|
*merge_hash_override = \&merge; |
19
|
|
|
|
|
|
|
|
20
|
11
|
|
|
11
|
|
54
|
use Exporter 'import'; |
|
11
|
|
|
|
|
19
|
|
|
11
|
|
|
|
|
10332
|
|
21
|
|
|
|
|
|
|
our @EXPORT = qw/merge_hash_override/; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Prototypes and meaning for methods: |
24
|
|
|
|
|
|
|
# the functions merge_* take as parameters hashrefs and a common key |
25
|
|
|
|
|
|
|
# the functions ARRAY_merge_* take as parameters arrayrefs and an index |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub merge_SCALAR_SCALAR { |
28
|
27
|
|
|
27
|
0
|
48
|
my ($A,$B,$k) = @_; |
29
|
|
|
|
|
|
|
|
30
|
27
|
100
|
100
|
|
|
322
|
$A->{$k} = $B->{$k} |
31
|
|
|
|
|
|
|
if exists $A->{$k} && |
32
|
|
|
|
|
|
|
defined $B->{$k}; |
33
|
|
|
|
|
|
|
}; |
34
|
|
|
|
|
|
|
sub merge_SCALAR_HASH { |
35
|
0
|
|
|
0
|
0
|
0
|
my ($A,$B,$k) = @_; |
36
|
|
|
|
|
|
|
}; |
37
|
|
|
|
|
|
|
sub merge_SCALAR_ARRAY { |
38
|
1
|
|
|
1
|
0
|
2
|
my ($A,$B,$k) = @_; |
39
|
|
|
|
|
|
|
}; |
40
|
|
|
|
|
|
|
sub merge_HASH_SCALAR { |
41
|
1
|
|
|
1
|
0
|
3
|
my ($A,$B,$k) = @_; |
42
|
|
|
|
|
|
|
}; |
43
|
|
|
|
|
|
|
sub merge_HASH_HASH { |
44
|
3
|
|
|
3
|
0
|
6
|
my ($A,$B,$k) = @_; |
45
|
|
|
|
|
|
|
|
46
|
3
|
|
|
|
|
15
|
merge($A->{$k},$B->{$k}); |
47
|
|
|
|
|
|
|
}; |
48
|
|
|
|
|
|
|
sub merge_HASH_ARRAY { |
49
|
0
|
|
|
0
|
0
|
0
|
my ($A,$B,$k) = @_; |
50
|
|
|
|
|
|
|
}; |
51
|
|
|
|
|
|
|
sub merge_ARRAY_SCALAR { |
52
|
0
|
|
|
0
|
0
|
0
|
my ($A,$B,$k) = @_; |
53
|
|
|
|
|
|
|
}; |
54
|
|
|
|
|
|
|
sub merge_ARRAY_HASH { |
55
|
0
|
|
|
0
|
0
|
0
|
my ($A,$B,$k) = @_; |
56
|
|
|
|
|
|
|
}; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub ARRAY_merge_SCALAR_SCALAR { |
60
|
9
|
|
|
9
|
0
|
17
|
my ($A,$B,$k) = @_; |
61
|
|
|
|
|
|
|
|
62
|
9
|
|
|
|
|
22
|
$A->[$_] = $B->[$_]; |
63
|
|
|
|
|
|
|
}; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub ARRAY_merge_HASH_HASH { |
66
|
2
|
|
|
2
|
0
|
3
|
my ($A,$B,$k) = @_; |
67
|
2
|
|
|
|
|
11
|
merge($A->[$k],$B->[$k]); |
68
|
|
|
|
|
|
|
}; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub merge_ARRAY_ARRAY { |
72
|
4
|
|
|
4
|
0
|
11
|
my ($A,$B,$k) = @_; |
73
|
|
|
|
|
|
|
|
74
|
4
|
|
|
|
|
10
|
for(0..-1+@{$B->{$k}}) { |
|
4
|
|
|
|
|
29
|
|
75
|
11
|
|
|
|
|
24
|
my $rA = ref($A->{$k}->[$_]); |
76
|
11
|
|
|
|
|
31
|
my $rB = ref($B->{$k}->[$_]); |
77
|
|
|
|
|
|
|
|
78
|
11
|
100
|
66
|
|
|
71
|
ARRAY_merge_SCALAR_SCALAR($A->{$k},$B->{$k},$_) if $rA eq '' && $rB eq '' ; |
79
|
11
|
100
|
66
|
|
|
51
|
ARRAY_merge_HASH_HASH( $A->{$k},$B->{$k},$_) if $rA eq 'HASH' && $rB eq 'HASH' ; |
80
|
|
|
|
|
|
|
}; |
81
|
|
|
|
|
|
|
}; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub merge { |
85
|
16
|
|
|
16
|
0
|
327
|
my ($A,$B) = @_; |
86
|
16
|
50
|
33
|
|
|
127
|
confess 'both arguments to merge($,$) need to be hashrefs' |
87
|
|
|
|
|
|
|
unless |
88
|
|
|
|
|
|
|
ref $A eq 'HASH' && |
89
|
|
|
|
|
|
|
ref $B eq 'HASH' ; |
90
|
|
|
|
|
|
|
|
91
|
16
|
|
|
|
|
65
|
for my $key_B (keys %$B) { |
92
|
36
|
|
|
|
|
67
|
my $rA = ref($A->{$key_B}); |
93
|
36
|
|
|
|
|
56
|
my $rB = ref($B->{$key_B}); |
94
|
36
|
100
|
100
|
|
|
210
|
merge_SCALAR_SCALAR($A,$B,$key_B) if $rA eq '' && $rB eq '' ; |
95
|
36
|
50
|
66
|
|
|
148
|
merge_SCALAR_HASH( $A,$B,$key_B) if $rA eq '' && $rB eq 'HASH' ; |
96
|
36
|
100
|
100
|
|
|
141
|
merge_SCALAR_ARRAY( $A,$B,$key_B) if $rA eq '' && $rB eq 'ARRAY' ; |
97
|
36
|
100
|
100
|
|
|
136
|
merge_HASH_SCALAR( $A,$B,$key_B) if $rA eq 'HASH' && $rB eq '' ; |
98
|
36
|
100
|
100
|
|
|
127
|
merge_HASH_HASH( $A,$B,$key_B) if $rA eq 'HASH' && $rB eq 'HASH' ; |
99
|
36
|
50
|
66
|
|
|
113
|
merge_HASH_ARRAY( $A,$B,$key_B) if $rA eq 'HASH' && $rB eq 'ARRAY' ; |
100
|
36
|
50
|
66
|
|
|
155
|
merge_ARRAY_SCALAR( $A,$B,$key_B) if $rA eq 'ARRAY' && $rB eq '' ; |
101
|
36
|
50
|
66
|
|
|
153
|
merge_ARRAY_HASH( $A,$B,$key_B) if $rA eq 'ARRAY' && $rB eq 'HASH' ; |
102
|
36
|
100
|
66
|
|
|
158
|
merge_ARRAY_ARRAY( $A,$B,$key_B) if $rA eq 'ARRAY' && $rB eq 'ARRAY' ; |
103
|
|
|
|
|
|
|
}; |
104
|
|
|
|
|
|
|
}; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
1; |
109
|
|
|
|
|
|
|
__END__ |