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:
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.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.
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.
In reply to Re (tilly) 1: Reverse Inheritance Irritance
by tilly
in thread Reverse Inheritance Irritance
by deprecated
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |