in reply to
Throw compilation error(or warning) if duplicate keys are present in hash
This will warn when run with or without `-c`:
sub no_dup_keys {
die "Odd number of elements" if @_ % 2;
my %h;
while ( my ( $key, $new_val ) = splice @_, 0, 2 ) {
if ( exists $h{$key} ) {
warn "Duplicate key: $key, old_value: $h{$key},"
. " new_value: $new_val\n ";
next; # Remove for rolling overlay.
}
$h{$key} = $new_val;
}
return %h;
}
my %hash;
BEGIN{
%hash = no_dup_keys(
"one" => "1",
"two" => "2",
"one" => "3",
);
}