radiantmatrix has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Carping about object destruction when errors are unprocessed?
by Old_Gray_Bear (Bishop) on Sep 05, 2006 at 17:56 UTC | |
by radiantmatrix (Parson) on Sep 05, 2006 at 18:42 UTC | |
|
Re: Carping about object destruction when errors are unprocessed?
by duckyd (Hermit) on Sep 05, 2006 at 17:19 UTC | |
by radiantmatrix (Parson) on Sep 05, 2006 at 18:33 UTC |