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