use constant and hash

D

david

Hi all,

Is there an option to create a constant hash ?

I tried two snippets
This does not compile:
use strict;
use warnings;
my %hash =(1=>2);
use constant A=>%hash;
print A{1},"\n";
perl -w a.pl
syntax error at a.pl line 5, near "A{"
syntax error at a.pl line 6, near "A{"
Execution of a.pl aborted due to compilation errors.

and the following does not do the work:
use strict;
use warnings;
my %hash =(1=>2);
use constant A=>\%hash;
print A->{1},"\n";
print A->{2},"\n";
print "why do i print this ?\n"
perl -w a.pl
Use of uninitialized value in print at a.pl line 6.
2

why do i print this ?


I thought afterwards, that the second snippet has no chance to work.
The hash address remains constant even the values changes :-(.

Has anyone another idea ?
Thanks,
David
 
T

Tim McDaniel

Is there an option to create a constant hash ?

What exactly do you mean?
(1) a fixed set of keys -- that is, you can neither create nor delete
entries?
(2) fixed data -- that is, you can give a value to a given key only
once, but you can create and delete keys?
(3) both fixed keys and fixed data?
(4) Do you mean recursive constant -- if there's a hash in a value, si
it locked too?
(5) Do you want the ability to lock and unlock at will?

A CPAN search for
hash readonly
showed module Readonly. Damian Conway suggests its use in _Perl Best
Practices_, ch 4, in preference to "use constant".

I did a CPAN search for
hash locked
and one hit was Hash::Util, which allows various mixtures of those
possibilities. (Note that lock_keys isn't quite (1): it allows
deleting a key.) It's somewhat more flexible, but I prefer to know
that something is read-only forever.
 
D

david

What exactly do you mean?
(1) a fixed set of keys -- that is, you can neither create nor delete
    entries?
(2) fixed data -- that is, you can give a value to a given key only
    once, but you can create and delete keys?
(3) both fixed keys and fixed data?
(4) Do you mean recursive constant -- if there's a hash in a value, si
    it locked too?
(5) Do you want the ability to lock and unlock at will?

A CPAN search for
    hash readonly
showed module Readonly.  Damian Conway suggests its use in _Perl Best
Practices_, ch 4, in preference to "use constant".

I did a CPAN search for
    hash locked
and one hit was Hash::Util, which allows various mixtures of those
possibilities.  (Note that lock_keys isn't quite (1): it allows
deleting a key.)  It's somewhat more flexible, but I prefer to know
that something is read-only forever.

I would prefer option 4 but option 3 is also good. Readonly looks nice
but the last version is from 2004 (does not have to mean anything).
Hash::Util looks very interesting.

Thanks
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top