cybergeek has asked for the wisdom of the Perl Monks concerning the following question:
Hi guys, could you help me on that one : I'm using two classes, one derived from the other one. My code looks like this :
* Super class (called "A") contains:* The derived class (called "B") contains :sub Schedule { my ($self) = @_; $self->addTab(\&WatchLog); $self->addTab(\&SendEmail); } sub WatchLog { ... } sub SendEmail { ... } sub addTab { ... } sub go { my ($self) = @_; foreach my $code (@{$self->{CODETAB}}){ &{$code}($self)) } }
use base A .... sub Schedule { my ($self) = @_; $self->addTab(\&AddLocalTime); $self->addTab(\&WatchLog);#This method is defined in the paren +t class $self->addTab(\&SendEmail); #Same here } sub AddLocalTime { }
If I create an instance of B in a script and call $b->Schedule I get an "undefined subroutine" error for the two references corresponding to the parent's methods.
I don't know how to get a reference to a parent's method (A) from the derived class (B). One hinted me to use the can method.
More over, I have been told that using references to dynamically call methods was not the best approach. Do you have an idea about how I could do the same code while being more respectful of the object structure ?
My final aim being that the script having the B instance is able to modify the Schedule by removing, inserting, adding method calls into the Scheduler via various addTab,delTab .... methods.
Thanks** UPDATED ** The solution which consist in calling the "can" method works !
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: I'm in trouble with Perl's OO programming
by guha (Priest) on Jun 09, 2005 at 09:16 UTC | |
by chromatic (Archbishop) on Jun 09, 2005 at 17:32 UTC | |
by guha (Priest) on Jun 09, 2005 at 20:30 UTC | |
|
Re: I'm in trouble with Perl's OO programming
by revdiablo (Prior) on Jun 09, 2005 at 17:30 UTC | |
by cybergeek (Novice) on Jun 10, 2005 at 11:24 UTC |