I haven't found a heck of a lot of documentation about pseudo-hashes outside of Damian Conway's "Object Oriented Perl" book. For those unfamiliar with a pseudo-hash, it's an new feature as of Perl 5.005. According to Programming Perl, third edition, a pseudo-hash is any array reference whose first field is a hash reference. This hash reference should be a mapping of hash keys and array elements for the subsequent elements in the array reference. Confused yet? Here's some code:
use warnings; use strict; my $ph = [ { alpha => 1, beta => 2, gamma => 3, delta => 4, epsilon => + 5 }, #hashref "val a", "val b", "val c", "val d", "val e" ]; + # maps to these values # Print some of the elements print $ph->[1], "\n"; print $ph->{ alpha }, "\n"; print $ph->{ epsilon }, "\n"; # Let's add an element # Note the amount of work involved (though this can be done on two lin +es) # We can't autovivify an entry!! $ph->[ $ph->[ 0 ]->{ omega } = @{ $ph } ] = "The real last val"; print $ph->{ omega };
Now we can use an array as either a hash or array, supposedly getting the best of both worlds. Of course, this really needs both a hash lookup and an array lookup and thus has extra overhead unless we use the fields module. And, of course, deleting keys is problematic, and the syntax is confusing, and...

This leads me to wonder... what problem do pseudo-hashes solve? I know there's the annoyance with a regular hash autovivifying hash entries. Pseudo-hashes don't allow for autovivification of entries and can prevent subtle logic problems. But is this the only benefit? And is it potent enough to justify the extra work involved with creating one?

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to Are pseudo-hashes worth the effort? by Ovid

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.