objects

package test;

sub new {
  return undef if ( !defined $this->{NAME} );
}
sub createNames {
}

sub displayNames {
}

--------------------------------------------------------------------------------------------------------


package test1;

sub new {
  my $proto = shift;
  my $class = ref($proto) || $proto;
  my $this = {};
  bless($this, $class);
  my %hash = @_;

  $this->{ID}             =  $hash{ID} ;
  $this->{OBJECT}          = ### this should be object reference to package test.
  return undef if ( !defined $this->{OBJECT} );
}

sub print {

}

--------------------------------------------------------------------------------------------
package test2;
use base 'test1';
sub new {
  my $proto       = shift;
  my $class       = ref($proto) || $proto;
  my %hash      = @_;
  my $this        = {};
  bless($this, $class);
  my $this             = $class->SUPER::new(%hash_args);  
   
  return $this;
}

sub disp {
### how can i access displayNames() of class "test" here
}

--------------------------------------------------------------------------------------------


example.pl

use test2;
my $obj = test2->new(ID=>"1234");
### how can $obj access the method displ of module "test2"
### how can i call displayName of "test"
perlperlAsked:
Who is Participating?
 
ozoCommented:
sub disp {
    my $self=shift;
    print $self->{OBJECT}->displayNames();
### how can i access displayNames() of class "test" here
}
0
 
ozoCommented:
package test1;

sub new {
  my $proto = shift;
  my $class = ref($proto) || $proto;
  my $this = {};
  bless($this, $class);
  my %hash = @_;

  $this->{ID}             =  $hash{ID} ;
  $this->{OBJECT}          = bless {},"test";
  return undef if ( !defined $this->{OBJECT} );
  return $this;
}


use test2;
my $obj = test2->new(ID=>"1234");
### how can $obj access the method displ of module "test2"
$obj->disp;
### how can i call displayName of "test"
$obj->{OBJECT}->displayNames;
0
 
perlperlAuthor Commented:
Ozo,
what will be
sub disp  { }   in package "test2"
also, will thre be any change in new() of test2
0
 
perlperlAuthor Commented:
ok..will try later and let you know....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.