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 |