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.04
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 merge
44 See below.
45
47 Hash::Merge::Simple->merge( <hash1>, <hash2>, <hash3>, ..., <hashN> )
48 Hash::Merge::Simple::merge( <hash1>, <hash2>, <hash3>, ..., <hashN> )
49 Merge <hash1> through <hashN>, with the nth-most (rightmost) hash
50 taking precedence.
51
52 Returns a new hash reference representing the merge.
53
54 NOTE: The code does not currently check for cycles, so infinite loops
55 are possible:
56
57 my $a = {};
58 $a->{b} = $a;
59 merge $a, $a;
60
61 NOTE: If you want to avoid giving/receiving side effects with the
62 merged result, use "clone_merge" or "dclone_merge" An example of this
63 problem (thanks Uri):
64
65 my $left = { a => { b => 2 } } ;
66 my $right = { c => 4 } ;
67
68 my $result = merge( $left, $right ) ;
69
70 $left->{a}{b} = 3 ;
71 $left->{a}{d} = 5 ;
72
73 # $result->{a}{b} == 3 !
74 # $result->{a}{d} == 5 !
75
76 Hash::Merge::Simple->clone_merge( <hash1>, <hash2>, <hash3>, ..., <hashN> )
77 Hash::Merge::Simple::clone_merge( <hash1>, <hash2>, <hash3>, ..., <hashN> )
78 Perform a merge, clone the merge, and return the result
79
80 This is useful in cases where you need to ensure that the result can be
81 tweaked without fear of giving/receiving any side effects
82
83 This method will use Clone to do the cloning
84
85 Hash::Merge::Simple->dclone_merge( <hash1>, <hash2>, <hash3>, ..., <hashN>
86 )
87 Hash::Merge::Simple::dclone_merge( <hash1>, <hash2>, <hash3>, ..., <hashN>
88 )
89 Perform a merge, clone the merge, and return the result
90
91 This is useful in cases where you need to ensure that the result can be
92 tweaked without fear of giving/receiving any side effects
93
94 This method will use Storable (dclone) to do the cloning
95
97 Robert Krimen, "<rkrimen at cpan.org>"
98
100 Hash::Merge, Catalyst::Utils
101
103 Please report any bugs or feature requests to "bug-hash-merge-simple at
104 rt.cpan.org", or through the web interface at
105 http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Hash-Merge-Simple
106 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Hash-Merge-Simple>. I
107 will be notified, and then you'll automatically be notified of progress
108 on your bug as I make changes.
109
111 You can find documentation for this module with the perldoc command.
112
113 perldoc Hash::Merge::Simple
114
115 You can also look for information at:
116
117 · RT: CPAN's request tracker
118
119 http://rt.cpan.org/NoAuth/Bugs.html?Dist=Hash-Merge-Simple
120 <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Hash-Merge-Simple>
121
122 · AnnoCPAN: Annotated CPAN documentation
123
124 http://annocpan.org/dist/Hash-Merge-Simple
125 <http://annocpan.org/dist/Hash-Merge-Simple>
126
127 · CPAN Ratings
128
129 http://cpanratings.perl.org/d/Hash-Merge-Simple
130 <http://cpanratings.perl.org/d/Hash-Merge-Simple>
131
132 · Search CPAN
133
134 http://search.cpan.org/dist/Hash-Merge-Simple
135 <http://search.cpan.org/dist/Hash-Merge-Simple>
136
138 This code was pretty much taken directly from Catalyst::Utils:
139
140 Sebastian Riedel "sri@cpan.org"
141
142 Yuval Kogman "nothingmuch@woobling.org"
143
145 Copyright 2008 Robert Krimen, all rights reserved.
146
147 This program is free software; you can redistribute it and/or modify it
148 under the same terms as Perl itself.
149
150
151
152perl v5.12.0 2010-05-02 Hash::Merge::Simple(3)