7 use Scalar::Util qw/blessed weaken/;
15 (ref($_[0]) eq 'HASH')
16 || confess "Single parameters to new() must be a HASH ref";
24 my $instance = bless {}, $class;
26 for my $attribute ($class->meta->compute_all_applicable_attributes) {
27 my $from = $attribute->init_arg;
28 my $key = $attribute->name;
31 if (defined($from) && exists($args{$from})) {
32 $attribute->verify_type_constraint($args{$from})
33 if $attribute->has_type_constraint;
35 $instance->{$key} = $args{$from};
37 weaken($instance->{$key})
38 if ref($instance->{$key}) && $attribute->is_weak_ref;
40 if ($attribute->has_trigger) {
41 $attribute->trigger->($instance, $args{$from}, $attribute);
45 if ($attribute->has_default || $attribute->has_builder) {
46 unless ($attribute->is_lazy) {
47 my $default = $attribute->default;
48 my $builder = $attribute->builder;
49 my $value = $attribute->has_builder
51 : ref($default) eq 'CODE'
55 $attribute->verify_type_constraint($value)
56 if $attribute->has_type_constraint;
58 $instance->{$key} = $value;
60 weaken($instance->{$key})
61 if ref($instance->{$key}) && $attribute->is_weak_ref;
65 if ($attribute->is_required) {
66 confess "Attribute (".$attribute->name.") is required";
72 $instance->BUILDALL(\%args);
77 sub DESTROY { shift->DEMOLISHALL }
83 return unless $self->can('BUILD');
87 for my $class (reverse $self->meta->linearized_isa) {
88 my $code = *{ $class . '::BUILD' }{CODE}
98 return unless $self->can('DEMOLISH');
102 for my $class ($self->meta->linearized_isa) {
103 my $code = *{ $class . '::DEMOLISH' }{CODE}
115 Mouse::Object - we don't need to steenkin' constructor
119 =head2 new arguments -> object
121 Instantiates a new Mouse::Object. This is obviously intended for subclasses.
123 =head2 BUILDALL \%args
125 Calls L</BUILD> on each class in the class hierarchy. This is called at the
130 You may put any business logic initialization in BUILD methods. You don't
131 need to redispatch or return any specific value.
135 Calls L</DEMOLISH> on each class in the class hierarchy. This is called at
140 You may put any business logic deinitialization in DEMOLISH methods. You don't
141 need to redispatch or return any specific value.