Subject: | Custom behaviours in 2.99 |
The following code used to work with Hash::Merge 2.00. Since 2.99 it dies with "Behavior 'My Behavior' does not exist". The code is mostly from the module synopsis.
use Hash::Merge;
use Modern::Perl;
my %a = (
'foo' => 1,
'bar' => [qw( a b e )],
'querty' => { 'bob' => 'alice' },
);
my %b = (
'foo' => 2,
'bar' => [qw(c d)],
'querty' => { 'ted' => 'margeret' },
);
# Set behaviour
my $behaviour = Hash::Merge::specify_behavior(
{ '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',
);
# set global behaviour to default
Hash::Merge::set_behavior('LEFT_PRECEDENT');
# Set local behaviour
my $merge = Hash::Merge->new('My Behavior');
my %c = %{ $merge->merge( \%a, \%b ) };