Problem using defined constants as hash keys

B

bkimelman

I am using perl version 5.8.6 on a Windows XP PC.

I am having a problem using defined constants as hash keys.

Here is an examaple to demonstrate the problem

====================================

use strict;
use constant;
use Class::Struct;
use Data::Dumper;

use constant OPT_BOOLEAN => 'b';
use constant OPT_STRING => 's';
use constant OPT_INTEGER => 'i';
use constant OPT_NOPARM => '-';

my ( %types );

%types = ( OPT_BOOLEAN => "boolean flag" , OPT_STRING => "string" ,
OPT_INTEGER => "integer" , OPT_NOPARM => "" ) ;

print Dumper(\%types),"\n";

exit 0;

==============================

When I run the above perl script the output from the print statement
shows the "names" of the defined constants as the values of the hash
keys. Can you get it to use the value of the defined constants as the
values of the hash keys ?

Currently I am forced into doing this until I c an solve this problem :

@parm_types = ( OPT_BOOLEAN , OPT_STRING , OPT_INTEGER , OPT_NOPARM );
@parm_type_notes = ( "boolean flag" , "string" , "integer" , "" );
%types = map { $parm_types[$_] , $parm_type_notes[$_] } ( 0 ..
$#parm_types);
 
G

Gunnar Hjalmarsson

I am having a problem using defined constants as hash keys.

Here is an examaple to demonstrate the problem

====================================

use strict;
use constant;
use Class::Struct;
use Data::Dumper;

use constant OPT_BOOLEAN => 'b';
use constant OPT_STRING => 's';
use constant OPT_INTEGER => 'i';
use constant OPT_NOPARM => '-';

my ( %types );

%types = ( OPT_BOOLEAN => "boolean flag" , OPT_STRING => "string" ,
OPT_INTEGER => "integer" , OPT_NOPARM => "" ) ;

print Dumper(\%types),"\n";

exit 0;

==============================

When I run the above perl script the output from the print statement
shows the "names" of the defined constants as the values of the hash
keys. Can you get it to use the value of the defined constants as the
values of the hash keys ?

Yes, you can do

%types = ( OPT_BOOLEAN() => "boolean flag", ...
--------------------------^^

See the "Bugs" section in "perldoc constant".
 
T

Tad McClellan

I am having a problem using defined constants as hash keys.
use constant OPT_BOOLEAN => 'b';
use constant OPT_STRING => 's';
%types = ( OPT_BOOLEAN => "boolean flag" , OPT_STRING => "string" ,

When I run the above perl script the output from the print statement
shows the "names" of the defined constants as the values of the hash
keys.


Because => is auto-quoting the constant's names.

Can you get it to use the value of the defined constants as the
values of the hash keys ?


Sure, you can either write it so that auto-quoting won't quote:

%types = ( OPT_BOOLEAN() => "boolean flag", OPT_STRING() => "string",

or don't use the operator that does auto-quoting:

%types = ( OPT_BOOLEAN, "boolean flag", OPT_STRING, "string",
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top