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) { |
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 | ||
65 | sub 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 | ||
88 | 1; | |
89 | ||
90 | __END__ | |
91 | ||
92 | =head1 NAME | |
93 | ||
bedd575c | 94 | Mouse::Object - The base object for Mouse classes |
c3398f5b | 95 | |
a25ca8d6 | 96 | =head1 VERSION |
97 | ||
034587d8 | 98 | This document describes Mouse version 0.40 |
a25ca8d6 | 99 | |
c3398f5b | 100 | =head1 METHODS |
101 | ||
31c5194b | 102 | =head2 C<< new (Arguments) -> Object >> |
c3398f5b | 103 | |
1820fffe | 104 | Instantiates a new C<Mouse::Object>. This is obviously intended for subclasses. |
c3398f5b | 105 | |
31c5194b | 106 | =head2 C<< BUILDARGS (Arguments) -> HashRef >> |
c3398f5b | 107 | |
1820fffe | 108 | Lets you override the arguments that C<new> takes. Return a hashref of |
109 | parameters. | |
c3398f5b | 110 | |
31c5194b | 111 | =head2 C<< BUILDALL (\%args) >> |
c3398f5b | 112 | |
1820fffe | 113 | Calls C<BUILD> on each class in the class hierarchy. This is called at the |
114 | end of C<new>. | |
c3398f5b | 115 | |
31c5194b | 116 | =head2 C<< BUILD (\%args) >> |
442125dc | 117 | |
1820fffe | 118 | You may put any business logic initialization in BUILD methods. You don't |
119 | need to redispatch or return any specific value. | |
442125dc | 120 | |
1820fffe | 121 | =head2 C<< DEMOLISHALL >> |
c3398f5b | 122 | |
1820fffe | 123 | Calls C<DEMOLISH> on each class in the class hierarchy. This is called at |
124 | C<DESTROY> time. | |
c3398f5b | 125 | |
1820fffe | 126 | =head2 C<< DEMOLISH >> |
c3398f5b | 127 | |
128 | You may put any business logic deinitialization in DEMOLISH methods. You don't | |
129 | need to redispatch or return any specific value. | |
130 | ||
df963a63 | 131 | |
1820fffe | 132 | =head2 C<< does ($role_name) -> Bool >> |
56a558f9 | 133 | |
1820fffe | 134 | This will check if the invocant's class B<does> a given C<$role_name>. |
56a558f9 | 135 | This is similar to "isa" for object, but it checks the roles instead. |
136 | ||
1820fffe | 137 | =head2 C<<dump ($maxdepth) -> Str >> |
df963a63 | 138 | |
139 | From 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 | ||
148 | The implementation was lifted directly from Moose::Object. | |
149 | ||
1820fffe | 150 | =head1 SEE ALSO |
151 | ||
152 | L<Moose::Object> | |
c3398f5b | 153 | |
1820fffe | 154 | =cut |
df963a63 | 155 |