I'm writing some module code in which I get data that I want to pass to the end-developer that is from a standard function that returns a list of items in a predefined order, much like how localtime works. Thus, for the end-developer that knows the standard format for this data, they'll probably want to use it as an array format. However, for the inexperienced developers, I'd also like to have that same data available as a hash with understandable names to make it easier for them to grab what data they need.

In otherwords, I'd want the following code to be valid:

my $obj = do_stuff_from_my_module(); my $name = $obj->[2]; # Assume name is the 3rd element # # OR!!! # my $name = $obj->{ name };
Now, if $obj is just a hash, the hash reference will work, but the array reference will give me problems. Of course, just using an array loses the meanful names.

If you look at a module like Net::Servent which trying to offer a similar interface, the names that I'd be using in my hash keys are now moved to method names, something I'm not thrilled about doing. I'd rather have the hash/array to be 'consistent' with the fact that this is just a data structure with no methods associated with it.

One thing that should be pointed out is that none of the names for the name will be solely numbers; this could be useful in distinguishing if the developer wants the array or the hash reference.

Is it possible to create a variable tied as both an array and a hash at the same time? Or is there some other approach one could use for this? Or is this an impossible task in perl?

-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important


In reply to 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.