Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Package-specific Attribute Handling and Multiple Inheritence

by Roger (Parson)
on Sep 03, 2005 at 14:12 UTC ( #488920=note: print w/ replies, xml ) Need Help??


in reply to Package-specific Attribute Handling and Multiple Inheritence

Quite a few years ago when I was still programming in C++, I found multiple inheritance of virtual functions a very tricky thing to handle.

Say, if class C is derived from class A and class B, and provides an overload for virtual function, say, X, that exists in both A and B, then you have to explicitly say which parent you are going to inherit X from, otherwise it will result in compilation error.

I see this particular problem similar to what I described above. Multiple inheritance of the MODIFY_CODE_ATTRIBUTES function. What is not declared explicitly in package Three is the member function MODIFY_CODE_ATTRIBUTES that is inherited from its parent, either A or B. Because Perl could not resolve the multiple inheritance, it simply picks the first package, One, as the parent class and ignores B.

When the Perl compiler checks the attributes, it first invokes the MODIFY_CODE_ATTRIBUTES function declared in package Three, which is inherited from package One. And when the function returns a non-empty list, Perl assumes that the attributes could not be handled by the parent package as well as itself, and therefore raise the compilation error.

I believe a remedy is to provide a MODIFY_CODE_ATTRIBUTES function in package Three that handles multiple inheritence explicitly.

use strict; use warnings; package One; sub MODIFY_CODE_ATTRIBUTES{ my ($pkg, $ref, @attr) = @_; print "Package A handler: @attr \n"; return @attr; } package Two; sub MODIFY_CODE_ATTRIBUTES{ my ($pkg, $ref, @attr) = @_; print "Package B handler: @attr\n"; return (); } package Three; use base qw[ One Two ]; sub MODIFY_CODE_ATTRIBUTES{ my ($pkg, $ref, @attr) = @_; @attr = $_[0]->One::MODIFY_CODE_ATTRIBUTES($ref, @attr); if (@attr) { @attr = $_[0]->Two::MODIFY_CODE_ATTRIBUTES($ref, @attr); } print "Package C handler: @attr \n"; return @attr; } sub foo : Eins Zwei Drei { } __END__ $ perl attr.pl Package A handler: Eins Zwei Drei Package B handler: Eins Zwei Drei Package C handler:


Comment on Re: Package-specific Attribute Handling and Multiple Inheritence
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2014-07-30 10:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (230 votes), past polls