I have been asked by my current client to create a private, common base class for all of our internal classes to inherit from. It contains such things as autogeneration of accessors and mutators for child classes (so that the corporate conventions can be enforced), etc.

The subject of this question, though, relates to a particular feature. There is an error method that operates similarly to Class::Base's, except that it pushes errors onto an array when setting. When getting, the behavior depends on context -- in scalar context, it returns the last message on the "stack" via pop; in list context it returns a copy of the entire "stack". In either case, any messages returned are removed from the array.

In general, it's a good policy to deal with all of the errors that may have occured before destroying the object, so I've been pondering the following DESTROY method:

sub DESTROY { my $this = shift; my @err = $this->error; while (@err) { my $msg = shift @err; carp "$this destroyed with error: '$msg'" .(@err ? ' (and '.scalar @err.' more).' : '.'); } }

Essentially, this checks to see if there are any errors that haven't been processed, and generates warnings for each of them when an object instance is destroyed. No unprocessed errors, no warnings. The goal here is to gently prod developers into processing errors from objects before allowing them to be destroyed. Sample output from a test script (preceded by a Dumper of the object):

$test = bless( { '_ERR_' => [ 'First error', 'Hello there!' ] }, 'LocalTest2' ); LocalTest2=HASH(0x18633a4) destroyed with error: 'First error' (and 1 +more). at test.pl line 0 LocalTest2=HASH(0x18633a4) destroyed with error: 'Hello there!'. at te +st.pl line 0

The question this raises, wise monks, is simply whether or not this is a good idea. Are there better ways to deal with this? Should I be dealing with this at all? Is there anything about my approach that might cause troubles?

<radiant.matrix>
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet

In reply to Carping about object destruction when errors are unprocessed? by radiantmatrix

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.