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 (values %{ $class->meta->get_attribute_map }) {
27 my $from = $attribute->init_arg;
28 my $key = $attribute->name;
31 if (!exists($args{$from})) {
32 if ($attribute->has_default || $attribute->has_builder) {
33 unless ($attribute->is_lazy) {
34 my $default = $attribute->default;
35 my $builder = $attribute->builder;
36 my $value = $attribute->has_builder
38 : ref($default) eq 'CODE'
42 $attribute->verify_type_constraint($value)
43 if $attribute->has_type_constraint;
45 $instance->{$key} = $value;
47 weaken($instance->{$key})
48 if ref($instance->{$key}) && $attribute->is_weak_ref;
52 if ($attribute->is_required) {
53 confess "Attribute (".$attribute->name.") is required";
58 if (exists($args{$from})) {
59 $attribute->verify_type_constraint($args{$from})
60 if $attribute->has_type_constraint;
62 $instance->{$key} = $args{$from};
64 weaken($instance->{$key})
65 if ref($instance->{$key}) && $attribute->is_weak_ref;
67 if ($attribute->has_trigger) {
68 $attribute->trigger->($instance, $args{$from}, $attribute);
73 $instance->BUILDALL(\%args);
78 sub DESTROY { shift->DEMOLISHALL }
84 return unless $self->can('BUILD');
88 for my $class (reverse $self->meta->linearized_isa) {
89 my $code = *{ $class . '::BUILD' }{CODE}
99 return unless $self->can('DEMOLISH');
103 for my $class ($self->meta->linearized_isa) {
104 my $code = *{ $class . '::DEMOLISH' }{CODE}
116 Mouse::Object - we don't need to steenkin' constructor
120 =head2 new arguments -> object
122 Instantiates a new Mouse::Object. This is obviously intended for subclasses.
124 =head2 BUILDALL \%args
126 Calls L</BUILD> on each class in the class hierarchy. This is called at the
131 You may put any business logic initialization in BUILD methods. You don't
132 need to redispatch or return any specific value.
136 Calls L</DEMOLISH> on each class in the class hierarchy. This is called at
141 You may put any business logic deinitialization in DEMOLISH methods. You don't
142 need to redispatch or return any specific value.