how to NOT quote constants in hash keys

M

Martin Adler

What can I "use" to make the following program work as I want it to?

#!/usr/bin/perl -w
use strict;
use constant N => 17 ;
my %x ;
$x{N} = 25 ; # who wants to write $x{(N)} = 25 all the time?
print keys %x ;

The output is "N", so Perl apparently interprets $x{N} as $x{"N"}.
But how can I tell it to interpret constants as their (constant)
value?
I want Perl to interpret $x{N} as $x{17}.

One of the goals of perl is "to make easy tasks easy and difficult
tasks possible". It seems to me that it is POSSIBLE to use constants
as hash keys (e.g.: $x{ (N) }), but it does not look EASY.

Or what else should I do?
 
T

Tore Aursand

#!/usr/bin/perl -w
use strict;
use constant N => 17 ;
my %x ;
$x{N} = 25 ; # who wants to write $x{(N)} = 25 all the time?
print keys %x ;

The output is "N", so Perl apparently interprets $x{N} as $x{"N"}.
But how can I tell it to interpret constants as their (constant)
value?
I want Perl to interpret $x{N} as $x{17}.

Why didn't you read the documentation before posting? It would have saved
you a lot of time;

perldoc constant
 
P

Paul Lalli

What can I "use" to make the following program work as I want it to?

#!/usr/bin/perl -w
use strict;
use constant N => 17 ;
my %x ;
$x{N} = 25 ; # who wants to write $x{(N)} = 25 all the time?
print keys %x ;

The output is "N", so Perl apparently interprets $x{N} as $x{"N"}.
But how can I tell it to interpret constants as their (constant)
value?
I want Perl to interpret $x{N} as $x{17}.

One of the goals of perl is "to make easy tasks easy and difficult
tasks possible". It seems to me that it is POSSIBLE to use constants
as hash keys (e.g.: $x{ (N) }), but it does not look EASY.

Or what else should I do?

Perl is trying to make something easy. It's just not what you want to be
easy in this case. Perl's auto-quoting is taking over here, which makes
it easy for the vast majority of cases, when we want $x{N} to mean
$x{'N'}, instead of the remarkably few cases when we want $x{N} to mean
$x{(WhateverValueConstantNRepresents)}.

You have three basic options:
write $x{N()} instead of $x{N}
write $x{+N} instead of $x{N}
use the ReadOnly module from CPAN instead of the constant pragma. This
defines constants that look like Perl variables instead of C Macros:

use ReadOnly $N => 17;
$x{$N} = 25;


Paul Lalli
 
P

Paul Lalli

use the ReadOnly module from CPAN instead of the constant pragma. This
defines constants that look like Perl variables instead of C Macros:

use ReadOnly $N => 17;
$x{$N} = 25;

I should clarify two things:
1) the above is not actually correct syntax.
Read the Readonly documentation for the correct syntax.

2) Readonly.pm comes with a significant performance hit over constant.pm.
Read teh Readonly documentation for details.

Paul Lalli
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top