Use dzil Authority plugin - remove $AUTHORITY from code
[gitmo/Moose.git] / lib / Moose / Object.pm
CommitLineData
fcd84ca9 1
2package Moose::Object;
3
4use strict;
5use warnings;
648e79ae 6
0d922627 7use Carp ();
f25f9912 8use Devel::GlobalDestruction ();
9use MRO::Compat ();
10use Scalar::Util ();
11use Try::Tiny ();
2eaf25da 12
9922fa6f 13use if ( not our $__mx_is_compiled ), 'Moose::Meta::Class';
14use if ( not our $__mx_is_compiled ), metaclass => 'Moose::Meta::Class';
bc1e29b5 15
fcd84ca9 16sub new {
2c0cbef7 17 my $class = shift;
f25f9912 18 my $real_class = Scalar::Util::blessed($class) || $class;
2eaf25da 19
a19ae3d7 20 my $params = $real_class->BUILDARGS(@_);
2eaf25da 21
a19ae3d7 22 return Class::MOP::Class->initialize($real_class)->new_object($params);
e606ae5f 23}
24
25sub BUILDARGS {
26 my $class = shift;
a62dcd43 27 if ( scalar @_ == 1 ) {
28 unless ( defined $_[0] && ref $_[0] eq 'HASH' ) {
46047bea 29 Class::MOP::class_of($class)->throw_error(
a62dcd43 30 "Single parameters to new() must be a HASH ref",
31 data => $_[0] );
86629f93 32 }
a62dcd43 33 return { %{ $_[0] } };
34 }
0d922627 35 elsif ( @_ % 2 ) {
36 Carp::carp(
37 "The new() method for $class expects a hash reference or a key/value list."
38 . " You passed an odd number of arguments" );
39 return { @_, undef };
40 }
8a157bab 41 else {
e606ae5f 42 return {@_};
8a157bab 43 }
fcd84ca9 44}
45
c0e30cf5 46sub BUILDALL {
d03bd989 47 # NOTE: we ask Perl if we even
d44714be 48 # need to do this first, to avoid
49 # extra meta level calls
17819f17 50 return unless $_[0]->can('BUILD');
fb1e11d5 51 my ($self, $params) = @_;
505033d9 52 foreach my $method (reverse Class::MOP::class_of($self)->find_all_methods_by_name('BUILD')) {
b8f014e7 53 $method->{code}->execute($self, $params);
fb1e11d5 54 }
c0e30cf5 55}
56
57sub DEMOLISHALL {
8955a780 58 my $self = shift;
d9359278 59 my ($in_global_destruction) = @_;
8955a780 60
61 # NOTE: we ask Perl if we even
cb5c79c9 62 # need to do this first, to avoid
8955a780 63 # extra meta level calls
17819f17 64 return unless $self->can('DEMOLISH');
8955a780 65
2bc1eab6 66 my @isa;
67 if ( my $meta = Class::MOP::class_of($self ) ) {
68 @isa = $meta->linearized_isa;
69 } else {
70 # We cannot count on being able to retrieve a previously made
71 # metaclass, _or_ being able to make a new one during global
72 # destruction. However, we should still be able to use mro at
73 # that time (at least tests suggest so ;)
74 my $class_name = ref $self;
75 @isa = @{ mro::get_linear_isa($class_name) }
76 }
77
78 foreach my $class (@isa) {
c063fc8b 79 no strict 'refs';
80 my $demolish = *{"${class}::DEMOLISH"}{CODE};
81 $self->$demolish($in_global_destruction)
82 if defined $demolish;
3a0c064a 83 }
84}
85
d03bd989 86sub DESTROY {
b288593e 87 my $self = shift;
88
89 local $?;
90
f25f9912 91 Try::Tiny::try {
92 $self->DEMOLISHALL(Devel::GlobalDestruction::in_global_destruction);
b288593e 93 }
f25f9912 94 Try::Tiny::catch {
b288593e 95 # Without this, Perl will warn "\t(in cleanup)$@" because of some
96 # bizarre fucked-up logic deep in the internals.
97 no warnings 'misc';
98 die $_;
99 };
100
c989424b 101 return;
c0e30cf5 102}
103
e606ae5f 104# support for UNIVERSAL::DOES ...
105BEGIN {
106 my $does = UNIVERSAL->can("DOES") ? "SUPER::DOES" : "isa";
107 eval 'sub DOES {
108 my ( $self, $class_or_role_name ) = @_;
109 return $self->'.$does.'($class_or_role_name)
110 || $self->does($class_or_role_name);
111 }';
112}
113
d03bd989 114# new does() methods will be created
50bc108b 115# as appropiate see Moose::Meta::Role
0677220d 116sub does {
bdabd620 117 my ($self, $role_name) = @_;
66d33a5c 118 my $meta = Class::MOP::class_of($self);
be05faea 119 (defined $role_name)
d9de4f04 120 || $meta->throw_error("You must supply a role name to does()");
b82ef9c3 121 return 1 if $meta->can('does_role') && $meta->does_role($role_name);
d03bd989 122 return 0;
0677220d 123}
ef333f17 124
d03bd989 125sub dump {
f742dfef 126 my $self = shift;
127 require Data::Dumper;
1a386a6c 128 local $Data::Dumper::Maxdepth = shift if @_;
f742dfef 129 Data::Dumper::Dumper $self;
130}
131
fcd84ca9 1321;
133
ad46f524 134# ABSTRACT: The base object for Moose
135
fcd84ca9 136__END__
137
138=pod
139
fcd84ca9 140=head1 DESCRIPTION
141
080e3614 142This class is the default base class for all Moose-using classes. When
143you C<use Moose> in this class, your class will inherit from this
144class.
6ba6d68c 145
080e3614 146It provides a default constructor and destructor, which run the
147C<BUILDALL> and C<DEMOLISHALL> methods respectively.
148
149You don't actually I<need> to inherit from this in order to use Moose,
150but it makes it easier to take advantage of all of Moose's features.
6ba6d68c 151
fcd84ca9 152=head1 METHODS
153
154=over 4
155
080e3614 156=item B<< Moose::Object->new(%params) >>
157
158This method calls C<< $class->BUILDARGS(@_) >>, and then creates a new
159instance of the appropriate class. Once the instance is created, it
160calls C<< $instance->BUILDALL($params) >>.
fcd84ca9 161
080e3614 162=item B<< Moose::Object->BUILDARGS(%params) >>
6ba6d68c 163
080e3614 164The default implementation of this method accepts a hash or hash
165reference of named parameters. If it receives a single argument that
166I<isn't> a hash reference it throws an error.
fcd84ca9 167
080e3614 168You can override this method in your class to handle other types of
169options passed to the constructor.
e606ae5f 170
080e3614 171This method should always return a hash reference of named options.
e606ae5f 172
080e3614 173=item B<< $object->BUILDALL($params) >>
e522431d 174
080e3614 175This method will call every C<BUILD> method in the inheritance
176hierarchy, starting with the most distant parent class and ending with
177the object's class.
c0e30cf5 178
080e3614 179The C<BUILD> method will be passed the hash reference returned by
180C<BUILDARGS>.
e522431d 181
080e3614 182=item B<< $object->DEMOLISHALL >>
c0e30cf5 183
080e3614 184This will call every C<DEMOLISH> method in the inheritance hierarchy,
185starting with the object's class and ending with the most distant
c3fdacda 186parent. C<DEMOLISHALL> and C<DEMOLISH> will receive a boolean
187indicating whether or not we are currently in global destruction.
e522431d 188
080e3614 189=item B<< $object->does($role_name) >>
ef333f17 190
080e3614 191This returns true if the object does the given role.
02a0fb52 192
e606ae5f 193=item B<DOES ($class_or_role_name)>
194
080e3614 195This is a a Moose role-aware implementation of L<UNIVERSAL/DOES>.
e606ae5f 196
080e3614 197This is effectively the same as writing:
e606ae5f 198
080e3614 199 $object->does($name) || $object->isa($name)
f742dfef 200
080e3614 201This method will work with Perl 5.8, which did not implement
202C<UNIVERSAL::DOES>.
f742dfef 203
080e3614 204=item B<< $object->dump($maxdepth) >>
f742dfef 205
080e3614 206This is a handy utility for C<Data::Dumper>ing an object. By default,
207the maximum depth is 1, to avoid making a mess.
f742dfef 208
fcd84ca9 209=back
210
211=head1 BUGS
212
d4048ef3 213See L<Moose/BUGS> for details on reporting bugs.
fcd84ca9 214
9922fa6f 215=cut