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:

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", }
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 = 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", }
Now there can be no confusion between the  , and  . operators. (Update: Of course, this trick only works for strings that have no embedded whitespace!)

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:  <%-{-{-{-<


In reply to Re: How to validate %hash = ( 'a' => 'b', ...); by AnomalousMonk
in thread How to validate %hash = ( 'a' => 'b', ...); by RCH

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.