[Melbourne-pm] Hash Containing Array

Daniel Pittman daniel at rimspace.net
Sat Mar 21 19:12:21 PDT 2009

Gerd Berner <gberner at intraspect.com.au> writes:

> I'm having a few problems with some basic object oriented methods.

Mostly, casting and referencing of data structures; the same issue would
happen in a non-OO program.

> My constructor and set method works fine, but I'm struggling to read
> the keys back from the object once it is created.  My methods are
> attached below along with a screen dump of the created object.  Any
> help would be greatly appreciated.
>     sub new {
>     $self->{_ACC_VIA_RULE}= {};

Here you correctly set _ACC_VIA_RULE to a HASHREF.


>     sub set_acc_via_rule


>             unshift @{$self->{_ACC_VIA_RULE}{$key}},$val;

You want '$self->{_ACC_VIA_RULE}->{$key}'

Notice the extra '->' dereference?  That is there because a hash value
is a scalar, which means you can only store a HASHREF, not a HASH in it.


>     sub get_back_keys


>     while (my ($key,$value) = each ($self->{_ACC_VIA_RULE}))

Here you want:

    %{ $self->{_ACC_VIA_RULE} }

Notice the cast to a HASH?  That is needed to turn the HASHREF back into
a normal HASH, which each can operate on.


More information about the Melbourne-pm mailing list