Dear experts,
Below is small pieces of OO perl codes. The calling sequence is not what I expected.
Base Class (base.pm):
Derived class (derived.pm):
And the main codes (test.pl):
Test run:
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!!
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!!