In a project of mine, I plan on making a "work list" of tuples available to a callback before the work is actually performed. This is naturally kept as a hash, so they can be looked-up by the left part, and still easy enough to process the whole list with each or keys.

However, I wonder if a derived class might want to know the order in which they were added, some day.

Originally I figured that the access method to get the worklist, used strictly internally also for all access to it, could be overridden to return a reference to a tied hash that also keeps an order. It turns out that this actually is a little more complex, because I would also need to provide a method to reset the list. So it's not as cute as I thought.

So I'm thinking why not just provide the order already? I could use such an ordered hash class from the getgo, but I don't see one as a standard module and I want to use only modules that come with the normal Perl installation. Also there might be a performance hit with that.

Or, I can keep a list for the order as well as the hash for the index, explicitly. So, how do I provide access to that?

Storing the ordering as a specially-named member of the hash seems awkward, and less than elegant from a module interface point of view.

Two separate named methods would be clear.

Having multiple return values from a single method would be neater, but would that be awkward too?

I'm open to suggestions and design musings.

—John


In reply to Hashes made to order by John M. Dlugosz

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.