There is no way that I know of to tell Perl that assigning a different value to a hash key is an error. That is because that is defined in the language to be completely ok!
If you want checking like that, then the way to do it is to read the data like it came from an external file, build the hash and report errors as the data is processed.
There are many formulations of how to do this. I present one below.
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my $hash_data = <<END; #this is a "here is" string
one 1
two 2
one 3
END
my %seen;
my %hash =
map{
my ($text, $number) = split;
warn "\"$text\" has been re-defined!\n" if $seen{$text}++;
($text,$number);
}split (/\n/, $hash_data);
print Dumper \%hash;
__END__
Prints:
"one" has been re-defined!
$VAR1 = {
'one' => '3',
'two' => '2'
};
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.