in reply to Substr versus Regexp

You might get better performance with stringwise XOR (see perldoc perlop, search for Bitwise String Operators):
sub characters_in_common { my ($p, $q) = @_; my $r = $p ^ $q; my $count = $r =~ y/\0//; return $count; }
BTW, I don't think your function calculates the Hamming distance...