http://qs321.pair.com?node_id=20490


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!