Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Multiple Inheritence, Munging @ISA

by Stevie-O (Friar)
on Jul 07, 2004 at 00:20 UTC ( #372232=note: print w/ replies, xml ) Need Help??


in reply to Multiple Inheritence, Munging @ISA

You're missing part of the picture.

You're right -- any call to a method of a MyRxC::quant object will be dispatched to MyRxA::quant.

Since MyRxA::quant does not define the 'visual' method, its @ISA tree will recursively be searched. This is simply Rx::quant, from what I understand. If Rx::quant defines the 'visual' method, then the Rx::quant one is called instead. Otherwise, Rx::quant's @ISA is checked, depth first, for a 'visual' method.

If no 'visual' method is found in MyRxA::quant, then MyRxB::quant (the 2nd entry in @MyRxC::quant::ISA) is checked for 'visual'. Thus, the MyRxB::quant::visual() method will be called.

Now, if Rx::quant defines a 'visual' method (you didn't actually SAY this, but I have a sneaking suspicion that this is why you made your post), you will have to resolve the issue this way:

package MyRxC::quant; use base 'MyRxA::quant'; use base 'MyRxB::quant'; sub raw { my $this = shift; $this->MyRxA::quant::raw(@_); } sub visual { my $this = shift; $this->MyRxB::quant::visual(@_); }
Explicitly qualifying the package name in the method call will override the usual method lookup. The $this->SUPER::foo() syntax is actually a special case that automagically handles @ISA-ness.

Note that, if you are ONLY delegating and not adding any of your own code, *and* you KNOW for a fact that MyRxA and MyRxB respectively define 'raw' and 'visual' themselves (i.e. instead of inheriting them), a slightly faster technique may be used:

package MyRxC; *raw = \&MyRxA::quant::raw; *visual = \&MyRxB::quant::visual;
This would effectively import the 'raw' and 'visual' methods from the corresponding packages into MyRxC, bypassing the extra call induced with the "$this->MyRxA::quant::raw()" code. The tradeoff is that the former technique plays fine with inheritance, while this latter technique only works if MyRxA and MyRxB directly contain the 'raw' and 'visual' subs in their respective namespaces.
--Stevie-O
$"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc


Comment on Re: Multiple Inheritence, Munging @ISA
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2014-09-16 02:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (155 votes), past polls