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:
and then use the resulting object, I can still autovivify member variables, leading to a whole raft of typo problems:package hashobject; sub new { my ($class) = @_; $class = ref($class) || $class; my $self = {}; $self->{variable} = 'foo'; return(bless($self, $class)); }
If we move to a pseudohash, the object looks like this:my $obj = new hashobject; $obj->{variablee} = 'bar'; # TYPO... print $obj->{variable}; # Prints 'foo'
And then (mis)use the object in the same way:package pseudohashobject; use fields qw(variable); sub new { my ($class) = @_; $class = ref($class) || $class; my $self = bless([\%FIELDS], $class]); $self->{variable} = 'foo'; return($self); }
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!my $obj = new pseudohashobject; $obj->{variablee} = 'bar'; # TYPO print $obj->{variable};
In Section
Past Polls