Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

RE: RE: My views on pseudohashes

by panache (Initiate)
on Jun 30, 2000 at 00:46 UTC ( #20490=note: print w/ replies, xml ) Need Help??


in reply to RE: My views on pseudohashes
in thread My views on pseudohashes

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!


Comment on RE: RE: My views on pseudohashes
Select or Download Code
Re: RE: RE: My views on pseudohashes
by sparky8342 (Acolyte) on May 23, 2001 at 20:34 UTC
    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://20490]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (15)
As of 2014-10-30 13:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls