Amoe has asked for the wisdom of the Perl Monks concerning the following question:
Hey monks.
I'm having a stab at writing my first OO module. All is going well, and I'm enjoying it, but I have stumbled upon a problem. The underlying hash of my object has a sub-hash, so to speak, called 'options'. (When I bless it, it's like this:)
The options hash has a lot of keys, so I thought I could write a subroutine to return an element. Here's my try:
sub option { shift->{options}->{shift()} }
All goes well, until I have to modify a value in the options hash. I'm sure there's a very simple way to do this. I tried this:
%hash = @array
and the pairs described by @array would be added to %hash, instead of wiping the hash and then adding them. This doesn't happen, and the only way I can think of accomplishing the same thing is to divide the array into two array based on modulo 2, to get the effective keys and values, then using a hash slice to assign them. Again, very cumbersome. I'm sure many OO modules have done this, but I searched for examples in vain. An example, preferably supporting changing of more than one element, would be great.
--
my one true love
bless {foo => 'bar', options => {anoption => 1, anotheroption => 'foo', option3 => 'barbar'}, bar => 'maz'}, $class;
The options hash has a lot of keys, so I thought I could write a subroutine to return an element. Here's my try:
sub option { shift->{options}->{shift()} }
All goes well, until I have to modify a value in the options hash. I'm sure there's a very simple way to do this. I tried this:
This works, but looks cumbersome. There has to be an easier way to do it. I think I need to be clearer on hash assignment in general, as well. I always thought that you say this:sub option { my $self = shift; if (scalar @_ == 1) { return $self->{options}->{shift()}; } else { my ($key, $value) = @_; $self->{options}->{$key} = $value; } }
%hash = @array
and the pairs described by @array would be added to %hash, instead of wiping the hash and then adding them. This doesn't happen, and the only way I can think of accomplishing the same thing is to divide the array into two array based on modulo 2, to get the effective keys and values, then using a hash slice to assign them. Again, very cumbersome. I'm sure many OO modules have done this, but I searched for examples in vain. An example, preferably supporting changing of more than one element, would be great.
--
my one true love
|
---|
Replies are listed 'Best First'. | |
---|---|
(jeffa) Re: setting and retrieving sub-hash elements in an object
by jeffa (Bishop) on Dec 17, 2001 at 02:03 UTC | |
Re: setting and retrieving sub-hash elements in an object
by Masem (Monsignor) on Dec 17, 2001 at 02:04 UTC | |
Re: setting and retrieving sub-hash elements in an object
by Aristotle (Chancellor) on Dec 17, 2001 at 04:29 UTC | |
Re: setting and retrieving sub-hash elements in an object
by Ven'Tatsu (Deacon) on Dec 17, 2001 at 02:55 UTC | |
Re: setting and retrieving sub-hash elements in an object
by clintp (Curate) on Dec 17, 2001 at 02:05 UTC | |
by tilly (Archbishop) on Dec 17, 2001 at 02:54 UTC |
Back to
Seekers of Perl Wisdom