http://www.perlmonks.org?node_id=67447


in reply to Reverse Inheritance Irritance

One thing that tye and I agree on is that inheritance is used far more than it should be. Now I cannot say whether this has anything to do with your problem, but when you inherit from a module whose guts you do not know, you get their implementation and your assumptions about what you can and cannot do with objects are likely to break.

But luckily it is possible to inherit the interface without running the risk of problems with the implementation. Here is a simple example showing how to do that:

package parent; use strict; sub new { return bless {}, shift; } sub talk { shift; print @_; } 1; package child; use Carp; use strict; use vars qw($AUTOLOAD); sub new { my $self = bless {}, shift; $self->{hndl} = new parent(@_); return $self; } sub AUTOLOAD { $AUTOLOAD =~ s/.*://; if (UNIVERSAL::can("parent", $AUTOLOAD)) { eval " sub $AUTOLOAD { (shift)->{hndl}->$AUTOLOAD(\@_); }"; no strict 'refs'; goto &$AUTOLOAD; } else { confess("Method $AUTOLOAD not implemented"); } } 1; package main; my $thing = new child; $thing->talk("Hello world\n"); # In the interface $thing->bye(); # Oops.
The trick is in AUTOLOAD. If you call a method that you have not defined it will see whether it can proxy it. If it can then it creates that method and goes to it. If it can't then it blows up.

UPDATE
Took out the check for DESTROY in AUTOLOAD. I could have sworn that it could be called implicitly but it wasn't when I tested? Ah well, live and learn.

UPDATE 2
I misunderstood the (non)output from the tests. DESTROY was being called after all. I just wasn't seeing the output from it.

Replies are listed 'Best First'.
Re: Re (tilly) 1: Reverse Inheritance Irritance
by darobin (Monk) on Mar 28, 2001 at 01:32 UTC

    Inheritance is indeed overused and proxying is often a good way to work around it. The downside to that is that it eats up more memory, and runs slower.

    While that is usually not bigger and slower enough to be truly detering, in some cases (mostly when you have potentially *lots* of object, for instance in a DOM tree) though it's clearly counter-indicated. I've been planning for a while to look into the ex::interface module on CPAN which is supposed to allow for interface inheritance but I haven't found the time at moments when my brain is in a good enough state :-/

    PS: do you still have the code that made DESTROY not turn up in AUTOLOAD ? It should really be called explicitly... a code construct that doesn't force one to check for that would be interesting.

    -- darobin

      I was seriously confused about this. After a bit of debugging I figured it out though. Here is a simple example that shows what is going on:
      my $test = sub {bless {}}; $test->(); $test->(); $test->(); sub DESTROY { print "Calling DESTROY\n"; die "But you won't see this message\n"; }
      See it?

      DESTROY is called in an eval. Since my code was only putting out output within a confess, that output was trapped. So I saw nothing, the script kept on going, and I thought that DESTROY wasn't being called. When in fact it is...

      Mystery solved. :-)