But this goes only one level deep. For "deeper" hash structures (like $object->{level1}{level2}=$value) this fails.
See example:
use strict;
use Data::Dumper;
my $x = { a => 1, b => 2, aa => { aaa => 11 } };
print Dumper($x);
my $y = { c => 3, d => 4, aa => { bbb => 11 } };
print Dumper($y);
$x = { %$x, %$y };
print Dumper($x);
Gives (see below) with "aaa" key in 'aa'=>'aaa'=>22 lost
$VAR1 = { 'c' => 3,
'a' => 1,
'b' => 2,
'd' => 4,
'aa' => {
'bbb' => 11
}
};
UPDATE: check this thread how to do it using Hash-Merge
http://www.perlmonks.org/?node_id=1015801
In a nut shell, add as appropriate:
use Hash-Dumper qw/merge/;
and modify
$x = { %$x, %$y };
to
$x = merge( $x, $y );
and resulting hash will look much better
$VAR1 = {
'c' => 3,
'a' => 1,
'b' => 2,
'd' => 4,
'aa' => {
'bbb' => 11,
'aaa' => 11
}
};
|