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.