Subject: | patch: override E::C object methods during initialisation |
Additional declaration option: install_sub. Overrides Exception::Class
object methods during initialisation; so it is possible to bundle all
Exception::Class code in the use statement.
Subject: | Class.pm.diff |
--- Class.pm.v1.23
+++ Class.pm
@@ -6,6 +6,7 @@
use vars qw($VERSION $BASE_EXC_CLASS %CLASSES);
use Scalar::Util qw(blessed);
+use Sub::Install qw();
BEGIN { $BASE_EXC_CLASS ||= 'Exception::Class::Base'; }
@@ -158,6 +159,18 @@
}
}
+ if ( my $install_subs_ref = $def->{install_sub} )
+ {
+ foreach my $sub_name (keys %{ $install_subs_ref })
+ {
+ Sub::Install::install_sub({
+ code => $install_subs_ref->{$sub_name},
+ into => $subclass,
+ as => $sub_name,
+ });
+ }
+ }
+
if ( my $alias = $def->{alias} )
{
die "Cannot make alias without caller"
@@ -411,6 +424,19 @@
fields => [ 'grandiosity', 'quixotic' ],
alias => 'throw_fields',
},
+
+ 'ExceptionWithCustomObjectMethods' =>
+ { install_sub =>
+ { full_message => sub # localised error message
+ { my $self = shift;
+ return translate($self->message);
+ },
+ trace => sub # shortened trace
+ { my $self = shift;
+ return substr($self->{trace}, 0, 100);
+ },
+ },
+ },
);
# try
@@ -535,6 +561,18 @@
someone forgot to document them) and you don't understand the error
messages.
+=item * install_sub
+
+Pass a hashref to this option. Its keys are identifiers,
+its values are anonymous subs. The anonymous subs are
+installed under the name of the corresponding identifiers.
+
+Use this if you want to override the behaviour of the
+L<object methods|/"Exception::Class::Base OBJECT METHODS">
+during initialisation of the object, or need to define
+fields that have more complex behaviour than just
+returning whatever was stored in them.
+
=back
The C<Exception::Class> magic attempts to detect circular class