line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::MuForm::Merge; |
2
|
|
|
|
|
|
|
# ABSTRACT: internal hash merging |
3
|
92
|
|
|
92
|
|
345
|
use warnings; |
|
92
|
|
|
|
|
195
|
|
|
92
|
|
|
|
|
2644
|
|
4
|
92
|
|
|
92
|
|
301
|
use Data::Clone ('data_clone'); |
|
92
|
|
|
|
|
122
|
|
|
92
|
|
|
|
|
3553
|
|
5
|
92
|
|
|
92
|
|
308
|
use base 'Exporter'; |
|
92
|
|
|
|
|
180
|
|
|
92
|
|
|
|
|
38303
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our @EXPORT_OK = ( 'merge' ); |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $matrix = { |
10
|
|
|
|
|
|
|
'SCALAR' => { |
11
|
|
|
|
|
|
|
'SCALAR' => sub { $_[0] }, |
12
|
|
|
|
|
|
|
'ARRAY' => sub { [ $_[0], @{ $_[1] } ] }, |
13
|
|
|
|
|
|
|
'HASH' => sub { $_[1] }, |
14
|
|
|
|
|
|
|
}, |
15
|
|
|
|
|
|
|
'ARRAY' => { |
16
|
|
|
|
|
|
|
'SCALAR' => sub { [ @{ $_[0] }, $_[1] ] }, |
17
|
|
|
|
|
|
|
'ARRAY' => sub { [ @{ $_[0] }, @{ $_[1] } ] }, |
18
|
|
|
|
|
|
|
'HASH' => sub { $_[1] }, |
19
|
|
|
|
|
|
|
}, |
20
|
|
|
|
|
|
|
'HASH' => { |
21
|
|
|
|
|
|
|
'SCALAR' => sub { $_[0] }, |
22
|
|
|
|
|
|
|
'ARRAY' => sub { $_[0] }, |
23
|
|
|
|
|
|
|
'HASH' => sub { merge_hashes( $_[0], $_[1] ) }, |
24
|
|
|
|
|
|
|
}, |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub merge { |
28
|
210
|
|
|
210
|
0
|
330
|
my ( $left, $right ) = @_; |
29
|
|
|
|
|
|
|
|
30
|
210
|
100
|
|
|
|
392
|
my $lefttype = |
|
|
100
|
|
|
|
|
|
31
|
|
|
|
|
|
|
ref $left eq 'HASH' ? 'HASH' : |
32
|
|
|
|
|
|
|
ref $left eq 'ARRAY' ? 'ARRAY' : |
33
|
|
|
|
|
|
|
'SCALAR'; |
34
|
210
|
100
|
|
|
|
373
|
my $righttype = |
|
|
100
|
|
|
|
|
|
35
|
|
|
|
|
|
|
ref $right eq 'HASH' ? 'HASH' : |
36
|
|
|
|
|
|
|
ref $right eq 'ARRAY' ? 'ARRAY' : |
37
|
|
|
|
|
|
|
'SCALAR'; |
38
|
210
|
|
|
|
|
764
|
$left = data_clone($left); |
39
|
210
|
|
|
|
|
1431
|
$right = data_clone($right); |
40
|
210
|
|
|
|
|
502
|
return $matrix->{$lefttype}{$righttype}->( $left, $right ); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub merge_hashes { |
44
|
193
|
|
|
193
|
0
|
174
|
my ( $left, $right ) = @_; |
45
|
193
|
|
|
|
|
147
|
my %newhash; |
46
|
193
|
|
|
|
|
359
|
foreach my $leftkey ( keys %$left ) { |
47
|
136
|
100
|
|
|
|
237
|
if ( exists $right->{$leftkey} ) { |
48
|
23
|
|
|
|
|
46
|
$newhash{$leftkey} = merge( $left->{$leftkey}, $right->{$leftkey} ); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
else { |
51
|
113
|
|
|
|
|
254
|
$newhash{$leftkey} = data_clone( $left->{$leftkey} ); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
193
|
|
|
|
|
460
|
foreach my $rightkey ( keys %$right ) { |
55
|
2196
|
100
|
|
|
|
2534
|
if ( !exists $left->{$rightkey} ) { |
56
|
2173
|
|
|
|
|
3543
|
$newhash{$rightkey} = data_clone( $right->{$rightkey} ); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
193
|
|
|
|
|
840
|
return \%newhash; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
1; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
__END__ |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=pod |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=encoding UTF-8 |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 NAME |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Data::MuForm::Merge - internal hash merging |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 VERSION |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
version 0.03 |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 AUTHOR |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Gerda Shank |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
This software is copyright (c) 2017 by Gerda Shank. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
87
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=cut |