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.74';
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);
60 # NOTE: we ask Perl if we even
61 # need to do this first, to avoid
62 # extra meta level calls
63 return unless $self->can('DEMOLISH');
65 # This is a hack, because Moose::Meta::Class may not be the right
66 # metaclass, but class_of may return undef during global
67 # destruction, if the metaclass object has already been cleaned
69 my $meta = Class::MOP::class_of($self)
70 || Moose::Meta::Class->initialize( ref $self );
72 foreach my $method ( $meta->find_all_methods_by_name('DEMOLISH') ) {
73 $method->{code}->execute($self);
78 # if we have an exception here ...
82 # run DEMOLISHALL ourselves, ...
87 # otherwise it is normal destruction
91 # support for UNIVERSAL::DOES ...
93 my $does = UNIVERSAL->can("DOES") ? "SUPER::DOES" : "isa";
95 my ( $self, $class_or_role_name ) = @_;
96 return $self->'.$does.'($class_or_role_name)
97 || $self->does($class_or_role_name);
101 # new does() methods will be created
102 # as appropiate see Moose::Meta::Role
104 my ($self, $role_name) = @_;
105 my $meta = Class::MOP::class_of($self);
107 || $meta->throw_error("You much supply a role name to does()");
108 foreach my $class ($meta->class_precedence_list) {
109 my $m = $meta->initialize($class);
111 if $m->can('does_role') && $m->does_role($role_name);
118 require Data::Dumper;
119 local $Data::Dumper::Maxdepth = shift if @_;
120 Data::Dumper::Dumper $self;
131 Moose::Object - The base object for Moose
135 This class is the default base class for all Moose-using classes. When
136 you C<use Moose> in this class, your class will inherit from this
139 It provides a default constructor and destructor, which run the
140 C<BUILDALL> and C<DEMOLISHALL> methods respectively.
142 You don't actually I<need> to inherit from this in order to use Moose,
143 but it makes it easier to take advantage of all of Moose's features.
149 =item B<< Moose::Object->new(%params) >>
151 This method calls C<< $class->BUILDARGS(@_) >>, and then creates a new
152 instance of the appropriate class. Once the instance is created, it
153 calls C<< $instance->BUILDALL($params) >>.
155 =item B<< Moose::Object->BUILDARGS(%params) >>
157 The default implementation of this method accepts a hash or hash
158 reference of named parameters. If it receives a single argument that
159 I<isn't> a hash reference it throws an error.
161 You can override this method in your class to handle other types of
162 options passed to the constructor.
164 This method should always return a hash reference of named options.
166 =item B<< $object->BUILDALL($params) >>
168 This method will call every C<BUILD> method in the inheritance
169 hierarchy, starting with the most distant parent class and ending with
172 The C<BUILD> method will be passed the hash reference returned by
175 =item B<< $object->DEMOLISHALL >>
177 This will call every C<DEMOLISH> method in the inheritance hierarchy,
178 starting with the object's class and ending with the most distant
181 =item B<< $object->does($role_name) >>
183 This returns true if the object does the given role.
185 =item B<DOES ($class_or_role_name)>
187 This is a a Moose role-aware implementation of L<UNIVERSAL/DOES>.
189 This is effectively the same as writing:
191 $object->does($name) || $object->isa($name)
193 This method will work with Perl 5.8, which did not implement
196 =item B<< $object->dump($maxdepth) >>
198 This is a handy utility for C<Data::Dumper>ing an object. By default,
199 the maximum depth is 1, to avoid making a mess.
205 All complex software has bugs lurking in it, and this module is no
206 exception. If you find a bug please either email me, or add the bug
211 Stevan Little E<lt>stevan@iinteractive.comE<gt>
213 =head1 COPYRIGHT AND LICENSE
215 Copyright 2006-2009 by Infinity Interactive, Inc.
217 L<http://www.iinteractive.com>
219 This library is free software; you can redistribute it and/or modify
220 it under the same terms as Perl itself.