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