line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Overlay; |
2
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
225497
|
use 5.10.0; # for // |
|
5
|
|
|
|
|
24
|
|
|
5
|
|
|
|
|
320
|
|
4
|
5
|
|
|
5
|
|
31
|
use warnings; |
|
5
|
|
|
|
|
25
|
|
|
5
|
|
|
|
|
161
|
|
5
|
5
|
|
|
5
|
|
30
|
use strict; |
|
5
|
|
|
|
|
14
|
|
|
5
|
|
|
|
|
202
|
|
6
|
5
|
|
|
5
|
|
27
|
use Carp qw(cluck confess); |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
410
|
|
7
|
5
|
|
|
5
|
|
27
|
use Scalar::Util qw(reftype refaddr blessed); |
|
5
|
|
|
|
|
12
|
|
|
5
|
|
|
|
|
359
|
|
8
|
5
|
|
|
5
|
|
84
|
use List::Util qw(reduce max); |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
604
|
|
9
|
5
|
|
|
5
|
|
4873
|
use List::MoreUtils qw(part); |
|
5
|
|
|
|
|
7044
|
|
|
5
|
|
|
|
|
461
|
|
10
|
5
|
|
|
5
|
|
4481
|
use Sub::Name qw(subname); |
|
5
|
|
|
|
|
7790
|
|
|
5
|
|
|
|
|
488
|
|
11
|
5
|
|
|
5
|
|
44
|
use Exporter 'import'; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
15036
|
|
12
|
|
|
|
|
|
|
# Data::Dumper lazy loaded when debugging |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.54'; |
15
|
|
|
|
|
|
|
$VERSION = eval $VERSION; ## no critic |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our @EXPORT = qw(overlay); |
18
|
|
|
|
|
|
|
our @EXPORT_OK = qw(overlay overlay_all compose combine_with); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 NAME |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Data::Overlay - merge/overlay data with composable changes |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 VERSION |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
Data::Overlay version 0.54 - ALPHA, no compatibility promises, seriously |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 SYNOPSIS |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
#!perl -s |
31
|
|
|
|
|
|
|
#line 31 |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
use strict; use warnings; |
34
|
|
|
|
|
|
|
use Data::Overlay qw(overlay compose); |
35
|
|
|
|
|
|
|
use Data::Dumper; |
36
|
|
|
|
|
|
|
$Data::Dumper::Sortkeys = 1; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my $data_structure = { |
39
|
|
|
|
|
|
|
a => 123, |
40
|
|
|
|
|
|
|
b => { |
41
|
|
|
|
|
|
|
w => [ "some", "content" ], |
42
|
|
|
|
|
|
|
x => "hello", |
43
|
|
|
|
|
|
|
y => \"world", |
44
|
|
|
|
|
|
|
}, |
45
|
|
|
|
|
|
|
c => [ 4, 5, 6], |
46
|
|
|
|
|
|
|
d => { da => [], db => undef, dc => qr/abc/ }, |
47
|
|
|
|
|
|
|
}; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my %changes = ( |
50
|
|
|
|
|
|
|
f => 0, # add top level key |
51
|
|
|
|
|
|
|
a => '1, 2, 3', # overwrite key |
52
|
|
|
|
|
|
|
b => { z => '!' }, # nested operation |
53
|
|
|
|
|
|
|
c => { '=unshift' => 3.5 },# prepend array |
54
|
|
|
|
|
|
|
c => { '=push' => 7 }, # append array |
55
|
|
|
|
|
|
|
d => { da => [ "DA" ], # replace w/ differing type |
56
|
|
|
|
|
|
|
db => { |
57
|
|
|
|
|
|
|
'=defor' => 123, # only update if undef |
58
|
|
|
|
|
|
|
}, |
59
|
|
|
|
|
|
|
}, |
60
|
|
|
|
|
|
|
); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# apply %changes to $data_structure (read-only ok), |
63
|
|
|
|
|
|
|
# returning a new data structure sharing unchanged data with the old |
64
|
|
|
|
|
|
|
my $new_data_structure = overlay($data_structure, \%changes); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# Note sharing shown by Dumper |
67
|
|
|
|
|
|
|
print Dumper($data_structure, \%changes, $new_data_structure); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
__END__ |