in reply to merge two hashes into one

My solution, designed to take any number of hashes, not just two. Encapsulated in a function named add_hashes. Uses some functions in List::Util.

use List::Util qw( sum uniq ); # pass list of hashrefs. # returns a hash (list). sub add_hashes(@) { map { my $k = $_; $k => sum( map $_->{$k}, @_ ) } uniq( map keys(%$_), @_ ) }
Test it:
my %h1 = map { ( $_ => 1 ) } qw( a b c d e f g h ); my %h2 = map { ( $_ => 1 ) } qw( b c d e f g h i ); my %h3 = map { ( $_ => 1 ) } qw( c d e f g h i j ); my %h4 = map { ( $_ => 1 ) } qw( d e f g h i j k ); my %summed = add_hashes( \%h1, \%h2, \%h3, \%h4, ); use Data::Dumper; $Data::Dumper::Sortkeys=1; print Dumper \%summed;