rjh right for the money.
I have written too much code that does exactly what you are describing - the hash value is a ref array if that is not the case. Covers and pins of conditional type. Then one day he hit me: โWhy am I writing all this shit? Just use the ref array everywhere, dummy,โ I told myself. From that day, blue birds fly away from the trees to sing to me whenever I walk in the park.
push @{$hash{$key}}, $val;
That is all you need. If the key does not exist, the array is autogenerated.
If you do not like autoviv and want to be explicit:
$hash{$key} = [] unless exists $hash{$key}; push @{$hash{$key}}, $val;
Even this โdetailedโ approach is much shorter.
source share