Hash::M...::Simple(3)
π NAME π
Hash::Merge::Simple β recursively merge two or more hashes, simply. like, actually simply. no cap. (this is the Hash::M...::Simple(3) page bestie, we love a concise queen π)
β¨ SYNOPSIS β¨
okay so first you gotta pull in the merge function:
use Hash::Merge::Simple qw/ merge /;
my $a = { a => 1 };
my $b = { a => 100, b => 2};
# Merge with righthand hash taking precedence
my $c = merge $a, $b;
# $c is { a => 100, b => 2 } ... Note: a => 100 has overridden => 1
# Also, merge will take care to recursively merge any subordinate hashes found
my $a = { a => 1, c => 3, d => { i => 2 }, r => {} };
my $b = { b => 2, a => 100, d => { l => 4 } };
my $c = merge $a, $b;
# $c is { a => 100, b => 2, c => 3, d => { i => 2, l => 4 }, r => {} }
# You can also merge more than two hashes at the same time
# The precedence increases from left to right (the rightmost has the most precedence)
my $everything = merge $this, $that, $mine, $yours, $kitchen_sink, ...;
like yes bestie, the rightmost hash WINS. it's giving main character energy on the right side of the argument list π
π¦ DESCRIPTION π¦
Hash::Merge::Simple is gonna recursively merge two or more hashes and hand you back the result as a new hash reference. the merge function descends into hashes that exist under the same node in both the left and right hash and merges those too, but it does not attempt to combine arrays, objects, scalars, or literally anything else. the rightmost hash also takes precedence, replacing whatever was in the left hash if a conflict occurs. periodt. π₯
this code was pretty much taken straight from Catalyst::Utils, and then modified to handle more than 2 hashes at the same time. we love an upgrade β¨
πΈ USAGE πΈ
π Hash::Merge::Simple->merge( <hash1>, <hash2>, <hash3>, ..., <hashN> )
π Hash::Merge::Simple::merge( <hash1>, <hash2>, <hash3>, ..., <hashN> )
merge <hash1> through <hashN>, with the nth-most (rightmost) hash taking precedence. yep, the further right you go, the more it dominates. it's giving hierarchy π
returns a new hash reference representing the merge. obsessed.
NOTE: the code does not currently check for cycles, so infinite loops are possible. like, literally possible bestie, be careful:
my $a = {};
$a->{b} = $a;
merge $a, $a;
NOTE: if you want to avoid giving/receiving side effects with the merged result, useclone_mergeordclone_merge. here's an example of this problem (thanks Uri):
my $left = { a => { b => 2 } } ;
my $right = { c => 4 } ;
my $result = merge( $left, $right ) ;
$left->{a}{b} = 3 ;
$left->{a}{d} = 5 ;
# $result->{a}{b} == 3 !
# $result->{a}{d} == 5 !
see how $result got lowkey dragged into $left's drama? yeah. that's the side effect vibe. use the clone versions if you don't want that π
π§ Hash::Merge::Simple->clone_merge( <hash1>, <hash2>, <hash3>, ..., <hashN> )
π§ Hash::Merge::Simple::clone_merge( <hash1>, <hash2>, <hash3>, ..., <hashN> )
perform a merge, clone the merge, and return the result. slay.
this is useful in cases where you need to ensure that the result can be tweaked without fear of giving/receiving any side effects. like, your result can live its own life, no strings attached π«Ά
this method will use Clone to do the cloning. iconic.
π Hash::Merge::Simple->dclone_merge( <hash1>, <hash2>, <hash3>, ..., <hashN> )
π Hash::Merge::Simple::dclone_merge( <hash1>, <hash2>, <hash3>, ..., <hashN> )
perform a merge, clone the merge, and return the result. same energy, different tool.
this is useful in cases where you need to ensure that the result can be tweaked without fear of giving/receiving any side effects. literally the same warning as above, because boundaries matter π
this method will use Storable (dclone) to do the cloning. fancy.
π SEE ALSO π
Hash::MergeCatalyst::UtilsCloneStorable
go check 'em out, iykyk π
π ACKNOWLEDGEMENTS π
this code was pretty much taken directly from Catalyst::Utils:
- Sebastian Riedel
<sri@cpan.org> - Yuval Kogman
<nothingmuch@woobling.org>
giving credit where credit is due, we love to see it π
π BUGS π
please report any bugs or feature requests on the bugtracker website <https://rt.cpan.org/Public/Dist/Display.html?Name=Hash-Merge-Simple> or by email to bug-Hash-Merge-Simple@rt.cpan.org <mailto:bug-Hash-Merge-Simple@rt.cpan.org>.
when submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. like, help us help you bestie π
π§ AUTHOR π§
Robert Krimen <robertkrimen@gmail.com>
π CONTRIBUTOR π
Graham Knop <haarg@haarg.org>
πΈ COPYRIGHT AND LICENSE πΈ
this software is copyright (c) 2008 by Robert Krimen.
this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
okay that's the whole page bestie β¨ this was Hash::M...::Simple(3) from perl v5.42.0, dated 2024-10-16. now go merge those hashes and slay π
π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Hash-Merge-Simple 0.52.0-bp160.1.11 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms