Commit | Line | Data |
---|---|---|
c3398f5b | 1 | package Mouse::Object; |
bc69ee88 | 2 | use Mouse::Util qw(does dump); # enables strict and warnings |
6d28c5cf | 3 | |
c3398f5b | 4 | sub 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 | |
fce211ae | 11 | my $instance = Mouse::Meta::Class->initialize($class)->new_object($args); |
d574882a | 12 | $instance->BUILDALL($args); |
c3398f5b | 13 | return $instance; |
14 | } | |
15 | ||
d574882a | 16 | sub 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 | 30 | sub 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 | |
47 | sub 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) { |
5124b717 | 54 | my $build = Mouse::Util::get_code_ref($class, 'BUILD') |
55 | || next; | |
7ca5c5fb | 56 | |
57 | $self->$build(@_); | |
c3398f5b | 58 | } |
3a63a2e7 | 59 | return; |
c3398f5b | 60 | } |
61 | ||
62 | sub DEMOLISHALL { | |
63 | my $self = shift; | |
64 | ||
65 | # short circuit | |
66 | return unless $self->can('DEMOLISH'); | |
67 | ||
8536d351 | 68 | # We cannot count on being able to retrieve a previously made |
69 | # metaclass, _or_ being able to make a new one during global | |
70 | # destruction. However, we should still be able to use mro at | |
71 | # that time (at least tests suggest so ;) | |
c26e296a | 72 | |
8536d351 | 73 | foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) { |
5124b717 | 74 | my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH') |
75 | || next; | |
7ca5c5fb | 76 | |
77 | $self->$demolish(); | |
c3398f5b | 78 | } |
3a63a2e7 | 79 | return; |
c3398f5b | 80 | } |
81 | ||
82 | 1; | |
83 | ||
84 | __END__ | |
85 | ||
86 | =head1 NAME | |
87 | ||
bedd575c | 88 | Mouse::Object - The base object for Mouse classes |
c3398f5b | 89 | |
a25ca8d6 | 90 | =head1 VERSION |
91 | ||
b06ce1f5 | 92 | This document describes Mouse version 0.40_01 |
a25ca8d6 | 93 | |
c3398f5b | 94 | =head1 METHODS |
95 | ||
31c5194b | 96 | =head2 C<< new (Arguments) -> Object >> |
c3398f5b | 97 | |
1820fffe | 98 | Instantiates a new C<Mouse::Object>. This is obviously intended for subclasses. |
c3398f5b | 99 | |
31c5194b | 100 | =head2 C<< BUILDARGS (Arguments) -> HashRef >> |
c3398f5b | 101 | |
1820fffe | 102 | Lets you override the arguments that C<new> takes. Return a hashref of |
103 | parameters. | |
c3398f5b | 104 | |
31c5194b | 105 | =head2 C<< BUILDALL (\%args) >> |
c3398f5b | 106 | |
1820fffe | 107 | Calls C<BUILD> on each class in the class hierarchy. This is called at the |
108 | end of C<new>. | |
c3398f5b | 109 | |
31c5194b | 110 | =head2 C<< BUILD (\%args) >> |
442125dc | 111 | |
1820fffe | 112 | You may put any business logic initialization in BUILD methods. You don't |
113 | need to redispatch or return any specific value. | |
442125dc | 114 | |
1820fffe | 115 | =head2 C<< DEMOLISHALL >> |
c3398f5b | 116 | |
1820fffe | 117 | Calls C<DEMOLISH> on each class in the class hierarchy. This is called at |
118 | C<DESTROY> time. | |
c3398f5b | 119 | |
1820fffe | 120 | =head2 C<< DEMOLISH >> |
c3398f5b | 121 | |
122 | You may put any business logic deinitialization in DEMOLISH methods. You don't | |
123 | need to redispatch or return any specific value. | |
124 | ||
df963a63 | 125 | |
1820fffe | 126 | =head2 C<< does ($role_name) -> Bool >> |
56a558f9 | 127 | |
1820fffe | 128 | This will check if the invocant's class B<does> a given C<$role_name>. |
56a558f9 | 129 | This is similar to "isa" for object, but it checks the roles instead. |
130 | ||
1820fffe | 131 | =head2 C<<dump ($maxdepth) -> Str >> |
df963a63 | 132 | |
133 | From the Moose POD: | |
134 | ||
135 | C'mon, how many times have you written the following code while debugging: | |
136 | ||
137 | use Data::Dumper; | |
138 | warn Dumper $obj; | |
139 | ||
140 | It can get seriously annoying, so why not just use this. | |
141 | ||
142 | The implementation was lifted directly from Moose::Object. | |
143 | ||
1820fffe | 144 | =head1 SEE ALSO |
145 | ||
146 | L<Moose::Object> | |
c3398f5b | 147 | |
1820fffe | 148 | =cut |
df963a63 | 149 |