Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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 Package A handler: Eins Zwei Drei Package B handler: Eins Zwei Drei Package C handler:

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://488920]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2017-02-26 02:38 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (371 votes). Check out past polls.