kEndE has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am trying to put together my first API and I am a bit stucked right now. I am just trying to get famillier with the OO side of the Perl, so I dont know whether is possible what I am trying to do or not.

So, the question is:
If I create a Kid object (see below example), which is the parent of Son and Daughter, can I call somehow the Kid's functions through the Kid, even though there is no Son instance yet? (The Kid should create one)

The reason why I am trying to do this, because I would like to be able to use the child modules separately also, plus would like to reach all the child modules from the parent.

(If I wasn't enough clear about my problem, feel free to ask me :) )

Example:
Kid.pm (parent) ------ package Kid; sub new { bless .... } sub getSex { print("I am unisex \n"); } Son.pm ------ package Kid::Son; @ISA = qw(Kid); use Kid; sub getSex { print("I am a boy \n"); } Daugther.pm ----------- package Kid::Daugther; @ISA = qw(Kid); use Kid; sub getSex { print("I am a girl \n"); } test.pl ------- #!/usr/bin/perl use Kid; use Kid::Son; use Kid::Daugther; # Create Instances $kid = Kid->new(); $son = Kid::Son->new(); # 1. Its OK $kid->getSex(); # 2. Its OK too $son->getSex(); # 3. Its not OK $kid->son->getSex(); # I know its wrong !
Thx, Kende

Replies are listed 'Best First'.
Re: OO Perl Inheritance - Is it possible somehow?
by kyle (Abbot) on Dec 17, 2008 at 16:16 UTC

    For $kid->son to do anything, there has to be a son method defined in the Kid package. If you want the $kid object to know about the $son object, you'll have to tell it somewhere, but then the question is what to do when there's more than one Kid::Son created.

Re: OO Perl Inheritance - Is it possible somehow?
by gwadej (Chaplain) on Dec 17, 2008 at 18:37 UTC

    Although I generally agree with Bloodnok's suggestion of using composition where possible, this is actually a proper need for inheritance.

    The problem you are having is due to the fact that the Kid should not be instantiated directly. If you think about it every male Kid should be a Kid::Son and every female Kid should be a Kid::Daughter. What does instantiating a generic Kid mean? In other languages, we could make this an abstract base class so that you can't create one, but can only create one of it's derived classes.

    Looked at another way, you should never have a raw unsexed Kid lying around in your code. But, the Kid class defines an interface that is shared by both Kid::Son and Kid::Daughter.

    The problem isn't with the OOP, it's that you are confusing the purpose of the base class. This kind of thinking is one of the hard parts of trying to learn OOP.

    It does eventually make sense, BTW

    G. Wade
Re: OO Perl Inheritance - Is it possible somehow?
by Bloodnok (Vicar) on Dec 17, 2008 at 16:34 UTC
    The short answer to the question posed in the title is undoubtedly yes, it is possible.

    However, that being said, composition is, these days, preferred over inheritance - hence inheritance per-se is, altho' extremely useful, not regarded as highly as it once was. To then have an inverse relationship between 2 inheriting classes is a worse idea since the coupling (introduced by the relationship itself) between the classes is exacerbated - leading to all sorts of problems e.g. maintenance, further down the line.

    Just my 2 penn'orth

    A user level that continues to overstate my experience :-))
      I definitely do not see the big picture here.

      If I am not able to reach the child's getSex() sub in a simply way, then whats the point of the inheritance. I mean the Parent should know about the child's subs.

      If I do the inheritance coming from the other direction, so the Son+Daugther are the parents and the Kid is the child then I will be able to reach the subs, but this is logically wrong.

      Kid.pm ------ package Kid; @ISA = (Son); use Son; sub new { } Son.pm ------- package Son; sub new { } sub getSonSex { print("I'm a boy \n"); } test.pl ------- use Kid; use Son; $kid = Kid->new(); # It's OK $kid->getSonSex();
      I'm getting more and more confused about these OO terms :)

        ohhh I am wrong. The child knows about the Parent's subs. :) Because he/she inherits the knowledge of the Elders :)