Core module overload allows you to overload dereferencing. If you overload hash dereferencing only, you may have it. Something like this, perhaps:

{ package TwoFaceRefs; use overload '%{}' => \&getByKey; my %idx = qw(name 2); # And probably some more :-) sub getByKey { # Likely faster to return a tied hash, # but you get the idea ... my $obj = shift; return {map{ $_ => $obj->[$idx{$_}] } keys %idx}; } sub dsfmm { # Assuming we get an arrayref: my $obj = do_stuff_from_my_module(); bless $obj, "TwoFaceRefs"; return $obj; } } my $obj = TwoFaceRefs::dsfmm(); my $name = $obj->[2]; # # or # my $name = $obj->{name};

Update: Comment and (a little) whitespace added.

Update 2: Oh, unless we make getByKey return a (properly) tied hash, assigning to the fields won't work:
$obj->{name} = "Sidhekin";
The original problem merely stated to access the fields, not setting them, but I just wanted to make it clear that this is one problem overloading (on its own) will not solve. (There is no problem overloading and tying together cannot solve. Well, okay. A few.)

The Sidhekin


In reply to Re: Scalar ref acting as simulatenous hash and array? by Sidhekin
in thread Scalar ref acting as simulatenous hash and array? by Masem

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.