Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

RE: My views on pseudohashes

by extremely (Priest)
on Jun 28, 2000 at 02:05 UTC ( #20091=note: print w/ replies, xml ) Need Help??


in reply to My views on pseudohashes

Pseudo-hash == evil nightmare. You are just asking for trouble using them. The occasional benefit of looking up things in an ordered list using hash like names isn't worth the nightmare that maintaining the code becomes. After explaining that @var is the list and $var[] is the item in a list, explain to a new programmer that $var{} is ALSO an item in the list SOMETIMES but is usually an item in a vaguely related hash. You wind up telling people about 'glob's and symbol tables when they still aren't sure when $_ is able to be used. Bleagh, bleagh, bleagh.

--
friend: Can `perl -e "print 'Hello, World!';"` be simplified?
me: What? How? I guess you could make it less formal. Like "Hi, folks." or something. Gah.


Comment on RE: My views on pseudohashes
RE: My views on pseudohashes
by csorensen (Beadle) on Jun 28, 2000 at 02:18 UTC
    am I missing something .. aren't pseudo-hashes and anonymous arrays the same thing ?
      No they aren't. An anonymous array is an array without a formal name. Like:
      my $q = [ $a, $b, $c, $d];
      returns an anonymous array ref into $q, you created an array without ever declaring an named array like with `my'.

      A pseudo-hash is an array (anonymous or not) that has a special first entry. Pseudo-hashes are still listed in `perldoc perlref' as experimental. try this:

      my @ph; $ph[0]={Foo=>1, Bar=>2, Bag=>3}; $ph[1]="what Foo points to"; $ph[2]="what Bar points to"; $ph[3]="what Bag points to"; print "$ph{Foo} is $ph[1]\n";

      In effect making the first item in an array a hash where each key points to a row index lets you use the array like a hash. All you favorite hash tricks work, `keys', `values', `exists' and more. The real win here is that you have an intrisic order to the hash thanks to the array, but the hash tools won't naturally return in that order, you have to do it yourself. Also, you can't add a new field with a simple `$ph{New}="a new item"' cause it doesn't know how to do it.

      If you decide to play with em, PLEASE read the `perldoc perlref' section on it. There are some handy-dandy tools in perl 5.6 like `perldoc fields' that will add some compile time sanity and ease of use to them.

      HTH somebody. =)

      -- mark

RE: RE: My views on pseudohashes
by Adam (Vicar) on Jun 28, 2000 at 02:19 UTC
    Yes, writing maintainable code is very important, and it is not a task that perl makes easy, however it is not a reason to discard half the tools in the tool box. In the event that your code starts to get to difficult for some one new to understand, include a couple helpful comments, and if they still can't get it then maybe they aren't ready to work and that project yet and should be put to work on something simpler until they are ready. You can't expect a freshman year physics major to maintain a nuclear power plant.
RE: RE: My views on pseudohashes
by panache (Initiate) on Jun 30, 2000 at 00:46 UTC
    The real win in using pseudohashes is when creating and using objects. As an example, if I create an object based on a hash:
    package hashobject; sub new { my ($class) = @_; $class = ref($class) || $class; my $self = {}; $self->{variable} = 'foo'; return(bless($self, $class)); }
    and then use the resulting object, I can still autovivify member variables, leading to a whole raft of typo problems:
    my $obj = new hashobject; $obj->{variablee} = 'bar'; # TYPO... print $obj->{variable}; # Prints 'foo'
    If we move to a pseudohash, the object looks like this:
    package pseudohashobject; use fields qw(variable); sub new { my ($class) = @_; $class = ref($class) || $class; my $self = bless([\%FIELDS], $class]); $self->{variable} = 'foo'; return($self); }
    And then (mis)use the object in the same way:
    my $obj = new pseudohashobject; $obj->{variablee} = 'bar'; # TYPO print $obj->{variable};
    the typo actually causes a runtime exception! This makes very hard to find bugs easy to find, and hence is a good thing (tm). I would suggest looking into Damien Conway's Object Oriented Perl (Manning) for more information. I would almost rate this book as more important than the Camel!
      Hi,

      I'm developing an application that creates a binary tree of objects.
      Previously they were all hash-based, but using pseudohashes has sped up processing by about 3 times!

      Having to use "no strict 'refs';" does worry me a bit though...

      Are pseudohashes really implemented in perl, or just a hack? will they be supported in future versions?
        If you're using no strict 'refs', you're doing something wrong. Pseudohashes work just fine under strict refs.

        It was recently decided on p5p that the current implementation of pseudohashes, as an array ref where the first element is a hash ref, will be deprecated. Pseudohashes themselves will continue to exist, however, hopefully with a more reasonable implementation. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://20091]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-08-31 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls