in reply to Re: hash as an object property
in thread hash as an object property

Yes I can see it is quite common, but having looked carefully through the tutorials, I haven't been able to find where this specific task is demonstrated (how to use a hash as a property of an object and how to write a method to retrieve the key/value pairs). No doubt it is easy once you know how (and understand what is going on), but I'm buggered (as they say in Australia) if I can work it out ....

Replies are listed 'Best First'.
Re: Re: Re: hash as an object property
by pixel (Scribe) on Oct 02, 2001 at 16:46 UTC

    Here's a generic get/set access method for instance data that's stored in a hash in an object that's implemented as a blessed hash.

    It takes two mandatory arguments, the name of the data item that you're trying to access and the key in that hash that you want to get of set. If it's passed a third (optional) parameter then it uses that as the new value. It always returns the value.

    sub hash_accessor { my $self = shift; my ($data, $key) = (shift, shift); if (@ARGV) { $self->{$data}{$key} = shift; } return $self->{$data}{$key}; }

    Blessed Be
    The Pixel