Just another Perl shrine | |
PerlMonks |
setting and retrieving sub-hash elements in an objectby Amoe (Friar) |
on Dec 17, 2001 at 01:52 UTC ( [id://132400]=perlquestion: print w/replies, xml ) | Need Help?? |
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: 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: %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
Back to
Seekers of Perl Wisdom
|
|