but with this, perl won't do anything at all
[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) = @_;
66d33a5c 115 my $meta = Class::MOP::class_of($self);
be05faea 116 (defined $role_name)
d9de4f04 117 || $meta->throw_error("You must supply a role name to does()");
b82ef9c3 118 return 1 if $meta->can('does_role') && $meta->does_role($role_name);
d03bd989 119 return 0;
0677220d 120}
ef333f17 121
d03bd989 122sub dump {
f742dfef 123 my $self = shift;
124 require Data::Dumper;
1a386a6c 125 local $Data::Dumper::Maxdepth = shift if @_;
f742dfef 126 Data::Dumper::Dumper $self;
127}
128
fcd84ca9 1291;
130
ad46f524 131# ABSTRACT: The base object for Moose
132
fcd84ca9 133__END__
134
135=pod
136
fcd84ca9 137=head1 DESCRIPTION
138
080e3614 139This class is the default base class for all Moose-using classes. When
140you C<use Moose> in this class, your class will inherit from this
141class.
6ba6d68c 142
080e3614 143It provides a default constructor and destructor, which run the
144C<BUILDALL> and C<DEMOLISHALL> methods respectively.
145
146You don't actually I<need> to inherit from this in order to use Moose,
147but it makes it easier to take advantage of all of Moose's features.
6ba6d68c 148
fcd84ca9 149=head1 METHODS
150
151=over 4
152
080e3614 153=item B<< Moose::Object->new(%params) >>
154
155This method calls C<< $class->BUILDARGS(@_) >>, and then creates a new
156instance of the appropriate class. Once the instance is created, it
157calls C<< $instance->BUILDALL($params) >>.
fcd84ca9 158
080e3614 159=item B<< Moose::Object->BUILDARGS(%params) >>
6ba6d68c 160
080e3614 161The default implementation of this method accepts a hash or hash
162reference of named parameters. If it receives a single argument that
163I<isn't> a hash reference it throws an error.
fcd84ca9 164
080e3614 165You can override this method in your class to handle other types of
166options passed to the constructor.
e606ae5f 167
080e3614 168This method should always return a hash reference of named options.
e606ae5f 169
080e3614 170=item B<< $object->BUILDALL($params) >>
e522431d 171
080e3614 172This method will call every C<BUILD> method in the inheritance
173hierarchy, starting with the most distant parent class and ending with
174the object's class.
c0e30cf5 175
080e3614 176The C<BUILD> method will be passed the hash reference returned by
177C<BUILDARGS>.
e522431d 178
080e3614 179=item B<< $object->DEMOLISHALL >>
c0e30cf5 180
080e3614 181This will call every C<DEMOLISH> method in the inheritance hierarchy,
182starting with the object's class and ending with the most distant
c3fdacda 183parent. C<DEMOLISHALL> and C<DEMOLISH> will receive a boolean
184indicating whether or not we are currently in global destruction.
e522431d 185
080e3614 186=item B<< $object->does($role_name) >>
ef333f17 187
080e3614 188This returns true if the object does the given role.
02a0fb52 189
e606ae5f 190=item B<DOES ($class_or_role_name)>
191
080e3614 192This is a a Moose role-aware implementation of L<UNIVERSAL/DOES>.
e606ae5f 193
080e3614 194This is effectively the same as writing:
e606ae5f 195
080e3614 196 $object->does($name) || $object->isa($name)
f742dfef 197
080e3614 198This method will work with Perl 5.8, which did not implement
199C<UNIVERSAL::DOES>.
f742dfef 200
080e3614 201=item B<< $object->dump($maxdepth) >>
f742dfef 202
080e3614 203This is a handy utility for C<Data::Dumper>ing an object. By default,
204the maximum depth is 1, to avoid making a mess.
f742dfef 205
fcd84ca9 206=back
207
208=head1 BUGS
209
d4048ef3 210See L<Moose/BUGS> for details on reporting bugs.
fcd84ca9 211
9922fa6f 212=cut