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.

my $class = shift; my $self = ExceptionThrowingModule->new(args); $self->{_myOwnattributes} = $this; $self->{_myOwnattributes} = $that; bless($self, $class);
And within the different overridden functions I called the base class. like this
sub funcA{ eval { $self->SUPER::funcA(); }; _handle_eval($self); }
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.
funcA{ $self = shift; Yada Yada Yada.... $self->funcB();<--- Which I had also overridden!! }
This ended up calling back to my overridden funcB() method as the hash passed in is blessed into my class.
So, it seems to me, I just broke the class I inherited from without illegally access it or anything:-(

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?


In reply to Overriding methods and Inheritance by bamaindk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.