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, use clone_merge or dclone_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::Merge
  • Catalyst::Utils
  • Clone
  • Storable

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>


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