Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How to use Inheritance with Inside-Out Classes and Hash Based Classes

by ikegami (Pope)
on Nov 15, 2005 at 17:13 UTC ( #508697=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: How to use Inheritance with Inside-Out Classes and Hash Based Classes
Select or Download Code
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://508697]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2014-12-20 11:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls