5 use Scalar::Util 'weaken';
11 my $args = $class->BUILDARGS(@_);
13 my $instance = bless {}, $class;
15 for my $attribute ($class->meta->compute_all_applicable_attributes) {
16 my $from = $attribute->init_arg;
17 my $key = $attribute->name;
19 if (defined($from) && exists($args->{$from})) {
20 $args->{$from} = $attribute->coerce_constraint($args->{$from})
21 if $attribute->should_coerce;
22 $attribute->verify_type_constraint($args->{$from})
23 if $attribute->has_type_constraint;
25 $instance->{$key} = $args->{$from};
27 weaken($instance->{$key})
28 if ref($instance->{$key}) && $attribute->is_weak_ref;
30 if ($attribute->has_trigger) {
31 $attribute->trigger->($instance, $args->{$from}, $attribute);
35 if ($attribute->has_default || $attribute->has_builder) {
36 unless ($attribute->is_lazy) {
37 my $default = $attribute->default;
38 my $builder = $attribute->builder;
39 my $value = $attribute->has_builder
41 : ref($default) eq 'CODE'
42 ? $default->($instance)
45 $value = $attribute->coerce_constraint($value)
46 if $attribute->should_coerce;
47 $attribute->verify_type_constraint($value)
48 if $attribute->has_type_constraint;
50 $instance->{$key} = $value;
52 weaken($instance->{$key})
53 if ref($instance->{$key}) && $attribute->is_weak_ref;
57 if ($attribute->is_required) {
58 confess "Attribute (".$attribute->name.") is required";
64 $instance->BUILDALL($args);
74 (ref($_[0]) eq 'HASH')
75 || confess "Single parameters to new() must be a HASH ref";
86 sub DESTROY { shift->DEMOLISHALL }
92 return unless $self->can('BUILD');
94 for my $class (reverse $self->meta->linearized_isa) {
97 my $code = *{ $class . '::BUILD' }{CODE}
107 return unless $self->can('DEMOLISH');
111 for my $class ($self->meta->linearized_isa) {
112 my $code = *{ $class . '::DEMOLISH' }{CODE}
124 Mouse::Object - we don't need to steenkin' constructor
128 =head2 new arguments -> object
130 Instantiates a new Mouse::Object. This is obviously intended for subclasses.
132 =head2 BUILDALL \%args
134 Calls L</BUILD> on each class in the class hierarchy. This is called at the
139 You may put any business logic initialization in BUILD methods. You don't
140 need to redispatch or return any specific value.
144 Lets you override the arguments that C<new> takes. Return a hashref of
149 Calls L</DEMOLISH> on each class in the class hierarchy. This is called at
154 You may put any business logic deinitialization in DEMOLISH methods. You don't
155 need to redispatch or return any specific value.