1Hash::Merge::Simple(3)User Contributed Perl DocumentationHash::Merge::Simple(3)
2
3
4
6 Hash::Merge::Simple - Recursively merge two or more hashes, simply
7
9 version 0.051
10
12 use Hash::Merge::Simple qw/ merge /;
13
14 my $a = { a => 1 };
15 my $b = { a => 100, b => 2};
16
17 # Merge with righthand hash taking precedence
18 my $c = merge $a, $b;
19 # $c is { a => 100, b => 2 } ... Note: a => 100 has overridden => 1
20
21 # Also, merge will take care to recursively merge any subordinate hashes found
22 my $a = { a => 1, c => 3, d => { i => 2 }, r => {} };
23 my $b = { b => 2, a => 100, d => { l => 4 } };
24 my $c = merge $a, $b;
25 # $c is { a => 100, b => 2, c => 3, d => { i => 2, l => 4 }, r => {} }
26
27 # You can also merge more than two hashes at the same time
28 # The precedence increases from left to right (the rightmost has the most precedence)
29 my $everything = merge $this, $that, $mine, $yours, $kitchen_sink, ...;
30
32 Hash::Merge::Simple will recursively merge two or more hashes and
33 return the result as a new hash reference. The merge function will
34 descend and merge hashes that exist under the same node in both the
35 left and right hash, but doesn't attempt to combine arrays, objects,
36 scalars, or anything else. The rightmost hash also takes precedence,
37 replacing whatever was in the left hash if a conflict occurs.
38
39 This code was pretty much taken straight from Catalyst::Utils, and
40 modified to handle more than 2 hashes at the same time.
41
43 Hash::Merge::Simple->merge( <hash1>, <hash2>, <hash3>, ..., <hashN> )
44 Hash::Merge::Simple::merge( <hash1>, <hash2>, <hash3>, ..., <hashN> )
45 Merge <hash1> through <hashN>, with the nth-most (rightmost) hash
46 taking precedence.
47
48 Returns a new hash reference representing the merge.
49
50 NOTE: The code does not currently check for cycles, so infinite loops
51 are possible:
52
53 my $a = {};
54 $a->{b} = $a;
55 merge $a, $a;
56
57 NOTE: If you want to avoid giving/receiving side effects with the
58 merged result, use "clone_merge" or "dclone_merge" An example of this
59 problem (thanks Uri):
60
61 my $left = { a => { b => 2 } } ;
62 my $right = { c => 4 } ;
63
64 my $result = merge( $left, $right ) ;
65
66 $left->{a}{b} = 3 ;
67 $left->{a}{d} = 5 ;
68
69 # $result->{a}{b} == 3 !
70 # $result->{a}{d} == 5 !
71
72 Hash::Merge::Simple->clone_merge( <hash1>, <hash2>, <hash3>, ..., <hashN> )
73 Hash::Merge::Simple::clone_merge( <hash1>, <hash2>, <hash3>, ..., <hashN> )
74 Perform a merge, clone the merge, and return the result
75
76 This is useful in cases where you need to ensure that the result can be
77 tweaked without fear of giving/receiving any side effects
78
79 This method will use Clone to do the cloning
80
81 Hash::Merge::Simple->dclone_merge( <hash1>, <hash2>, <hash3>, ..., <hashN>
82 )
83 Hash::Merge::Simple::dclone_merge( <hash1>, <hash2>, <hash3>, ..., <hashN>
84 )
85 Perform a merge, clone the merge, and return the result
86
87 This is useful in cases where you need to ensure that the result can be
88 tweaked without fear of giving/receiving any side effects
89
90 This method will use Storable (dclone) to do the cloning
91
93 Hash::Merge
94
95 Catalyst::Utils
96
97 Clone
98
99 Storable
100
102 This code was pretty much taken directly from Catalyst::Utils:
103
104 Sebastian Riedel "sri@cpan.org"
105
106 Yuval Kogman "nothingmuch@woobling.org"
107
109 Robert Krimen <robertkrimen@gmail.com>
110
112 This software is copyright (c) 2010 by Robert Krimen.
113
114 This is free software; you can redistribute it and/or modify it under
115 the same terms as the Perl 5 programming language system itself.
116
117
118
119perl v5.36.0 2023-01-20 Hash::Merge::Simple(3)