The problem is your use of the stringifying comma (=>). The parser sees it following a bareword, and assumes that you want the string value "WISCONSIN" rather than the return value of the subroutine &WISCONSIN().

There are various ways around this: you can change the fat comma to a regular comma, or you can change the bareword into something Perl recognizes as subroutine call (since that's what a constant is actually implemented as).

my %capitol_map = ( WISCONSIN , "Madison", ILLINOIS , "Springfield", ... );
or
my %capitol_map = ( &WISCONSIN => "Madison", ILLINOIS() => "Springfield", ... );

Update: it has been suggested that both of these have the potential to confuse future readers of your code, which I find a strong enough argument to recommend against using this particular construct. Use the skinny comma or dws's suggestion above, instead. </update>

I was going to suggest %map = ( +WISCONSIN => "Madison"), but that doesn't actually do the trick with my perl. It does, however, work for getting keys back out of the hash, should you ever need to:

print $capitol_map{+WISCONSIN};



If God had meant us to fly, he would *never* have given us the railroads.
    --Michael Flanders


In reply to Re: use constant for strings by ChemBoy
in thread use constant for strings by shemp

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.