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).
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).