6 use Scalar::Util qw/weaken/;
12 my $args = $class->BUILDARGS(@_);
14 my $instance = bless {}, $class;
16 for my $attribute ($class->meta->compute_all_applicable_attributes) {
17 my $from = $attribute->init_arg;
18 my $key = $attribute->name;
20 if (defined($from) && exists($args->{$from})) {
21 $args->{$from} = $attribute->coerce_constraint($args->{$from})
22 if $attribute->should_coerce;
23 $attribute->verify_type_constraint($args->{$from})
24 if $attribute->has_type_constraint;
26 $instance->{$key} = $args->{$from};
28 weaken($instance->{$key})
29 if ref($instance->{$key}) && $attribute->is_weak_ref;
31 if ($attribute->has_trigger) {
32 $attribute->trigger->($instance, $args->{$from}, $attribute);
36 if ($attribute->has_default || $attribute->has_builder) {
37 unless ($attribute->is_lazy) {
38 my $default = $attribute->default;
39 my $builder = $attribute->builder;
40 my $value = $attribute->has_builder
42 : ref($default) eq 'CODE'
43 ? $default->($instance)
46 $value = $attribute->coerce_constraint($value)
47 if $attribute->should_coerce;
48 $attribute->verify_type_constraint($value)
49 if $attribute->has_type_constraint;
51 $instance->{$key} = $value;
53 weaken($instance->{$key})
54 if ref($instance->{$key}) && $attribute->is_weak_ref;
58 if ($attribute->is_required) {
59 confess "Attribute (".$attribute->name.") is required";
65 $instance->BUILDALL($args);
75 (ref($_[0]) eq 'HASH')
76 || confess "Single parameters to new() must be a HASH ref";
87 sub DESTROY { shift->DEMOLISHALL }
93 return unless $self->can('BUILD');
95 for my $class (reverse $self->meta->linearized_isa) {
98 my $code = *{ $class . '::BUILD' }{CODE}
108 return unless $self->can('DEMOLISH');
112 for my $class ($self->meta->linearized_isa) {
113 my $code = *{ $class . '::DEMOLISH' }{CODE}
125 Mouse::Object - we don't need to steenkin' constructor
129 =head2 new arguments -> object
131 Instantiates a new Mouse::Object. This is obviously intended for subclasses.
133 =head2 BUILDALL \%args
135 Calls L</BUILD> on each class in the class hierarchy. This is called at the
140 You may put any business logic initialization in BUILD methods. You don't
141 need to redispatch or return any specific value.
145 Lets you override the arguments that C<new> takes. Return a hashref of
150 Calls L</DEMOLISH> on each class in the class hierarchy. This is called at
155 You may put any business logic deinitialization in DEMOLISH methods. You don't
156 need to redispatch or return any specific value.