9 use if ( not our $__mx_is_compiled ), 'Moose::Meta::Class';
10 use if ( not our $__mx_is_compiled ), metaclass => 'Moose::Meta::Class';
12 our $VERSION = '0.76';
13 $VERSION = eval $VERSION;
14 our $AUTHORITY = 'cpan:STEVAN';
19 my $params = $class->BUILDARGS(@_);
21 # We want to support passing $self->new, but initialize
22 # takes only an unblessed class name
23 my $real_class = Scalar::Util::blessed($class) || $class;
24 my $self = Class::MOP::Class->initialize($real_class)->new_object($params);
26 $self->BUILDALL($params);
33 if ( scalar @_ == 1 ) {
34 unless ( defined $_[0] && ref $_[0] eq 'HASH' ) {
35 Class::MOP::class_of($class)->throw_error(
36 "Single parameters to new() must be a HASH ref",
39 return { %{ $_[0] } };
47 # NOTE: we ask Perl if we even
48 # need to do this first, to avoid
49 # extra meta level calls
50 return unless $_[0]->can('BUILD');
51 my ($self, $params) = @_;
52 foreach my $method (reverse Class::MOP::class_of($self)->find_all_methods_by_name('BUILD')) {
53 $method->{code}->execute($self, $params);
59 my ($in_global_destruction) = @_;
61 # NOTE: we ask Perl if we even
62 # need to do this first, to avoid
63 # extra meta level calls
64 return unless $self->can('DEMOLISH');
66 # This is a hack, because Moose::Meta::Class may not be the right
67 # metaclass, but class_of may return undef during global
68 # destruction, if the metaclass object has already been cleaned
70 my $meta = Class::MOP::class_of($self)
71 || Moose::Meta::Class->initialize( ref $self );
73 foreach my $method ( $meta->find_all_methods_by_name('DEMOLISH') ) {
74 $method->{code}->execute($self, $in_global_destruction);
79 # if we have an exception here ...
83 # run DEMOLISHALL ourselves, ...
84 $_[0]->DEMOLISHALL(Class::MOP::in_global_destruction);
88 # otherwise it is normal destruction
89 $_[0]->DEMOLISHALL(Class::MOP::in_global_destruction);
92 # support for UNIVERSAL::DOES ...
94 my $does = UNIVERSAL->can("DOES") ? "SUPER::DOES" : "isa";
96 my ( $self, $class_or_role_name ) = @_;
97 return $self->'.$does.'($class_or_role_name)
98 || $self->does($class_or_role_name);
102 # new does() methods will be created
103 # as appropiate see Moose::Meta::Role
105 my ($self, $role_name) = @_;
106 my $meta = Class::MOP::class_of($self);
108 || $meta->throw_error("You much supply a role name to does()");
109 foreach my $class ($meta->class_precedence_list) {
110 my $m = $meta->initialize($class);
112 if $m->can('does_role') && $m->does_role($role_name);
119 require Data::Dumper;
120 local $Data::Dumper::Maxdepth = shift if @_;
121 Data::Dumper::Dumper $self;
132 Moose::Object - The base object for Moose
136 This class is the default base class for all Moose-using classes. When
137 you C<use Moose> in this class, your class will inherit from this
140 It provides a default constructor and destructor, which run the
141 C<BUILDALL> and C<DEMOLISHALL> methods respectively.
143 You don't actually I<need> to inherit from this in order to use Moose,
144 but it makes it easier to take advantage of all of Moose's features.
150 =item B<< Moose::Object->new(%params) >>
152 This method calls C<< $class->BUILDARGS(@_) >>, and then creates a new
153 instance of the appropriate class. Once the instance is created, it
154 calls C<< $instance->BUILDALL($params) >>.
156 =item B<< Moose::Object->BUILDARGS(%params) >>
158 The default implementation of this method accepts a hash or hash
159 reference of named parameters. If it receives a single argument that
160 I<isn't> a hash reference it throws an error.
162 You can override this method in your class to handle other types of
163 options passed to the constructor.
165 This method should always return a hash reference of named options.
167 =item B<< $object->BUILDALL($params) >>
169 This method will call every C<BUILD> method in the inheritance
170 hierarchy, starting with the most distant parent class and ending with
173 The C<BUILD> method will be passed the hash reference returned by
176 =item B<< $object->DEMOLISHALL >>
178 This will call every C<DEMOLISH> method in the inheritance hierarchy,
179 starting with the object's class and ending with the most distant
180 parent. C<DEMOLISHALL> and C<DEMOLISH> will receive a boolean
181 indicating whether or not we are currently in global destruction.
183 =item B<< $object->does($role_name) >>
185 This returns true if the object does the given role.
187 =item B<DOES ($class_or_role_name)>
189 This is a a Moose role-aware implementation of L<UNIVERSAL/DOES>.
191 This is effectively the same as writing:
193 $object->does($name) || $object->isa($name)
195 This method will work with Perl 5.8, which did not implement
198 =item B<< $object->dump($maxdepth) >>
200 This is a handy utility for C<Data::Dumper>ing an object. By default,
201 the maximum depth is 1, to avoid making a mess.
207 All complex software has bugs lurking in it, and this module is no
208 exception. If you find a bug please either email me, or add the bug
213 Stevan Little E<lt>stevan@iinteractive.comE<gt>
215 =head1 COPYRIGHT AND LICENSE
217 Copyright 2006-2009 by Infinity Interactive, Inc.
219 L<http://www.iinteractive.com>
221 This library is free software; you can redistribute it and/or modify
222 it under the same terms as Perl itself.