[tpm] Understanding 'use base ...'

Rick Delaney rick at bort.ca
Tue Jul 15 14:58:27 PDT 2008


On Jul 15 2008, Uri Guttman wrote:
> multiple inheritance can be wacky no matter how you set up @ISA. this is
> why some langs disallow it altogether. the classic diamond pattern of
> inheritance has serious issues (A inherits from B & C which both inherit
> from D. does an inherited method call to A check B then D and then C or
> B then C then D?)
> 
> but my point about use base vs. explicit assigning of @ISA stands. there
> is no difference between them. use base is cleaner as it loads the
> module and sets @ISA for you in one line.

That would be nice, if true.  But base.pm does things to try to prevent
ever-growing @ISA for loads of the same module.  So for (not-real-world)
example,

    package R;
    use base qw(Q);

    package S;
    use base qw(Q R);

@S::ISA is ["Q"] instead of ["Q", "R"].  Which in this example would
mean that no methods from R would be found for an object of type S.
This is not what most people would expect, I think.

Your point about multiple inheritance is well taken but some people do
try to use it and using it along with base.pm can lead to these kinds of
problems.  There is a much simpler module on CPAN, parent.pm, that acts
in the way one would expect base.pm to behave.

    http://search.cpan.org/~corion/parent-0.221/lib/parent.pm

-- 
Rick Delaney
rick at bort.ca


More information about the toronto-pm mailing list