Checking in changes prior to tagging of version 0.89.
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Destructor.pm
CommitLineData
bbf64e76 1package Mouse::Meta::Method::Destructor;
3821b191 2use Mouse::Util qw(:meta); # enables strict and warnings
bbf64e76 3
401ad01d 4use constant _MOUSE_DEBUG => $ENV{MOUSE_DEBUG} ? 1 : 0;
68ad9d66 5
380e1cd7 6sub _generate_destructor{
7 my (undef, $metaclass) = @_;
bbf64e76 8
380e1cd7 9 my $demolishall = '';
10 for my $class ($metaclass->linearized_isa) {
a5c683f6 11 if (Mouse::Util::get_code_ref($class, 'DEMOLISH')) {
68ad9d66 12 $demolishall .= ' ' . $class
13 . '::DEMOLISH($self, $Mouse::Util::in_global_destruction);'
14 . "\n",
bbf64e76 15 }
380e1cd7 16 }
bbf64e76 17
68ad9d66 18 if($demolishall) {
19 $demolishall = sprintf <<'EOT', $demolishall;
70a92220 20 my $e = do{
21 local $?;
22 local $@;
7746ba36 23 eval{
70a92220 24 %s;
7746ba36 25 };
70a92220 26 $@;
7746ba36 27 };
28 no warnings 'misc';
70a92220 29 die $e if $e; # rethrow
68ad9d66 30EOT
31 }
32
33 my $name = $metaclass->name;
34 my $source = sprintf(<<'EOT', __FILE__, $name, $demolishall);
35#line 1 "%s"
36 package %s;
37 sub {
38 my($self) = @_;
39 return $self->Mouse::Object::DESTROY()
40 if ref($self) ne __PACKAGE__;
41 # DEMOLISHALL
42 %s;
43 return;
bbf64e76 44 }
b17191d0 45EOT
bbf64e76 46
68ad9d66 47 warn $source if _MOUSE_DEBUG;
48
380e1cd7 49 my $code;
2a464664 50 my $e = do{
51 local $@;
380e1cd7 52 $code = eval $source;
2a464664 53 $@;
54 };
ad087d11 55 die $e if $e;
380e1cd7 56 return $code;
bbf64e76 57}
58
591;
0126c27c 60__END__
a25ca8d6 61
62=head1 NAME
63
b842ace0 64Mouse::Meta::Method::Destructor - A Mouse method generator for destructors
a25ca8d6 65
66=head1 VERSION
67
5b3e7678 68This document describes Mouse version 0.89
a25ca8d6 69
70=head1 SEE ALSO
71
72L<Moose::Meta::Method::Destructor>
73
74=cut