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