Hash questions

L

lroland

Hi all

I use a the OO capabilities of Perl to define a data structure using a
hash of hashes - this unfortunately implies that the resulting data
structure is modifiable (ideally only the values should be modifiable
not the structure of the hash (i.e. it should be impossible to add any
new keys)). So my questions goes:


1) Is it possible to define a data structure using a hash where only
the values of the keys is modifiable (i.e. much like a struct in C).
i.e I want to alter this function to somehow return a structure:

sub new
{
my ($this) = @_;
bless {
key1 => undef,
key2 => {
key2a => undef
}
}, $this;
}

where it it is not possible to do something like this:

my $test = new();
$test->{key2}->{key2b} = "24";

but this should be possible:

$test->{key2}->{key2a} = "42"

can this be done ?

2) Is there a generic way of traversing all the key/value pairs in a
hash of hashes (I want to create a function to check if all the values
in my "hash of hashes" are defined) - googling around seams to indicate
that using "keys" will only work on the outer level of a hash of hashes
which implies that you will need to nest calls to "keys" which will
require knowledge of the structure of the hash of hashes (recursive
calls could solve this however).
 
X

xhoster

Hi all

I use a the OO capabilities of Perl to define a data structure using a
hash of hashes - this unfortunately implies that the resulting data
structure is modifiable (ideally only the values should be modifiable
not the structure of the hash (i.e. it should be impossible to add any
new keys)). So my questions goes:

1) Is it possible to define a data structure using a hash where only
the values of the keys is modifiable (i.e. much like a struct in C).

Yes, but if you are into programming bondage and discipline, Perl
probably shouldn't be your language choice in the first place. Anyway,
you could implement your objects as closures, or as inside-out objects.
This would prevent all access to the object that doesn't go through the
objects methods, so the methods would enforce the non-modification of the
structure.

i.e I want to alter this function to somehow return a structure:

sub new
{
my ($this) = @_;
bless {
key1 => undef,
key2 => {
key2a => undef
}
}, $this;
}

where it it is not possible to do something like this:

my $test = new();
$test->{key2}->{key2b} = "24";

but this should be possible:

$test->{key2}->{key2a} = "42"

Why should that be possible? It isn't very OO. You could probably
somehow use tie to tie the hash to an object that implements this, but
I don't know how circumventable that is.

can this be done ?

2) Is there a generic way of traversing all the key/value pairs in a
hash of hashes (I want to create a function to check if all the values
in my "hash of hashes" are defined) - googling around seams to indicate
that using "keys" will only work on the outer level of a hash of hashes
which implies that you will need to nest calls to "keys" which will
require knowledge of the structure of the hash of hashes (recursive
calls could solve this however).

Yes, recursive calls are the generic way of doing that.

Xho
 
M

Mark Clements

Hi all

I use a the OO capabilities of Perl to define a data structure using a
hash of hashes - this unfortunately implies that the resulting data
structure is modifiable (ideally only the values should be modifiable
not the structure of the hash (i.e. it should be impossible to add any
new keys)). So my questions goes:


1) Is it possible to define a data structure using a hash where only
the values of the keys is modifiable (i.e. much like a struct in C).
i.e I want to alter this function to somehow return a structure:

sub new
{
my ($this) = @_;
bless {
key1 => undef,
key2 => {
key2a => undef
}
}, $this;
}

where it it is not possible to do something like this:

my $test = new();
$test->{key2}->{key2b} = "24";

but this should be possible:

$test->{key2}->{key2a} = "42"

can this be done ?

check out lock_keys in Hash::Util

Also, see

perldoc fields

Mark
 
A

Ala Qumsieh

Yes, recursive calls are the generic way of doing that.

I believe the OP is looking for something like:

for my $key1 (%hash) {
for my $key2 (%{$hash{$key1}}) {
# etc ...
}
}

--Ala
 
D

David Wall

I use a the OO capabilities of Perl to define a data structure
using a hash of hashes - this unfortunately implies that the
resulting data structure is modifiable (ideally only the values
should be modifiable not the structure of the hash (i.e. it
should be impossible to add any new keys)). So my questions
goes:


1) Is it possible to define a data structure using a hash where
only the values of the keys is modifiable (i.e. much like a
struct in C). i.e I want to alter this function to somehow
return a structure:

If you don't already have a copy of Damian Conway's book _Object
Oriented Perl_, it's highly recommended by many folks in the Perl
community. See http://books.perl.org/book/171

It seems there's a David Wall whose review of the book is at the link
above. I certainly don't remember writing that review. My name
isn't a very rare one, though. I just didn't know about this
instance of it.
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top