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

Hi,

I'm newbie with problems with basic understanding of object programming under perl.
I have parent class called Tinia_CanExtender and inherited class of Tinia_CanPwm. I have set method defined in parent class and no definition in child class. Now if I call set method on child's class instance, call goes to set method of parent object but with reference to child instance. Now in this method I'd like to get reference to parent object Tinia_CanExtender and do some operations on its data.
I have:
package Tinia_CanPwm; @Tinia_CanPwm::ISA = qw (Tinia_CanExtender);

.... in Tinia_CanExtender ......
sub set { my ($self, $state,$set_by) = @_;


Now $self points to child instance, but I'd like to get reference to parent instance out of it. Can I do that ?
Another question: Can I tie(inherit) child objects to/from particular instance of parent class ?

Thanks in advance,

Robert.

Replies are listed 'Best First'.
Re: Objects: how to get parent object from child $self reference ?
by BrowserUk (Patriarch) on Feb 28, 2004 at 08:17 UTC

    This might clarify the problem with your question. If your Tinia_CanPwn class isa subclass of your Tinia_CanExtender class, as indicated by your code snippet, then there is no "parent instance". There is only one object (instance) and it is an instance of Tinia_CanPwm, that has inhereted methods from the Tinia_CanExtender class ... not from an instance of that class.

    #! perl -slw use strict; { package Test1; ## This is the parent class which inherits from not +hing sub new{ my $class = shift; return bless { __PACKAGE__ => 'test1 attribute value' }, $clas +s; } sub whatAmI{ __PACKAGE__ }; ## No SUPER->whatAmI to call! ## No SUPER->selfRef. sub selfRef{ shift } 1; } { package Test2; our @ISA = 'Test1'; ## A subclass which inherits from Test1. sub new{ my $class = shift; my $self = $class->SUPER::new; $self->{ __PACKAGE__ } = 'test2 attribute value'; return bless $self, $class; } sub whatAmI{ shift->SUPER::whatAmI() . '->' . __PACKAGE__; }; sub superRef{ shift->SUPER::selfRef; } sub selfRef{ shift } 1; } { package Test3; our @ISA = 'Test2'; ## A sub-subclass which inherits from Test2 sub new{ my $class = shift; my $self = $class->SUPER::new; $self->{ __PACKAGE__ } = 'test3 attribute data'; return bless $self, $class; } sub whatAmI{ shift->SUPER::whatAmI() . '->' . __PACKAGE__; }; sub selfRef{ shift } sub superRef{ shift->SUPER::selfRef; } 1; } package main; my $obj = Test3->new; print 'I am a ', $obj->whatAmI, $/; print 'My instance id is ', $obj, $/; print 'My instance id in the class is ', $obj->selfRef, $/ +; print 'My instance id in my parent class is ', $obj->superRef, $ +/; print 'My instance id in my grandparents class is ', $obj->superRef->s +uperRef, $/; __END__ P:\test>332433 I am a Test1->Test2->Test3 My instance id is Test3=HASH(0x18360e0) My instance id in the class is Test3=HASH(0x18360e0) My instance id in my parent class is Test3=HASH(0x18360e0) My instance id in my grandparents class is Test3=HASH(0x18360e0)

    What this attempts to show is that whilst there are three classes providing methods to the instance $obj, the instance handle is the same regardless of which class you are executing a method from.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Timing (and a little luck) are everything!
•Re: Objects: how to get parent object from child $self reference ?
by merlyn (Sage) on Feb 28, 2004 at 01:58 UTC
      Hi,

      I'd like to have one instance of parent class for communication with serial port. Now child instances beside their own data also contribute to "global" send/receive queue in parent class. Now I have method in parent instance that deals with this queue. Method is inherited into child instances and when I call it - I get reference to child object, but would like to perform operations on parent instance' queue.

      Now I would like somehow to get to reference of parent instance from reference of child instance that got into method. How to do this ?

      Thanks,

      Robert.
        You probably don't have an "IS-A" relationship then, but rather a "HAS-A" relationship. One instance of one class (your "parent class") owning a bunch of other specialized classes (your "child class").

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re: Objects: how to get parent object from child $self reference ?
by NetWallah (Canon) on Feb 28, 2004 at 06:37 UTC
    You are confusing an inheritance hierarchy with an Instaciation hierarchy.

    The answer to the question - the way you have asked it - is NO - it cannot be done - because it does not make any sense.

    You need to figure out which process instantiates the parent and the child - That process can be used to connect the two.

    "Experience is a wonderful thing. It enables you to recognize a mistake when you make it again."
Re: Objects: how to get parent object from child $self reference ?
by dragonchild (Archbishop) on Feb 28, 2004 at 18:55 UTC
    First off, based on your replies, you aren't dealing with the problem right. You're confusing parent-child in terms of classes (where @ISA is used) and parent-child in terms of objects (like a Board object is the parent of a Piece object).

    If you want to have the latter, you will need to tell the child object who its parent is when you create it. That is the only way.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Objects: how to get parent object from child $self reference ?
by gmpassos (Priest) on Feb 28, 2004 at 22:52 UTC
    Robert, maybe you need some OO advice.

    Inherite (extend) a class exists to get a basic class and add or overwrite some methods. For example, in GUI OO libraries, is very common to have a basic class for controls, that will handle (implement methods) that are common to all the controls, like create, destroy, show, etc... And for each control, like a Button, we have it's own class that Inherite from the basic class, or extends the basic class. Is like tell to class to import the methods and attributes from another class.

    What you want is to have a object X that handle some socket data, and another object Y that handle more socket data and also work with X. Soo, create one object X, from your class Tinia_CanExtender, and than create the object Y from the class Tinia_CanPwm. If you want that the object Y has internal reference to X, soo you can paste X as an argument, or set an attribute inside Y.

    Take a look in this code:

    my $x = new Tinia_CanExtender() ; my $y = new Tinia_CanPwm($x) ; package Tinia_CanPwm ; @ISA = qw(Tinia_CanExtender); sub new { my $class = shift ; my $this = bless({} , $class) ; my ( $parent ) = @_ ; $this->{PARENT} = $parent ; return $this ; } package Tinia_CanExtender ; sub new { my $class = shift ; my $this = bless({} , $class) ; return $this ; } sub set { my $self = shift ; my ($state,$set_by) = @_; ... }
    If you want to code classes in Perl in a style similar to Java, you can take a look in the module Class::HPLOO.

    Graciliano M. P.
    "Creativity is the expression of the liberty".