Skip BUILDALL in the default constructor, and DEMOLISHALL in the default destructor:
[gitmo/Mouse.git] / lib / Mouse / Object.pm
1 package Mouse::Object;
2 use Mouse::Util qw(does dump); # enables strict and warnings
3
4 sub new {
5     my $class = shift;
6
7     $class->meta->throw_error('Cannot call new() on an instance') if ref $class;
8
9     my $args = $class->BUILDARGS(@_);
10
11     my $meta = Mouse::Meta::Class->initialize($class);
12     my $self = $meta->new_object($args);
13
14     # BUILDALL
15     if( $self->can('BUILD') ) {
16         for my $class (reverse $meta->linearized_isa) {
17             my $build = Mouse::Util::get_code_ref($class, 'BUILD')
18                 || next;
19
20             $self->$build($args);
21         }
22     }
23
24     return $self;
25 }
26
27 sub BUILDARGS {
28     my $class = shift;
29
30     if (scalar @_ == 1) {
31         (ref($_[0]) eq 'HASH')
32             || $class->meta->throw_error("Single parameters to new() must be a HASH ref");
33
34         return {%{$_[0]}};
35     }
36     else {
37         return {@_};
38     }
39 }
40
41 sub DESTROY {
42     my $self = shift;
43
44     return unless $self->can('DEMOLISH'); # short circuit
45
46     local $?;
47
48     my $e = do{
49         local $@;
50         eval{
51
52             # DEMOLISHALL
53
54             # We cannot count on being able to retrieve a previously made
55             # metaclass, _or_ being able to make a new one during global
56             # destruction. However, we should still be able to use mro at
57             # that time (at least tests suggest so ;)
58
59             foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) {
60                 my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH')
61                     || next;
62
63                 $self->$demolish();
64             }
65         };
66         $@;
67     };
68
69     no warnings 'misc';
70     die $e if $e; # rethrow
71 }
72
73 sub BUILDALL {
74     my $self = shift;
75
76     # short circuit
77     return unless $self->can('BUILD');
78
79     for my $class (reverse $self->meta->linearized_isa) {
80         my $build = Mouse::Util::get_code_ref($class, 'BUILD')
81             || next;
82
83         $self->$build(@_);
84     }
85     return;
86 }
87
88 sub DEMOLISHALL {
89     my $self = shift;
90
91     # short circuit
92     return unless $self->can('DEMOLISH');
93
94     # We cannot count on being able to retrieve a previously made
95     # metaclass, _or_ being able to make a new one during global
96     # destruction. However, we should still be able to use mro at
97     # that time (at least tests suggest so ;)
98
99     foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) {
100         my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH')
101             || next;
102
103         $self->$demolish();
104     }
105     return;
106 }
107
108 1;
109
110 __END__
111
112 =head1 NAME
113
114 Mouse::Object - The base object for Mouse classes
115
116 =head1 VERSION
117
118 This document describes Mouse version 0.40_05
119
120 =head1 METHODS
121
122 =head2 C<< new (Arguments) -> Object >>
123
124 Instantiates a new C<Mouse::Object>. This is obviously intended for subclasses.
125
126 =head2 C<< BUILDARGS (Arguments) -> HashRef >>
127
128 Lets you override the arguments that C<new> takes. Return a hashref of
129 parameters.
130
131 =head2 C<< BUILDALL (\%args) >>
132
133 Calls C<BUILD> on each class in the class hierarchy. This is called at the
134 end of C<new>.
135
136 =head2 C<< BUILD (\%args) >>
137
138 You may put any business logic initialization in BUILD methods. You don't
139 need to redispatch or return any specific value.
140
141 =head2 C<< DEMOLISHALL >>
142
143 Calls C<DEMOLISH> on each class in the class hierarchy. This is called at
144 C<DESTROY> time.
145
146 =head2 C<< DEMOLISH >>
147
148 You may put any business logic deinitialization in DEMOLISH methods. You don't
149 need to redispatch or return any specific value.
150
151
152 =head2 C<< does ($role_name) -> Bool >>
153
154 This will check if the invocant's class B<does> a given C<$role_name>.
155 This is similar to "isa" for object, but it checks the roles instead.
156
157 =head2 C<<dump ($maxdepth) -> Str >>
158
159 From the Moose POD:
160
161     C'mon, how many times have you written the following code while debugging:
162
163      use Data::Dumper; 
164      warn Dumper $obj;
165
166     It can get seriously annoying, so why not just use this.
167
168 The implementation was lifted directly from Moose::Object.
169
170 =head1 SEE ALSO
171
172 L<Moose::Object>
173
174 =cut
175