Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Exception-Class CPAN distribution.

Report information
The Basics
Id: 27726
Status: rejected
Priority: 0/
Queue: Exception-Class

People
Owner: Nobody in particular
Requestors: daxim [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 1.23
Fixed in: (no value)



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
Subject: Re: [rt.cpan.org #27726] patch: override E::C object methods during initialisation
Date: Sat, 30 Jun 2007 23:07:54 -0500 (CDT)
To: Lars Dɪᴇᴄᴋᴏᴡ 迪拉斯 via RT <bug-Exception-Class [...] rt.cpan.org>
From: Dave Rolsky <autarch [...] urth.org>
On Mon, 25 Jun 2007, Lars Dɪᴇᴄᴋᴏᴡ 迪拉斯 via RT wrote: Show quoted text
> 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.
The syntax for doing this is pretty gross. If you _really_ insist on doing something this dangerous, I'd suggest you just go ahead and do something like this: { package Exception::Class; sub full_message { ... } } That'll look as gross and dangerous as it is ;) But you'd probably be better off simply making your own generic subclass of Exception::Class::Base and then making all your real exceptions override subclass it: use Exception::Class( 'Base', 'Real' => { isa => 'Base' }, ); package Base; use base 'Exception::Class'; sub full_message { ... } This is much, much cleaner. In summary, given that there are better and safer ways to do what you want, I won't be adding this feature. -dave