Require Dist::Zilla 4.200016+
[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 die $_;
96 };
97
c989424b 98 return;
c0e30cf5 99}
100
e606ae5f 101# support for UNIVERSAL::DOES ...
102BEGIN {
103 my $does = UNIVERSAL->can("DOES") ? "SUPER::DOES" : "isa";
104 eval 'sub DOES {
105 my ( $self, $class_or_role_name ) = @_;
106 return $self->'.$does.'($class_or_role_name)
107 || $self->does($class_or_role_name);
108 }';
109}
110
d03bd989 111# new does() methods will be created
50bc108b 112# as appropiate see Moose::Meta::Role
0677220d 113sub does {
bdabd620 114 my ($self, $role_name) = @_;
6d87e241 115 my $class = Scalar::Util::blessed($self) || $self;
116 my $meta = Class::MOP::Class->initialize($class);
be05faea 117 (defined $role_name)
d9de4f04 118 || $meta->throw_error("You must supply a role name to does()");
b82ef9c3 119 return 1 if $meta->can('does_role') && $meta->does_role($role_name);
d03bd989 120 return 0;
0677220d 121}
ef333f17 122
d03bd989 123sub dump {
f742dfef 124 my $self = shift;
125 require Data::Dumper;
1a386a6c 126 local $Data::Dumper::Maxdepth = shift if @_;
f742dfef 127 Data::Dumper::Dumper $self;
128}
129
fcd84ca9 1301;
131
ad46f524 132# ABSTRACT: The base object for Moose
133
fcd84ca9 134__END__
135
136=pod
137
fcd84ca9 138=head1 DESCRIPTION
139
080e3614 140This class is the default base class for all Moose-using classes. When
141you C<use Moose> in this class, your class will inherit from this
142class.
6ba6d68c 143
2dc42d42 144It provides a default constructor and destructor, which run all of the
145C<BUILD> and C<DEMOLISH> methods in the inheritance hierarchy,
146respectively.
080e3614 147
148You don't actually I<need> to inherit from this in order to use Moose,
149but it makes it easier to take advantage of all of Moose's features.
6ba6d68c 150
fcd84ca9 151=head1 METHODS
152
153=over 4
154
d81ecac9 155=item B<< Moose::Object->new(%params|$params) >>
080e3614 156
157This method calls C<< $class->BUILDARGS(@_) >>, and then creates a new
158instance of the appropriate class. Once the instance is created, it
2dc42d42 159calls C<< $instance->BUILD($params) >> for each C<BUILD> method in the
160inheritance hierarchy.
fcd84ca9 161
d81ecac9 162=item B<< Moose::Object->BUILDARGS(%params|$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->does($role_name) >>
ef333f17 174
080e3614 175This returns true if the object does the given role.
02a0fb52 176
d81ecac9 177=item B<< $object->DOES($class_or_role_name) >>
e606ae5f 178
080e3614 179This is a a Moose role-aware implementation of L<UNIVERSAL/DOES>.
e606ae5f 180
080e3614 181This is effectively the same as writing:
e606ae5f 182
080e3614 183 $object->does($name) || $object->isa($name)
f742dfef 184
080e3614 185This method will work with Perl 5.8, which did not implement
186C<UNIVERSAL::DOES>.
f742dfef 187
080e3614 188=item B<< $object->dump($maxdepth) >>
f742dfef 189
080e3614 190This is a handy utility for C<Data::Dumper>ing an object. By default,
191the maximum depth is 1, to avoid making a mess.
f742dfef 192
2dc42d42 193=item B<< $object->DESTROY >>
194
195A default destructor is provided, which calls
196C<< $instance->DEMOLISH($in_global_destruction) >> for each C<DEMOLISH>
197method in the inheritance hierarchy.
198
fcd84ca9 199=back
200
201=head1 BUGS
202
d4048ef3 203See L<Moose/BUGS> for details on reporting bugs.
fcd84ca9 204
9922fa6f 205=cut