Hash::Merge(3)
okay bestie, welcome to the Hash::Merge(3) page from the User Contributed Perl Documentation π this is the manual for a Perl module that merges arbitrarily deep hashes into a single hash, and we're gonna make it cute, clear, and fully detailed. no cap.
πΈ NAME πΈ
Hash::Merge β merges arbitrarily deep hashes into a single hash. that's the whole name, and honestly? iconic. π«Ά
β¨ SYNOPSIS β¨
okay so here's the whole vibe in code. this part is copied exactly, so don't come for me:
my %a = (
'foo' => 1,
'bar' => [qw( a b e )],
'querty' => { 'bob' => 'alice' },
);
my %b = (
'foo' => 2,
'bar' => [qw(c d)],
'querty' => { 'ted' => 'margeret' },
);
my %c = %{ merge( \%a, \%b ) };
Hash::Merge::set_behavior('RIGHT_PRECEDENT');
# This is the same as above
Hash::Merge::add_behavior_spec(
{ 'SCALAR' => {
'SCALAR' => sub { $_[1] },
'ARRAY' => sub { [ $_[0], @{ $_[1] } ] },
'HASH' => sub { $_[1] },
},
'ARRAY' => {
'SCALAR' => sub { $_[1] },
'ARRAY' => sub { [ @{ $_[0] }, @{ $_[1] } ] },
'HASH' => sub { $_[1] },
},
'HASH' => {
'SCALAR' => sub { $_[1] },
'ARRAY' => sub { [ values %{ $_[0] }, @{ $_[1] } ] },
'HASH' => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) },
},
},
'My Behavior',
);
# Also there is OO interface.
my $merger = Hash::Merge->new('LEFT_PRECEDENT');
my %c = %{ $merger->merge( \%a, \%b ) };
# All behavioral changes (e.g. $merge->set_behavior(...)), called on an object remain specific to that object
# The legacy "Global Setting" behavior is respected only when new called as a non-OO function.
# re-use globally specified behavior
my $merger = Hash::Merge->new();
$merger->add_behavior_spec(Hash::Merge::get_behavior_spec("My Behavior"), "My Behavior");
my %c = %{ $merger->merge( \%a, \%b ) };
# re-use externally specified behavior
use Hash::Merge::Extra ();
my $merger = Hash::Merge->new();
$merger->add_behavior_spec(Hash::Merge::Extra::L_REPLACE, "L_REPLACE");
my %c = %{ $merger->merge( \%a, \%b ) };
π DESCRIPTION π
Hash::Merge takes two arbitrarily deep hashes and smooshes them into one single hash π. at any level, it adds non-conflicting key-value pairs from one hash to the other, and it follows a set of specific rules when there are key value conflicts (all laid out below, don't worry). the hash is followed recursively, so deeply nested hashes that are at the same level will be merged when the parent hashes are merged.
β οΈ important caveat: self-referencing hashes, or recursive references, are not handled well by this method. like, it's giving "please don't" π.
Values in hashes are considered to be either ARRAY references, HASH references, or otherwise are treated as SCALARs. by default, the data passed to the merge function will be cloned using the Clone module; however, if necessary, this behavior can be changed to use as many of the original values as possible. (see set_clone_behavior below.)
Because there are a number of possible ways that one may want to merge values when keys are conflicting, Hash::Merge provides several preset methods for your convenience, as well as a way to define your own. these are (currently):
πΈ Left Precedence πΈ
this is the default behavior. periodt. β¨
The values buried in the left hash will never be lost; any values that can be added from the right hash will be attempted.
my $merge = Hash::Merge->new();
my $merge = Hash::Merge->new('LEFT_PRECEDENT');
$merge->set_behavior('LEFT_PRECEDENT');
Hash::Merge::set_behavior('LEFT_PRECEDENT');
πΈ Right Precedence πΈ
Same as Left Precedence, but with the right hash values never being lost. π
my $merge = Hash::Merge->new('RIGHT_PRECEDENT');
$merge->set_behavior('RIGHT_PRECEDENT');
Hash::Merge::set_behavior('RIGHT_PRECEDENT');
πΈ Storage Precedence πΈ
If conflicting keys have two different storage mediums, the 'bigger' medium will win; arrays are preferred over scalars, hashes over either. The other medium will try to be fitted in the other, but if this isn't possible, the data is dropped. π
my $merge = Hash::Merge->new('STORAGE_PRECEDENT');
$merge->set_behavior('STORAGE_PRECEDENT');
Hash::Merge::set_behavior('STORAGE_PRECEDENT');
πΈ Retainment Precedence πΈ
No data will be lost; scalars will be joined with arrays, and scalars and arrays will be 'hashified' to fit them into a hash. π§
my $merge = Hash::Merge->new('RETAINMENT_PRECEDENT');
$merge->set_behavior('RETAINMENT_PRECEDENT');
Hash::Merge::set_behavior('RETAINMENT_PRECEDENT');
Specific descriptions of how these work are detailed below. π
merge ( <hashref>, <hashref> )β merges two hashes given the rules specified. returns a reference to the new hash. β¨_hashify( <scalar>|<arrayref> )-- INTERNAL FUNCTION β returns a reference to a hash created from the scalar or array reference, where, for the scalar value, or each item in the array, there is a key and it's value equal to that specific value. Example: if you pass scalar'3', the hash will be{ 3 => 3 }. π§_merge_hashes( <hashref>, <hashref> )-- INTERNAL FUNCTION β actually does the key-by-key evaluation of two hashes and returns the new merged hash. note that this recursively calls"merge". πset_clone_behavior( <scalar> )β sets how the data cloning is handled byHash::Merge. if this is true, then data will be cloned; if false, then original data will be used whenever possible. by default, cloning is on (set to true). πget_clone_behavior( )β returns the current behavior for data cloning. πΈset_behavior( <scalar> )β specify which built-in behavior for merging that is desired. the scalar must be one of those given below. πget_behavior( )β returns the behavior that is currently in use byHash::Merge. βspecify_behavior( <hashref>, [<name>] ) [deprecated]β alias for"add_behavior_spec". π«Άadd_behavior_spec( <hashref>, [<name>] )β add a custom merge behavior spec forHash::Merge. this must be a hashref defined with (at least) 3 keys,SCALAR,ARRAY, andHASH; each of those keys must have another hashref with (at least) the same 3 keys defined. furthermore, the values in those hashes must be coderefs. these will be called with two arguments, the left and right values for the merge. your coderef should return either a scalar or an array or hash reference as per your planned behavior. if necessary, use the functions_hashifyand_merge_hashesas helper functions for these. for example, if you want to add the leftSCALARto the rightARRAY, you can have your behavior specification include:%spec = ( ...SCALAR => { ARRAY => sub { [ $_[0], @$_[1] ] }, ... } } );β note that you can import_hashifyand_merge_hashesinto your program's namespace with the'custom'tag. πget_behavior_spec( [<name>] )β return a previously defined merge behavior spec. if name isn't specified, the same default asadd_behavior_specis applied. if no such name is known referring to a behavior spec, nothing is returned. π
π§ BUILT-IN BEHAVIORS π§
Here is the specifics on how the current internal behaviors are called, and what each does. assume that the left value is given as $a, and the right as $b (these are either scalars or appropriate references):
LEFT TYPE RIGHT TYPE LEFT_PRECEDENT RIGHT_PRECEDENT
SCALAR SCALAR $a $b
SCALAR ARRAY $a ( $a, @$b )
SCALAR HASH $a %$b
ARRAY SCALAR ( @$a, $b ) $b
ARRAY ARRAY ( @$a, @$b ) ( @$a, @$b )
ARRAY HASH ( @$a, values %$b ) %$b
HASH SCALAR %$a $b
HASH ARRAY %$a ( values %$a, @$b )
HASH HASH merge( %$a, %$b ) merge( %$a, %$b )
LEFT TYPE RIGHT TYPE STORAGE_PRECEDENT RETAINMENT_PRECEDENT
SCALAR SCALAR $a ( $a ,$b )
SCALAR ARRAY ( $a, @$b ) ( $a, @$b )
SCALAR HASH %$b merge( hashify( $a ), %$b )
ARRAY SCALAR ( @$a, $b ) ( @$a, $b )
ARRAY ARRAY ( @$a, @$b ) ( @$a, @$b )
ARRAY HASH %$b merge( hashify( @$a ), %$b )
HASH SCALAR %$a merge( %$a, hashify( $b ) )
HASH ARRAY %$a merge( %$a, hashify( @$b ) )
HASH HASH merge( %$a, %$b ) merge( %$a, %$b )
(*) note that merge calls _merge_hashes, hashify calls _hashify. π¦
π AUTHOR π
this masterpiece was authored by Michael K. Neylon <mneylon-pm@masemware.com>, Daniel Muey <dmuey@cpan.org>, Jens Rehsack <rehsack@cpan.org>, and Stefan Hermes <hermes@cpan.org>. legends only. π
π COPYRIGHT π
Copyright (c) 2001,2002 Michael K. Neylon. All rights reserved.
Copyright (c) 2013-2020 Jens Rehsack. All rights reserved. Copyright (c) 2017-2020 Stefan Hermes. All rights reserved.
This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself. π
and that's the whole Hash::Merge(3) page, bestie π it's from perl v5.42.0, dated 2020-08-05, and the page itself is Hash::Merge(3). go merge some hashes responsibly, mwah ππ
π¦ where this page comes from
- opensuse-leap 16.0: perl-Hash-Merge 0.302-bp160.1.10 Β· 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