7 use Scalar::Util qw/blessed weaken/;
13 my $args = $class->BUILDARGS(@_);
15 my $instance = bless {}, $class;
17 for my $attribute ($class->meta->compute_all_applicable_attributes) {
18 my $from = $attribute->init_arg;
19 my $key = $attribute->name;
22 if (defined($from) && exists($args->{$from})) {
23 if ($attribute->has_trigger && $attribute->trigger->{before}) {
24 $attribute->trigger->{before}->($instance, $args->{$from}, $attribute);
27 $attribute->verify_type_constraint($args->{$from})
28 if $attribute->has_type_constraint;
30 $instance->{$key} = $args->{$from};
32 weaken($instance->{$key})
33 if ref($instance->{$key}) && $attribute->is_weak_ref;
35 if ($attribute->has_trigger && $attribute->trigger->{after}) {
36 $attribute->trigger->{after}->($instance, $args->{$from}, $attribute);
40 if ($attribute->has_default || $attribute->has_builder) {
41 unless ($attribute->is_lazy) {
42 my $default = $attribute->default;
43 my $builder = $attribute->builder;
44 my $value = $attribute->has_builder
46 : ref($default) eq 'CODE'
50 $attribute->verify_type_constraint($value)
51 if $attribute->has_type_constraint;
53 $instance->{$key} = $value;
55 weaken($instance->{$key})
56 if ref($instance->{$key}) && $attribute->is_weak_ref;
60 if ($attribute->is_required) {
61 confess "Attribute (".$attribute->name.") is required";
67 $instance->BUILDALL($args);
77 (ref($_[0]) eq 'HASH')
78 || confess "Single parameters to new() must be a HASH ref";
89 sub DESTROY { shift->DEMOLISHALL }
95 return unless $self->can('BUILD');
97 for my $class (reverse $self->meta->linearized_isa) {
100 my $code = *{ $class . '::BUILD' }{CODE}
110 return unless $self->can('DEMOLISH');
114 for my $class ($self->meta->linearized_isa) {
115 my $code = *{ $class . '::DEMOLISH' }{CODE}
127 Mouse::Object - we don't need to steenkin' constructor
131 =head2 new arguments -> object
133 Instantiates a new Mouse::Object. This is obviously intended for subclasses.
135 =head2 BUILDALL \%args
137 Calls L</BUILD> on each class in the class hierarchy. This is called at the
142 You may put any business logic initialization in BUILD methods. You don't
143 need to redispatch or return any specific value.
147 Lets you override the arguments that C<new> takes. Return a hashref of
152 Calls L</DEMOLISH> on each class in the class hierarchy. This is called at
157 You may put any business logic deinitialization in DEMOLISH methods. You don't
158 need to redispatch or return any specific value.