in reply to Understanding 'Multiple Inheritance'
I love Perl, but it's "object orientation" is incredibly brittle as a result of it being so damned ad-hoc. Really, Perl is name space oriented, and the actual storage of member data is up to the creator of a class. Typically this is done by blessing a hash into a class, and then making the "member variables" be elements of said hash. This, apart from being inefficient, is functional in the case of stand alone classes. It becomes dangerous when you inherit from another class as name clashes can occur, and the dangerousness increases geometrically with the size of your inheritence family.
In either C++ or Java, member variables are explicitly specified as being either public, protected, or private. If they are private, then derived classes cannot even see them, and if they are protected, then descendants can see them, but there is name-spacing such that in the case of a base and derived class having a duplicate name, there are actually two distinct variables. In this case, I believe access to a variable in a derived class defaults to the one in the child class, and (in the case of protected and public) the parent class's variable can be accessed by explicitly naming it with the parent class's name. The base class will know nothing of the child class's variable by the same name.
Unfortunately, in Perl you're just stuck with everything being in a single hash. The best thing I can think to do is to have your hash be two-tiered, with the first level of keys being class names, and the second level being member data for the corresponding class. This saves you from having classes in a hierarchy carelessly step on one another's data. So, instead of seeing methods like this...
you would instead have stuff like...sub set_foo { my ($self, $val) = @_; $self->{foo} = $val; }
sub set_foo { my ($self, $val) = @_; $self->{ref($self)}{foo} = $val; }
Of course, this has two problems... First of all, everyone has to agree to do it. If you have ultimate control over all classes in the hierarchy, then maybe this isn't a problem, but what if you want to write classes derived from a class provided by a third party library? You are at their mercy. Also, another problem is that unlike C++ and Java which effect this name-spacing at compile time, this solution in Perl does it at run time, and we end up paying a penalty for having to drill through not one level of a hash, but two. It's bad enough as it is that we have to deal with hashes pretending to be objects. Slowing down member variable access by a factor of two might be unacceptable.
As best I can tell, Python also drops the ball on inheritance. The local dictionary associated with every Python object is basically the same thing as what we have in Perl, albeit built into the language core explicitly.
Does anyone know if Perl6 is going to suck less in this regard? I would truly love to have proper objects in Perl. The present ones are disappointing.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Inheritance can be messy in Perl. Multiple inheritance can be disastrous.
by jdporter (Paladin) on Mar 07, 2005 at 17:10 UTC | |
by Anonymous Monk on Mar 08, 2005 at 09:39 UTC | |
by jdporter (Paladin) on Mar 08, 2005 at 15:26 UTC | |
by Anonymous Monk on Mar 08, 2005 at 16:59 UTC | |
by jdporter (Paladin) on Mar 08, 2005 at 17:47 UTC | |
|
Re: Inheritance can be messy in Perl. Multiple inheritance can be disastrous.
by Anonymous Monk on Mar 08, 2005 at 09:32 UTC |