I'm looking for some heavy magic here. I came up with this question while pursuing a very bad approach to a problem, but I wonder if it could be done.

Let's say that I have a reference. For the sake of argument, let's not (yet) talk about what it points to.
my $ref;

I'd like to be able to do things like this to it:
$ref->{key}; %$ref->{key}; %{$ref}->{key}; $$ref{$key};

All looks good so far, right? Looks, sounds, and tastes like a hash ref. Okay, I'd also like to be able to do stuff like this to it:
$ref->[0]; @$ref->[0]; @{$ref}->[0]; $$ref[0];

...and...
$$ref = 1; $$ref;

So, basically, I'm wondering if I could use any combination of OO, tied variables, symbol table entries, etc. to be able to do something like this:
my $ref = POLYMORPH->new( qw( key in some init values) ); $ref->{key}; # might return 'in' $ref->[0]; # might return 'key' $ref; # depends on implementation

I got as far as being able to tie a variable to a class (such as a subclass of Tie::Hash and/or Tie::Array). But, it seems that Perl already knows what kind of variable got tied, and dereferencing that variable as a particular type doesn't have any (obvious) hooks that I could manipulate.

I thought about mucking with some symbol table entries, but I'm not sure that would get me anywhere, since I'm not using named variables here -- I'm using a blessed reference returned from a constructor. That's pretty specific.

So, anybody out there want to take a stab at this? In case you got lost, the question is this:
How can I make a single reference that can be LEGALLY DEREFERENCED as a hash ref, array ref, AND a scalar ref, with the resulting operation tied to a class implementation?

Or, if anyone could point me at the piece of documentation that clearly states that this is patently impossible (and why), that would sate my curiosity just as well.

Thanks!
dpmott

In reply to I'm looking for some 'heavy magic' by dpmott

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.