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


in reply to How to use Inheritance with Inside-Out Classes and Hash Based Classes

The short answer is that you can't. You need to dispatch the methods to the "base class" yourself instead of letting perl do it through @ISA, because you need to use ident on the invoker first. In other words, you need to wrap the "base class" instead of inheriting from it.

It can be done by removing the use base and adding one of the following functions:

sub AUTOLOAD { my $method = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2); my $code_ref = File::Samba->can($method); if (!$code_ref) { require Carp; Carp::croak("Undefined subroutine $method called"); } # Create stub functions to avoid calling # AUTOLOAD more than necessary. my $stub = sub { # This only works with methods, # and only if they are non-static. local $_[0] = ident $_[0]; &$code_ref; }; { no strict 'refs'; *$method = $stub; } goto($stub); }

or

sub AUTOLOAD { my $method = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2); my $code_ref = File::Samba->can($method); if (!$code_ref) { require Carp; Carp::croak("Undefined subroutine $method called"); } # Don't create stub functions to avoid # clearing perl's method cache repeatedly. # See http://www.perlmonks.org/?node_id=505443 # This only works with methods, # and only if they are non-static. local $_[0] = ident $_[0]; goto($code_ref); }

You might want to override can method to check File::Samba in addition to your class. The following should do the trick, but it's untested:

sub can { my $p = shift(@_); return UNVERSAL::can($p, @_) || File::Samba->can(@_); }

The above function have been tested (IIRC), but to a limited extent.

There may be a better method.

Update: Various bits added for roundness.

Replies are listed 'Best First'.
Re^2: How to use Inheritance with Inside-Out Classes and Hash Based Classes
by diotalevi (Canon) on Nov 15, 2005 at 19:54 UTC
    This is also called delegation. Its generally preferred to ISA anyway since it doesn't specify that a particular object *is* another kind of thing, it has-a the other object, uses it, and forwards appropriate things through.