in reply to Question related to the use of constants

What you intended to be constant names are getting parsed as literal strings.

use strict; use warnings; use constant CONST1=>10; use constant CONST2=>12; my %var=( CONST1=>'test1', CONST2=>'test2', ); use Data::Dumper; print Dumper \%var; __END__ $VAR1 = { 'CONST1' => 'test1', 'CONST2' => 'test2' };

Internally, constants are functions. You can force them to be recognized by using function syntax:

my %var=( CONST1()=>'test1', CONST2()=>'test2', ); print $var{CONST1()}."\n"; print $var{10}."\n"; use Data::Dumper; print Dumper \%var; __END__ test1 test1 $VAR1 = { '10' => 'test1', '12' => 'test2' };

Or, as I personally prefer:

my %var=( (CONST1)=>'test1', (CONST2)=>'test2', ); print $var{(CONST1)}."\n"; print $var{10}."\n";

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^2: Question related to the use of constants
by ikegami (Patriarch) on Aug 20, 2004 at 17:56 UTC

    danielcid, just to clarify the parent post a little,

    The '=>' operator treats barewords on the left hand side as a string, and CONST1 is a bareword. Changing

    CONST1 => 'test1'

    to any of the following will work:

    (CONST1) => 'test1' # No longer a bareword. CONST1() => 'test1' # constants are really special functions. CONST1, 'test1' # use "," instead of "=>". It's the same, # except it doesn't quote the LHS.