| 1 |
NAME |
| 2 |
Hash::Merge::Simple - Recursively merge two or more hashes, simply |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use Hash::Merge::Simple qw/ merge /; |
| 6 |
|
| 7 |
my $a = { a => 1 }; |
| 8 |
my $b = { a => 100, b => 2}; |
| 9 |
|
| 10 |
# Merge with righthand hash taking precedence |
| 11 |
my $c = merge $a, $b; |
| 12 |
# $c is { a => 100, b => 2 } ... Note: a => 100 has overridden => 1 |
| 13 |
|
| 14 |
# Also, merge will take care to recursively merge any subordinate hashes found |
| 15 |
my $a = { a => 1, c => 3, d => { i => 2 }, r => {} }; |
| 16 |
my $b = { b => 2, a => 100, d => { l => 4 } }; |
| 17 |
my $c = merge $a, $b; |
| 18 |
# $c is { a => 100, b => 2, c => 3, d => { i => 2, l => 4 }, r => {} } |
| 19 |
|
| 20 |
# You can also merge more than two hashes at the same time |
| 21 |
# The precedence increases from left to right (the rightmost has the most precedence) |
| 22 |
my $everything = merge $this, $that, $mine, $yours, $kitchen_sink, ...; |
| 23 |
|
| 24 |
DESCRIPTION |
| 25 |
Hash::Merge::Simple will recursively merge two or more hashes and return |
| 26 |
the result as a new hash reference. The merge function will descend and |
| 27 |
merge hashes that exist under the same node in both the left and right |
| 28 |
hash, but doesn't attempt to combine arrays, objects, scalars, or |
| 29 |
anything else. The rightmost hash also takes precedence, replacing |
| 30 |
whatever was in the left hash if a conflict occurs. |
| 31 |
|
| 32 |
This code was pretty much taken straight from Catalyst::Utils, and |
| 33 |
modified to handle more than 2 hashes at the same time. |
| 34 |
|
| 35 |
USAGE |
| 36 |
Hash::Merge::Simple->merge( <hash1>, <hash2>, <hash3>, ..., <hashN> ) |
| 37 |
Hash::Merge::Simple::merge( <hash1>, <hash2>, <hash3>, ..., <hashN> ) |
| 38 |
Merge <hash1> through <hashN>, with the nth-most (rightmost) hash taking |
| 39 |
precedence. |
| 40 |
|
| 41 |
Returns a new hash reference representing the merge. |
| 42 |
|
| 43 |
NOTE: The code does not currently check for cycles, so infinite loops |
| 44 |
are possible: |
| 45 |
|
| 46 |
my $a = {}; |
| 47 |
$a->{b} = $a; |
| 48 |
merge $a, $a; |
| 49 |
|
| 50 |
NOTE: If you want to avoid giving/receiving side effects with the merged |
| 51 |
result, use "clone_merge" or "dclone_merge" An example of this problem |
| 52 |
(thanks Uri): |
| 53 |
|
| 54 |
my $left = { a => { b => 2 } } ; |
| 55 |
my $right = { c => 4 } ; |
| 56 |
|
| 57 |
my $result = merge( $left, $right ) ; |
| 58 |
|
| 59 |
$left->{a}{b} = 3 ; |
| 60 |
$left->{a}{d} = 5 ; |
| 61 |
|
| 62 |
# $result->{a}{b} == 3 ! |
| 63 |
# $result->{a}{d} == 5 ! |
| 64 |
|
| 65 |
Hash::Merge::Simple->clone_merge( <hash1>, <hash2>, <hash3>, ..., <hashN> ) |
| 66 |
Hash::Merge::Simple::clone_merge( <hash1>, <hash2>, <hash3>, ..., <hashN> ) |
| 67 |
Perform a merge, clone the merge, and return the result |
| 68 |
|
| 69 |
This is useful in cases where you need to ensure that the result can be |
| 70 |
tweaked without fear of giving/receiving any side effects |
| 71 |
|
| 72 |
This method will use Clone to do the cloning |
| 73 |
|
| 74 |
Hash::Merge::Simple->dclone_merge( <hash1>, <hash2>, <hash3>, ..., <hashN> ) |
| 75 |
Hash::Merge::Simple::dclone_merge( <hash1>, <hash2>, <hash3>, ..., <hashN> ) |
| 76 |
Perform a merge, clone the merge, and return the result |
| 77 |
|
| 78 |
This is useful in cases where you need to ensure that the result can be |
| 79 |
tweaked without fear of giving/receiving any side effects |
| 80 |
|
| 81 |
This method will use Storable ("dclone") to do the cloning |
| 82 |
|
| 83 |
SEE ALSO |
| 84 |
Hash::Merge |
| 85 |
|
| 86 |
Catalyst::Utils |
| 87 |
|
| 88 |
Clone |
| 89 |
|
| 90 |
Storable |
| 91 |
|
| 92 |
ACKNOWLEDGEMENTS |
| 93 |
This code was pretty much taken directly from Catalyst::Utils: |
| 94 |
|
| 95 |
Sebastian Riedel "[email protected]" |
| 96 |
|
| 97 |
Yuval Kogman "[email protected]" |
| 98 |
|
| 99 |
BUGS |
| 100 |
Please report any bugs or feature requests on the bugtracker website |
| 101 |
<https://rt.cpan.org/Public/Dist/Display.html?Name=Hash-Merge-Simple> or |
| 102 |
by email to [email protected] |
| 103 |
<mailto:[email protected]>. |
| 104 |
|
| 105 |
When submitting a bug or request, please include a test-file or a patch |
| 106 |
to an existing test-file that illustrates the bug or desired feature. |
| 107 |
|
| 108 |
AUTHOR |
| 109 |
Robert Krimen <[email protected]> |
| 110 |
|
| 111 |
CONTRIBUTOR |
| 112 |
Graham Knop <[email protected]> |
| 113 |
|
| 114 |
COPYRIGHT AND LICENSE |
| 115 |
This software is copyright (c) 2008 by Robert Krimen. |
| 116 |
|
| 117 |
This is free software; you can redistribute it and/or modify it under |
| 118 |
the same terms as the Perl 5 programming language system itself. |
| 119 |
|