Ovid has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Are pseudo-hashes worth the effort?
by autark (Friar) on Dec 13, 2000 at 05:54 UTC
    "Is pseudo-hashes usefull ?" - You are certainly not the first person to ask that question :-)

    I don't remember the exact reason why that feature was introduced. However, some of the pro opinions was:

    • pseudo-hashes has a smaller memory footprint than regular hashes.
    • Faster runtime access to fields, $pseudo->{foo} will compile to $pseudo->[15]
    • Compile time checking of object attributes ($pseudo->{errur})
    IIRC, it was once a consistency problem between pseudo-hashes and arrays that led to the delete and exists functions being able to take array elements as well: @a = (1,2,3); delete $a[1] Now, this "little" feature introduces something interesting. We can "delete" an array element - but what does that mean ?
    @a = (1,2,3); delete $a[1]; print "($_)\n" for @a;
    This would produce the following:
    (1) () (3)
    Aha! So delete $a[1] must be the same as $a[1] = undef then ? Let's check that statement:
    @a = (1,2,3); delete $a[1]; print "I'm ", exists $a[1] ? "here" : "gone"; @b = (1,2,3); $b[1] = undef; print "I'm ", exists $b[1] ? "here" : "gone";
    And indeed, this invalidates my previous statement. For the output is:
    I'm gone I'm here
    So delete $a[1] is not the same as $a[1] = undef. Hm, isn't that strange ? Well, the story is this, when we use undef we set the variable to the special value undef. When we use delete it does not set our variable to the special value undef, but to the special special value called uninitialized. Uninitialized means that the variable has not been set at all.

    This may seem strange but then again, pseudo-hashes and delete/exists on arrays is an experimental feature. (delete and exists on arrays was added in 5.6 IIRC).

    If you ask me it is a good reason pseudo-hashes is experimental, if you are not interested in my opinion, then maybe the opinion of our current pumpkin for the development track of perl (5.7) might interest you :-)

    Autark.

      exists() and delete() on arrays was Schwern's stupid idea. He must be severely punished.

Re (tilly) 1: Are pseudo-hashes worth the effort?
by tilly (Archbishop) on Dec 13, 2000 at 05:53 UTC
    They are not likely to be in Perl 6 because they ate chip alive on the first try at Perl 6.

    For the record I will miss them. To see why you would want to use them and some alternately solutions, take a look at RE: You can't get there from here.....

Re: Are pseudo-hashes worth the effort?
by Dominus (Parson) on Dec 14, 2000 at 09:36 UTC
    I think they're a huge stinker.

    About six months ago I was going to write a piece for www.perl.com about how pseudo-hashes had been a mistake. But I couldn't finish it because there was too much to say. Just when I thought the end was in sight (and it would still have been a long article) I mentioned it to Damian and he sent me a list of about six big problems I hadn't even thought of.

    In Perl 6, people will be able to plug in a new hash implementation that behaves like a pseudo-hash, if they want to, and then the rest of us won't have to suffer.