sub method_metaclass() { 'Mouse::Meta::Method' }
sub attribute_metaclass() { 'Mouse::Meta::Attribute' }
+sub constructor_class() { 'Mouse::Meta::Method::Constructor' }
+sub destructor_class() { 'Mouse::Meta::Method::Destructor' }
+
sub _construct_meta {
my($class, %args) = @_;
$self->{is_immutable}++;
if ($args{inline_constructor}) {
- # generate and install
- Mouse::Meta::Method::Constructor->_generate_constructor_method($self, \%args);
+ $self->add_method($args{constructor_name} =>
+ $self->constructor_class->_generate_constructor($self, \%args));
}
if ($args{inline_destructor}) {
- # generate and install
- Mouse::Meta::Method::Destructor->_generate_destructor_method($self, \%args);
+ $self->add_method(DESTROY =>
+ $self->destructor_class->_generate_destructor($self, \%args));
}
# Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
package Mouse::Meta::Method::Constructor;
use Mouse::Util; # enables strict and warnings
-sub _generate_constructor_method {
+sub _generate_constructor {
my ($class, $metaclass, $args) = @_;
my $associated_metaclass_name = $metaclass->name;
$@;
};
die $e if $e;
-
- $metaclass->add_method($args->{constructor_name} => $code);
- return;
+ return $code;
}
sub _generate_processattrs {
package Mouse::Meta::Method::Destructor;
use Mouse::Util; # enables strict and warnings
-sub _empty_destroy{ }
+sub _empty_DESTROY{ }
-sub _generate_destructor_method {
- my ($class, $metaclass) = @_;
+sub _generate_destructor{
+ my (undef, $metaclass) = @_;
- my $demolishall = do {
- if ($metaclass->name->can('DEMOLISH')) {
- my @code = ();
- for my $class ($metaclass->linearized_isa) {
- no strict 'refs';
- if (*{$class . '::DEMOLISH'}{CODE}) {
- push @code, "${class}::DEMOLISH(\$self);";
- }
- }
- join "\n", @code;
- } else {
- $metaclass->add_method(DESTROY => \&_empty_destroy);
- return;
+ if(!$metaclass->name->can('DEMOLISH')){
+ return \&_empty_DESTROY;
+ }
+
+ my $demolishall = '';
+ for my $class ($metaclass->linearized_isa) {
+ no strict 'refs';
+ if (*{$class . '::DEMOLISH'}{CODE}) {
+ $demolishall .= "${class}::DEMOLISH(\$self);\n";
}
- };
+ }
- my $destructor_name = $metaclass->name . '::DESTROY';
my $source = sprintf("#line %d %s\n", __LINE__, __FILE__) . <<"...";
- sub $destructor_name \{
+ sub {
my \$self = shift;
$demolishall;
}
...
+ my $code;
my $e = do{
local $@;
- eval $source;
+ $code = eval $source;
$@;
};
die $e if $e;
- return;
+ return $code;
}
1;