Heritable::Types - Make object dispatch look at an object's type


  use Heritable::Types

  sub Object::as_string
    { my($self) = @_;
      join " ", 'a', ref($self), $self->content_string; }

  sub HASH::content_string
    { my($self) = @_;
      my $str = join ', ', map {"$_ => $self->{$_}", keys %$self;
      return "{ $str }" }

  sub ARRAY::content_string
    { my($self) = @_;
      return '[ ', join(', ', @$self), ' ]' }


Heritable::Types sets about making Perl's method dispatch system consistent with the way isa works. Right now, if you have an object which you represent as, say, a blessed Hash, then, according to UNIVERSAL::isa, that object is a HASH. But if you implement, say HASH::foo, a method that only exists in the HASH namespace, then UNIVERSAL:can will not see it, nor will it get called if you do $obj->foo. This strikes me as an unsatisfactory state of affairs, hence Heritable::Types.


There's nothing to it, see the synopsis for how it works. Note that, if once one module uses Heritable::Types then *all* objects will do method lookup via their types.

If you want to have a method which all types can inherit from, but which will ensure that individual types can override that method, then you should implement it in the Object class, rather than in UNIVERSAL (if you implement a method in UNIVERSAL there's a good chance that the specific type's methods will never get called, which isn't what anyone wants.


None sighted so far. There are bound to be some though.


What support there is for this module is provided on a "When the author has time" basis. If you do have problems with it, please, drop me a line. Support requests that come with a failing test are greatly appreciated. Bug reports that come with a new test and a patch to fix it will earn my undying gratitude.


        Piers Cawley


This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.