in reply to perl calls unexpected function
You can override normal method lookup, like $self->X::funcB(). I think there recently was a thread on PerlMonks about it. It's an extension to the slightly more orthodox $self->SUPER::funcB() call mechanism.
Update: found it: Invoking method in another package.
Try this, all in one single script — which again shows that it's the package the method is in, that matters, not the source file. The BEGIN block for the assignment to @theModule::ISA is only to make sure it is set when the main code runs, as the module definition is underneath the main code in the script source.
This prints:$obj = new theModule; $obj->funcA('own'); $obj->funcA('X'); $obj->funcA('SUPER'); package theModule; BEGIN { @ISA = 'Y'; } sub new { my %self; return bless \%self, shift; } sub funcA { my $self = shift; my $param = shift; print "\nfuncA in theModule got called, param = $param\n"; if($param eq 'own') { $self->funcB; } elsif($param eq 'X') { $self->X::funcB; } elsif($param eq 'SUPER') { $self->SUPER::funcB; } } sub funcB { my $self = shift; print "funcB in theModule is called\n"; } package X; sub funcB { my $self = shift; print "funcB in X is called\n"; } package Y; sub funcB { my $self = shift; print "funcB in Y is called\n"; }
funcA in theModule got called, param = own funcB in theModule is called funcA in theModule got called, param = X funcB in X is called funcA in theModule got called, param = SUPER funcB in Y is called
|
|---|