From perlop:

The "=>" operator is a synonym for the comma, but forces any word (consisting entirely of word characters) to its left to be interpreted as a string (as of 5.001). This includes words that might otherwise be considered a constant or function call.

Constants are, in perl, implemented as subroutines which don't take an argument and return the constant's value. That means that

use constant { NULL => 0, EINS => 1, ZWEI => 2, };

has the same effect as saying

sub NULL { return 0; } sub EINS { return 1; } sub ZWEI { return 2; }

After either method (use constant LIST or setting up subroutines) you can use the sub denominating bare-words in your program, which will be replaced with the associated values by calling their corresponding subroutine.

But! if you construct your hash as you did,

my %english_translation = ( NULL => "Zero", 'EINS' => "One", ZWEI() => 'Two', );

only the key ZWEI will be resolved as a call of a function, because, as per the above snippt from perlfunc, the "=>" (i.e. "fat comma") operator forces any word (consisting entirely of word characters) to its left to be interpreted as a string, so saying EINS => 1 is exactly the same as saying 'EINS' => 1. The list operator "," (i.e. the normal comma) doesn't do that, so saying

my %english_translation = ( NULL, "Zero", EINS, "One", ZWEI, 'Two', );

will call the functions associated with NULL, EINS and ZWEI and interpolate their results.


In reply to Re^3: Module constant and constants in Hashes by shmem
in thread Module constant and constants in Hashes by Lya

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.