in reply to How to validate %hash = ( 'a' => 'b', ...);
Of course, if you make the same stupid mistake twice (or any even number of times) in the same hash initialization, warnings won't help you:
Because Perl so transparently interconverts numeric and string values, if the hash only contains literals, it may be advantageous to use qw// to get rid of operators altogether:c:\@Work\Perl>perl -MData::Dump -le "use warnings; use strict; ;; my %hash = ( 'accents' => 'utf-8', 'accounting' => 'money', 'bank' => 'money'. 'ing' => 'money', 'foo' => 'bar'. 'perls' => 'perl', ); dd \%hash; " { accents => "utf-8", accounting => "money", bank => "moneying", barperls => "perl", money => "foo", }
Now there can be no confusion between the , and . operators. (Update: Of course, this trick only works for strings that have noc:\@Work\Perl>perl -MData::Dump -le "use warnings; use strict; ;; my %hash = qw( accents utf-8 accounting money bank money ing money foo 1 perls perl ); dd \%hash; " { accents => "utf-8", accounting => "money", bank => "money", foo => 1, ing => "money", perls => "perl", }
BTW: In case you should feel that my characterization of this mistake as "stupid" is a bit harsh, don't worry: I'm a member of the "Been There, Done That, Got The Scars To Prove It" club too.
Give a man a fish: <%-{-{-{-<
|
|---|