Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Inheritance confused

by derby (Abbot)
on Dec 07, 2015 at 13:59 UTC ( #1149577=note: print w/replies, xml ) Need Help??


in reply to Inheritance confused

You do not need to keep a reference to the parent, that's what SUPER does for you. In the Lite.pm constructor, drop the parent and in the methods, just

$self->SUPER::chkAccessRight
that construct will move up the inheritance tree to find the next chkAccessRight method. This construct
$self->{Parent}->SUPER::chkAccessRight
asks the *parent* object to move up it's hierarchy to find the method. Since the parent is the base, there's nowhere to move up to.

-derby

Replies are listed 'Best First'.
Re^2: Inheritance confused
by exilepanda (Pilgrim) on Dec 07, 2015 at 14:22 UTC
    Thanks derby,

    I just modified my code, and you're right, removed the Parent reference also resolve the problem.

    However, I have reason to keep the Base object inside {Parent}, as there are other constructors which will be invoked by other methods in Lite class.

    Since I have the Base reference, then I can make this shareable ( and I need to ) amount other on-the-fly constructed objects. Is there possible to keep my {Parent} ?

      Sure, you could keep the parent just as you have. In your OP, it's not necessary and I haven't had enough coffee this morning to fully evaluate your response but in general keeping a reference like that is pretty much an OO code smell. A class that instantiates objects is normally a Factory but for most Factory classes, that's *all* they do - they do not have other behaviors.

      -derby
      However, I have reason to keep the Base object inside {Parent}, as there are other constructors which will be invoked by other methods in Lite class.

      There's a better way to do that by making the constructor in the parent class subclass-friendly. This involves checking the first argument you get in an OO constructor routine and identifying if it has a valid ref() associated with it. If you write your constructors as my example code below shows, you'll be making life easy for anyone else who wants to built on your code base.

      In the code below, note a couple of things:

      1. Note that the $ex2 object in consumer.pl is created by using the constructor method of the $ex1 object, which is of the "Improved" subclass.
      2. See how the Parent class uses the ref() call to correctly determine when a subclass is re-using the Parent's constructor method.

      Here's the example code structure:

        Thanks Apero,

        The way you suggested can resolve the method calling issue. But this won't able to the make the {Parent} attributes shareable in different constructors in Lite in my case.

        That said, with my Lite class, I might have

        $admin = MyTest::Lite -> asAdmin( $id ); $user = MyTest::Lite -> asUser( $id );
        where a data dump will give something like:
        $admin => { Mode => 'Admin', CommonParentAttributes => {...} }, MyTest::Lite
        and
        $user => { Mode => 'User', CommonParentAttributes => {...} }, MyTest::Lite
        I don't actually mind if Mode is written into the CommonParentAttributes (thus I don't need CommomParentAttributes at all) , but when $user is created, $admin's Mode will become 'User'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-12-08 12:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?