Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OOD overriding problem

Status
Not open for further replies.

whn

Programmer
Oct 14, 2007
265
US
Dear experts,

Below is small pieces of OO perl codes. The calling sequence is not what I expected.

Base Class (base.pm):
Code:
package base;

use strict;
use warnings;

sub new {
  my ($obj, $testArgs, $prodName) = @_;
  print "In base.pm..new()\n";
  my $class = ref($obj) || $obj;
  my $this  = {};
  bless $this, $class;
  return $this;
}

sub basePrepare {
  my ($this) = @_;
  print "In base.pm..basePrepare(), calling subroutine1()\n";
  $this->subroutine1();
};

sub subroutine1 {
  my ($this) = @_;
  print "In base.pm..subroutine1()\n";
}

1;
[

Derived class (derived.pm):
Code:
package derived;

use strict;
use warnings;
use lib './';
use base;
our @ISA = "base";

sub new {
  my ($obj, $testArgs) = @_;
  print "In derived.pm..new()\n";
  my $class = ref($obj) || $obj;
  my $this  = base->new();
  bless $this, $class;
  return $this;
}

sub prepare {
  my ($this) = @_;
  print "In derived.pm..prepare(), calling subroutine1()\n";
  $this->subroutine1();
};

sub subroutine1 {
  my ($this) = @_;
  print "In derived.pm..subroutine1()\n";
}

1;

And the main codes (test.pl):
Code:
#! /usr/bin/perl

use strict;
use warnings;
use derived;

my $obj = derived->new();
print "$0..To call base.pm..basePrepare()\n";
$obj->basePrepare();
print "$0..To call derived.pm..prepare()\n";
$obj->prepare();
exit;

Test run:
Code:
% ./test.pl
In derived.pm..new()
In base.pm..new()
./test.pl..To call base.pm..basePrepare()
In base.pm..basePrepare(), calling subroutine1()
[COLOR=red][b]In derived.pm..subroutine1()[/b][/color]
./test.pl..To call derived.pm..prepare()
In derived.pm..prepare(), calling subroutine1()
In derived.pm..subroutine1()

My question:

I expect that base.pm..basePrepare() would call base.pm..subroutine1(). However, it actually calls In derived.pm..subroutine1() instead.

Could someone please tell me
1) whether my understanding is incorrect?
OR
2) whether my implementation is incorrect?

Many thanks!!
 
You're constructor in the derived module looks strange to me.

you're making $this = a reference to another object and then blessing it as it's own object?

That's not how I inherit a superclass's attributes, i use this...
Code:
sub new {
    
    # class variable
    my $class = shift;

    # instantiate superclass helper object
    my $helper = MySuperClass->new();
    
    # object attributes
    my $self = {
       
        THIS_ATTRIBUTE => 'a value',             
        [b]%{$helper}[/b]             
        
    };
     
    # Bless the data structure, making it a true object
    bless ($self,$class);

    # Return the object to the calling program.
    return $self;
}

But i'm not sure that will sort out the method call issue.

This looks like an overriding issue, i don't beleive perl handles overloading/ridding per sae , so input arguments don't create overloading, as it would in other languages.

The instantiated object is 'derived' and it contains a subroutine1 , I guess perl sees that and so runs it.

If you want a particular function to run from the superclass via the @ISA system array, make sure it isn't called the same in any other module that makes up the entire object.

That's what I'd do at least to test if it resolves the issue.

Then you can look into how perl handles overriding (or perhaps doesn't) and at least give you an angle to come at it from!

Just my pennies worth, hope it helps.





"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
 
Thank you, DMF. I need a bit more time to digest it.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top