Lya has asked for the wisdom of the Perl Monks concerning the following question:

I tried the following:
use constant { NULL => 0, EINS => 1, ZWEI => 2, }; my %english_translation = ( NULL => "Zero", EINS => "One", ZWEI => "Two", ); foreach ( keys %english_translation ) { print "$_ = $english_translation{$_}\n"; }
The output is:
EINS = One NULL = Zero ZWEI = Two
But I want to have something like:
0 = Zero 1 = One 2 = Three
Why aren't the constants substituted in the Hash-Definition or how can I use constants that are doing what I want?

Thanks, Barbara

Replies are listed 'Best First'.
Re: Module constant and constants in Hashes
by Corion (Patriarch) on Jan 14, 2009 at 14:43 UTC

    The "fat comma" => stringifies its left-hand argument. So Perl sees your code as:

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

    To avoid that, you could change the fat comma to a normal comma.

      Or append parens so it's explicitly a sub call and not seen as a bareword (e.g. ... NULL() => "Zero", ...). Or look into the Readonly module and use $NULL.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

      Now I tried the following:
      my %english_translation = ( NULL => "Zero", 'EINS' => "One", ZWEI() => 'Two', );
      And I got:
      EINS = One NULL = Zero 2 = Two
      I think, I did not understood the explanation with the "fat comma" and the "normal comma", but the parentheses help.

      Thank you Barbara

        => is sometimes called a "fat comma". A normal comma is , ... Try it.

        use constant { NULL => 0, EINS => 1, ZWEI => 2, }; my %english_translation = ( NULL, "Zero", EINS, "One", ZWEI, "Two", ); foreach ( keys %english_translation ) { print "$_ = $english_translation{$_}\n"; }

        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.

Re: Module constant and constants in Hashes
by borisz (Canon) on Jan 14, 2009 at 14:49 UTC
    another option is:
    my %english_translation = ( NULL() => "Zero", EINS() => "One", ZWEI() => "Two", );
    Boris
Re: Module constant and constants in Hashes
by zentara (Cardinal) on Jan 14, 2009 at 14:51 UTC
    I'm kind of confused here, but the perldoc for constant, says " Constants defined using this module cannot be interpolated into strings like variables.".

    Why use constant? Just use a translation hash.


    I'm not really a human, but I play one on earth Remember How Lucky You Are
      What is a "translation hash"?

      Lya

        #!/usr/bin/perl use warnings; use strict; #But I want to have something like: #0 = Zero #1 = One #2 = Three my %hash = ( 'NULL' => "Zero", 'EINS' => "One", 'ZWEI' => "Two", ); my %trans_hash =( 'NULL' => 0, 'EINS' => 1, 'ZWEI' => 2, ); foreach (sort keys %hash ) { print "$trans_hash{$_} = $hash{$_}\n"; }

        I'm not really a human, but I play one on earth Remember How Lucky You Are