This is really just the same thing as you do, but much briefer and IMHO a lot easier on the eyes:
sub option { my ($self, $key, $value) = @_; return defined $value ? $self->{options}->{$key} = $value : $self->{options}->{$key}; }
Merging hashes (or an array into a hash, which really is the same thing) unfortunately really is cumbersome in Perl since there's no built in way to do it. The %hash = (%hash, @array); method is agreeable, readability wise, but pretty inefficient - only use it in seldom used code and/or small sets of data. My solution:
@hash{ map $array[$_], grep !($_ & 1), 0 .. $#array } = map $array[$_] +, grep $_ & 1, 0 .. $#array;
The greps get a list of the array's indices and filter out the odd and the even ones, respectively, then the maps turn the index lists into values out of the array. The odd-index values set up a hash slice, the even-index values feed it. Of course this painful on both eyes and fingers. I would make a sub that I can safely stash it away:
sub merge_hash { my ($hash, $array) = @_; @$hash{ map $array->[$_], grep !($_ & 1), 0 .. $#{$array} } = map +$array->[$_], grep $_ & 1, 0 .. $#{$array}; }
This way I can just write down merge_hash(\%hash, \@array); and get an efficient and descriptive piece of code. You may even want to make it
sub merge_hash (\%\@) { ## rest unchanged }
so that you can simply write merge_hash(%hash, @array);. But beware, Perl will then insist on seeing a % and a @ even if you're passing a hash and an array reference - just like with the first parameter of push for example.

In reply to Re: setting and retrieving sub-hash elements in an object by Aristotle
in thread setting and retrieving sub-hash elements in an object by Amoe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.