G'day nysus,
"OK, I just learned that when extending non-Moose modules, you should use the MooseX::NonMoose package."
I did this a few years back;
these two articles were extremely helpful:
-
Subclassing Tricky Non-Moose Classes: Don't Do It
-
Subclassing Tricky Non-Moose Classes: Constructor Problems
I used the information in those blogs to code Anomaly (a personal, unpublished module),
which extends Exception::Class::Base.
This module is complete and working;
it has examples of code that will cover much of what you might need, such as BUILDARGS & FOREIGNBUILDARGS, and uses MooseX::ClassAttribute.
package Anomaly v0.0.1;
use 5.12.0;
use Moose;
use MooseX::ClassAttribute;
use MooseX::NonMoose;
extends qw(Exception::Class::Base);
use namespace::autoclean;
class_has generic_description => (
is => q{ro},
isa => q{Str},
default => q{Generic Anomaly},
);
around BUILDARGS => sub {
my ($rc_constructor, $proto, @args) = @_;
my $class = ref $proto || $proto;
if (@args % 2) {
push @args, q{message};
}
return $class->$rc_constructor(@args);
};
sub FOREIGNBUILDARGS {
my ($proto, @args) = @_;
my $class = ref $proto || $proto;
my %wanted = ();
if (@args % 2) {
$wanted{message} = shift @args;
}
my %remaining = (@args);
my %translation = (msg => q{message});
for my $key ($class->meta->get_attribute_list()) {
if (exists $remaining{$key}) {
if (exists $translation{$key}) {
$wanted{$translation{$key}} = $remaining{$key};
}
delete $remaining{$key};
}
}
if (exists $remaining{error}) {
if (! exists $wanted{message}) {
$wanted{message} = $remaining{error};
}
delete $remaining{error};
}
return (%wanted, %remaining);
}
has description => (
is => q{rw},
isa => q{Str},
default => sub { __PACKAGE__->generic_description() },
);
has detail => (
is => q{ro},
isa => q{Str},
default => q{},
);
has additional_info => (
is => q{ro},
isa => q{ArrayRef[HashRef]},
default => sub { [] },
lazy => 1,
required => 0,
);
sub add_additional_info {
my ($self, $rh_new_info) = @_;
my $ra_current_info = $self->additional_info();
push @$ra_current_info, $rh_new_info;
$self->additional_info($ra_current_info);
return;
}
no Exception::Class::Base;
no MooseX::NonMoose;
no MooseX::ClassAttribute;
no Moose;
__PACKAGE__->meta->make_immutable;
1;