Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / Moose / Meta / Method / Destructor.pm
CommitLineData
3fea05b9 1
2package Moose::Meta::Method::Destructor;
3
4use strict;
5use warnings;
6
7use Devel::GlobalDestruction ();
8use Scalar::Util 'blessed', 'weaken';
9use Try::Tiny ();
10
11our $VERSION = '0.93';
12$VERSION = eval $VERSION;
13our $AUTHORITY = 'cpan:STEVAN';
14
15use base 'Moose::Meta::Method',
16 'Class::MOP::Method::Inlined';
17
18sub new {
19 my $class = shift;
20 my %options = @_;
21
22 (ref $options{options} eq 'HASH')
23 || $class->throw_error("You must pass a hash of options", data => $options{options});
24
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");
27
28 my $self = bless {
29 # from our superclass
30 'body' => undef,
31 'package_name' => $options{package_name},
32 'name' => $options{name},
33 # ...
34 'options' => $options{options},
35 'associated_metaclass' => $options{metaclass},
36 } => $class;
37
38 # we don't want this creating
39 # a cycle in the code, if not
40 # needed
41 weaken($self->{'associated_metaclass'});
42
43 $self->_initialize_body;
44
45 return $self;
46}
47
48## accessors
49
50sub options { (shift)->{'options'} }
51
52## method
53
54sub is_needed {
55 my $self = shift;
56 my $metaclass = shift;
57
58 ( blessed $metaclass && $metaclass->isa('Class::MOP::Class') )
59 || $self->throw_error(
60 "The is_needed method expected a metaclass object as its arugment");
61
62 return $metaclass->find_method_by_name("DEMOLISHALL");
63}
64
65sub initialize_body {
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;
69}
70
71sub _initialize_body {
72 my $self = shift;
73 # TODO:
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)
80
81 my @DEMOLISH_methods = $self->associated_metaclass->find_all_methods_by_name('DEMOLISH');
82
83 my $source;
84 if ( @DEMOLISH_methods ) {
85 $source = 'sub {';
86 $source .= 'my $self = shift;' . "\n";
87
88 $source .= 'local $?;' . "\n";
89
90 $source .= 'my $in_global_destruction = Devel::GlobalDestruction::in_global_destruction;' . "\n";
91
92 $source .= 'Try::Tiny::try {' . "\n";
93
94 $source .= '$self->' . $_->{class} . '::DEMOLISH($in_global_destruction);' . "\n"
95 for @DEMOLISH_methods;
96
97 $source .= '}';
98 $source .= q[ Try::Tiny::catch { no warnings 'misc'; die $_ };] . "\n";
99 $source .= 'return;' . "\n";
100
101 $source .= '}';
102 } else {
103 $source = 'sub { }';
104 }
105
106 warn $source if $self->options->{debug};
107
108 my ( $code, $e ) = $self->_compile_code(
109 environment => {},
110 code => $source,
111 );
112
113 $self->throw_error(
114 "Could not eval the destructor :\n\n$source\n\nbecause :\n\n$e",
115 error => $e, data => $source )
116 if $e;
117
118 $self->{'body'} = $code;
119}
120
121
1221;
123
124__END__
125
126=pod
127
128=head1 NAME
129
130Moose::Meta::Method::Destructor - Method Meta Object for destructors
131
132=head1 DESCRIPTION
133
134This class is a subclass of L<Class::MOP::Class::Generated> that
135provides Moose-specific functionality for inlining destructors.
136
137To understand this class, you should read the the
138L<Class::MOP::Class::Generated> documentation as well.
139
140=head1 INHERITANCE
141
142C<Moose::Meta::Method::Destructor> is a subclass of
143L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Generated>.
144
145=head1 METHODS
146
147=over 4
148
149=item B<< Moose::Meta;:Method::Destructor->new(%options) >>
150
151This constructs a new object. It accepts the following options:
152
153=over 8
154
155=item * package_name
156
157The package for the class in which the destructor is being
158inlined. This option is required.
159
160=item * name
161
162The name of the destructor method. This option is required.
163
164=item * metaclass
165
166The metaclass for the class this destructor belongs to. This is
167optional, as it can be set later by calling C<<
168$metamethod->attach_to_class >>.
169
170=back
171
172=item B<< Moose::Meta;:Method::Destructor->is_needed($metaclass) >>
173
174Given a L<Moose::Meta::Class> object, this method returns a boolean
175indicating whether the class needs a destructor. If the class or any
176of its parents defines a C<DEMOLISH> method, it needs a destructor.
177
178=back
179
180=head1 AUTHORS
181
182Stevan Little E<lt>stevan@iinteractive.comE<gt>
183
184=head1 COPYRIGHT AND LICENSE
185
186Copyright 2006-2009 by Infinity Interactive, Inc.
187
188L<http://www.iinteractive.com>
189
190This library is free software; you can redistribute it and/or modify
191it under the same terms as Perl itself.
192
193=cut
194