in reply to Objects: how to get parent object from child $self reference ?

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".

  • Comment on Re: Objects: how to get parent object from child $self reference ?
  • Download Code