in reply to hash as an object property

It is traditional to use a hash as an object:

my $self = {}; bless $self, ...; $self->{thing} = "something"; $self->{catagory}{subcatagory} = "something";

See perltoot, perlboot, perlobj, and any other manpages dealing with objects (there are many).

____________________
Jeremy
I didn't believe in evil until I dated it.

Replies are listed 'Best First'.
Re: Re: hash as an object property
by dhammaBum (Acolyte) on Oct 02, 2001 at 14:06 UTC
    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 ....

      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