package Mouse::Meta::Method::Destructor;
-use Mouse::Util; # enables strict and warnings
+use Mouse::Util qw(:meta); # enables strict and warnings
-sub _empty_DESTROY{ }
+use constant _MOUSE_DEBUG => !!$ENV{MOUSE_DEBUG};
sub _generate_destructor{
my (undef, $metaclass) = @_;
- 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";
+ if (Mouse::Util::get_code_ref($class, 'DEMOLISH')) {
+ $demolishall .= ' ' . $class
+ . '::DEMOLISH($self, $Mouse::Util::in_global_destruction);'
+ . "\n",
}
}
- my $source = sprintf("#line %d %s\n", __LINE__, __FILE__) . <<"...";
+ if($demolishall) {
+ $demolishall = sprintf <<'EOT', $demolishall;
+ my $e = do{
+ local $?;
+ local $@;
+ eval{
+ %s;
+ };
+ $@;
+ };
+ no warnings 'misc';
+ die $e if $e; # rethrow
+EOT
+ }
+
+ my $name = $metaclass->name;
+ my $source = sprintf(<<'EOT', __FILE__, $name, $demolishall);
+#line 1 "%s"
+ package %s;
sub {
- my \$self = shift;
- $demolishall;
+ my($self) = @_;
+ return $self->Mouse::Object::DESTROY()
+ if ref($self) ne __PACKAGE__;
+ # DEMOLISHALL
+ %s;
+ return;
}
-...
+EOT
+
+ warn $source if _MOUSE_DEBUG;
my $code;
my $e = do{
1;
__END__
+
+=head1 NAME
+
+Mouse::Meta::Method::Destructor - A Mouse method generator for destructors
+
+=head1 VERSION
+
+This document describes Mouse version 0.81
+
+=head1 SEE ALSO
+
+L<Moose::Meta::Method::Destructor>
+
+=cut