2 package Moose::Meta::Method::Destructor;
7 use Devel::GlobalDestruction ();
8 use Scalar::Util 'blessed', 'weaken';
11 our $VERSION = '1.19';
12 $VERSION = eval $VERSION;
13 our $AUTHORITY = 'cpan:STEVAN';
15 use base 'Moose::Meta::Method',
16 'Class::MOP::Method::Inlined';
22 (ref $options{options} eq 'HASH')
23 || $class->throw_error("You must pass a hash of options", data => $options{options});
25 ($options{package_name} && $options{name})
26 || $class->throw_error("You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT");
31 'package_name' => $options{package_name},
32 'name' => $options{name},
34 'options' => $options{options},
35 'associated_metaclass' => $options{metaclass},
38 # we don't want this creating
39 # a cycle in the code, if not
41 weaken($self->{'associated_metaclass'});
43 $self->_initialize_body;
50 sub options { (shift)->{'options'} }
56 my $metaclass = shift;
58 ( blessed $metaclass && $metaclass->isa('Class::MOP::Class') )
59 || $self->throw_error(
60 "The is_needed method expected a metaclass object as its arugment");
62 return $metaclass->find_method_by_name("DEMOLISHALL");
66 Carp::cluck('The initialize_body method has been made private.'
67 . " The public version is deprecated and will be removed in a future release.\n");
68 shift->_initialize_body;
71 sub _initialize_body {
74 # the %options should also include a both
75 # a call 'initializer' and call 'SUPER::'
76 # options, which should cover approx 90%
77 # of the possible use cases (even if it
78 # requires some adaption on the part of
79 # the author, after all, nothing is free)
81 my @DEMOLISH_methods = $self->associated_metaclass->find_all_methods_by_name('DEMOLISH');
84 $source = 'sub {' . "\n";
85 $source .= 'my $self = shift;' . "\n";
86 $source .= 'return $self->Moose::Object::DESTROY(@_)' . "\n";
87 $source .= ' if Scalar::Util::blessed($self) ne ';
88 $source .= "'" . $self->associated_metaclass->name . "'";
89 $source .= ';' . "\n";
91 if ( @DEMOLISH_methods ) {
92 $source .= 'local $?;' . "\n";
94 $source .= 'my $in_global_destruction = Devel::GlobalDestruction::in_global_destruction;' . "\n";
96 $source .= 'Try::Tiny::try {' . "\n";
98 $source .= '$self->' . $_->{class} . '::DEMOLISH($in_global_destruction);' . "\n"
99 for @DEMOLISH_methods;
102 $source .= q[ Try::Tiny::catch { no warnings 'misc'; die $_ };] . "\n";
103 $source .= 'return;' . "\n";
109 warn $source if $self->options->{debug};
112 $self->_compile_code(source => $source);
116 "Could not eval the destructor :\n\n$source\n\nbecause :\n\n$_",
122 $self->{'body'} = $code;
134 Moose::Meta::Method::Destructor - Method Meta Object for destructors
138 This class is a subclass of L<Class::MOP::Class::Generated> that
139 provides Moose-specific functionality for inlining destructors.
141 To understand this class, you should read the the
142 L<Class::MOP::Class::Generated> documentation as well.
146 C<Moose::Meta::Method::Destructor> is a subclass of
147 L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Generated>.
153 =item B<< Moose::Meta::Method::Destructor->new(%options) >>
155 This constructs a new object. It accepts the following options:
161 The package for the class in which the destructor is being
162 inlined. This option is required.
166 The name of the destructor method. This option is required.
170 The metaclass for the class this destructor belongs to. This is
171 optional, as it can be set later by calling C<<
172 $metamethod->attach_to_class >>.
176 =item B<< Moose::Meta;:Method::Destructor->is_needed($metaclass) >>
178 Given a L<Moose::Meta::Class> object, this method returns a boolean
179 indicating whether the class needs a destructor. If the class or any
180 of its parents defines a C<DEMOLISH> method, it needs a destructor.
186 See L<Moose/BUGS> for details on reporting bugs.
190 Stevan Little E<lt>stevan@iinteractive.comE<gt>
192 =head1 COPYRIGHT AND LICENSE
194 Copyright 2006-2010 by Infinity Interactive, Inc.
196 L<http://www.iinteractive.com>
198 This library is free software; you can redistribute it and/or modify
199 it under the same terms as Perl itself.