use warnings;
use Mouse::Meta::Method::Constructor;
+use Mouse::Meta::Method::Destructor;
use Mouse::Util qw/get_linear_isa blessed/;
use Carp 'confess';
my $name = $self->name;
$self->{is_immutable}++;
no strict 'refs';
- *{"$name\::new"} = Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self );
+ *{"$name\::new"} = Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self );
+ *{"$name\::DESTROY"} = Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self );
}
sub make_mutable {
Carp::croak "Mouse::Meta::Class->make_mutable does not supported by Mouse";
--- /dev/null
+package Mouse::Meta::Method::Destructor;
+use strict;
+use warnings;
+
+sub generate_destructor_method_inline {
+ my ($class, $meta) = @_;
+
+ my $demolishall = do {
+ if ($meta->name->can('DEMOLISH')) {
+ my @code = ();
+ no strict 'refs';
+ for my $klass ($meta->linearized_isa) {
+ if (*{$klass . '::DEMOLISH'}{CODE}) {
+ push @code, "${klass}::DEMOLISH(\$self);";
+ }
+ }
+ join "\n", @code;
+ } else {
+ ''; # no demolish =)
+ }
+ };
+
+ my $code = <<"...";
+ sub {
+ my \$self = shift;
+ $demolishall;
+ }
+...
+ warn $code if $ENV{DEBUG};
+
+ local $@;
+ my $res = eval $code;
+ die $@ if $@;
+ $res;
+}
+
+1;
--- /dev/null
+use strict;
+use warnings;
+use Test::More tests => 2;
+use t::Exception;
+
+my $i;
+
+{
+ package Parent;
+ use Mouse;
+ sub DEMOLISH {
+ main::is $i++, 1;
+ }
+ no Mouse;
+ __PACKAGE__->meta->make_immutable;
+}
+
+{
+ package Child;
+ use Mouse;
+ extends 'Parent';
+ sub DEMOLISH {
+ main::is $i++, 0;
+ }
+ __PACKAGE__->meta->make_immutable;
+}
+
+Child->new();
+