in reply to passing two hashs to a subroutine

Passing hash refs, and returning hash ref.

Let's assume, we want to implement such an operation, it takes each pair from the first hash, if the same key exists in the second hash, sum them up. For those keys exist in hash2, but not hash1, forget about them. Then what you can do is:

use Data::Dumper; use strict; my $h1 = {"a" => 1, "b" => 2}; my $h2 = {"c" => 1, "b" => 3}; my $ret = plus($h1, $h2); print Dumper($ret); sub plus { my $ret = {}; my ($hash1, $hash2) = @_; for (keys %$hash1) { if (exists($hash2->{$_})) { $ret->{$_} = $hash1->{$_} + $hash2->{$_}; } else { $ret->{$_} = $hash1->{$_}; } } return $ret; }