bamaindk has asked for the wisdom of the Perl Monks concerning the following question:
We were using a module in our system which was throwing exceptions and killing our script. We could not change the module being used. So we needed to wrap it in a new module to catch the exceptions so we could continue.
The new(wrapper) module would re-implement the functions provided by the exception throwing module and simply call the wrapped functions inside an eval plus some stuff. That way the impact to our system would be minimal and transparent as we could just swap out the 'use' in the affected parts with the new module.
Then I had the idea to make the new module inherit from the exception throwing module and override the functions that threw an exception instead of wrapping them. I thought this would give the benefit of providing all of the functionality already supplied by the module without me having to wrap everything within it, which was rather extensive. Any non implemented functions would simply be passed to the base class. Whereas the wrapping would only give what was supplied in the new module and any non implemented functions would end in an exception. Aahh! Object orientation and re-use:-)
So I did something like this in my constructor.
And within the different overridden functions I called the base class. like thismy $class = shift; my $self = ExceptionThrowingModule->new(args); $self->{_myOwnattributes} = $this; $self->{_myOwnattributes} = $that; bless($self, $class);
I implemented the inheritance and everything seemed to work splendidly until I ran into this! In one of the ExcpetionThrowingModules methods, that I had overridden, it did this.sub funcA{ eval { $self->SUPER::funcA(); }; _handle_eval($self); }
This ended up calling back to my overridden funcB() method as the hash passed in is blessed into my class.funcA{ $self = shift; Yada Yada Yada.... $self->funcB();<--- Which I had also overridden!! }
Does this mean that you can never safely inherit and override a method in Perl?
When SUPER is used to look up an ancestor should it not also ensure the proper object type?
Does anyone have a solution for how to do this?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Overriding methods and Inheritance
by dragonchild (Archbishop) on Jan 07, 2008 at 15:03 UTC | |
by bamaindk (Initiate) on Jan 08, 2008 at 09:29 UTC | |
by dragonchild (Archbishop) on Jan 08, 2008 at 13:41 UTC | |
|
Re: Overriding methods and Inheritance
by friedo (Prior) on Jan 07, 2008 at 14:39 UTC | |
|
Re: Overriding methods and Inheritance
by dsheroh (Monsignor) on Jan 07, 2008 at 17:25 UTC | |
by bamaindk (Initiate) on Jan 08, 2008 at 07:19 UTC | |
by dsheroh (Monsignor) on Jan 08, 2008 at 17:06 UTC |