fix up a few spelling and pod issues
[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
2dc42d42 143It provides a default constructor and destructor, which run all of the
144C<BUILD> and C<DEMOLISH> methods in the inheritance hierarchy,
145respectively.
080e3614 146
147You don't actually I<need> to inherit from this in order to use Moose,
148but it makes it easier to take advantage of all of Moose's features.
6ba6d68c 149
fcd84ca9 150=head1 METHODS
151
152=over 4
153
d81ecac9 154=item B<< Moose::Object->new(%params|$params) >>
080e3614 155
156This method calls C<< $class->BUILDARGS(@_) >>, and then creates a new
157instance of the appropriate class. Once the instance is created, it
2dc42d42 158calls C<< $instance->BUILD($params) >> for each C<BUILD> method in the
159inheritance hierarchy.
fcd84ca9 160
d81ecac9 161=item B<< Moose::Object->BUILDARGS(%params|$params) >>
6ba6d68c 162
080e3614 163The default implementation of this method accepts a hash or hash
164reference of named parameters. If it receives a single argument that
165I<isn't> a hash reference it throws an error.
fcd84ca9 166
080e3614 167You can override this method in your class to handle other types of
168options passed to the constructor.
e606ae5f 169
080e3614 170This method should always return a hash reference of named options.
e606ae5f 171
080e3614 172=item B<< $object->does($role_name) >>
ef333f17 173
080e3614 174This returns true if the object does the given role.
02a0fb52 175
d81ecac9 176=item B<< $object->DOES($class_or_role_name) >>
e606ae5f 177
080e3614 178This is a a Moose role-aware implementation of L<UNIVERSAL/DOES>.
e606ae5f 179
080e3614 180This is effectively the same as writing:
e606ae5f 181
080e3614 182 $object->does($name) || $object->isa($name)
f742dfef 183
080e3614 184This method will work with Perl 5.8, which did not implement
185C<UNIVERSAL::DOES>.
f742dfef 186
080e3614 187=item B<< $object->dump($maxdepth) >>
f742dfef 188
080e3614 189This is a handy utility for C<Data::Dumper>ing an object. By default,
190the maximum depth is 1, to avoid making a mess.
f742dfef 191
2dc42d42 192=item B<< $object->DESTROY >>
193
194A default destructor is provided, which calls
195C<< $instance->DEMOLISH($in_global_destruction) >> for each C<DEMOLISH>
196method in the inheritance hierarchy.
197
fcd84ca9 198=back
199
200=head1 BUGS
201
d4048ef3 202See L<Moose/BUGS> for details on reporting bugs.
fcd84ca9 203
9922fa6f 204=cut