Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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]
[Corion]: Not every Krug is a Bembel!
[karlgoethebier]: there is just the mug: 🍺
[Corion]: A Bembel must look like this
[karlgoethebier]: https://de. wiki/Bembel#/ media/File: Bembel_Zum_blauen_ Bock_1978.jpg
[choroba]: as my friend says, Ich stehe Deutsche ver nicht aber klein ;-)
[Corion]: Krug is generic pottery ware for pouring beverages, but a Bembel is painted with that blue paint and contains only (!) Apfelwein
[shmem]: Corion: alas, air too.
[Corion]: shmem: Yes, very sad!
[choroba]: doesn't sound much useful, which means I'll probably remember it
[karlgoethebier]: "that blue pain" == Salzglasur as far as i remember

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (13)
As of 2017-05-24 08:51 GMT
Find Nodes?
    Voting Booth?