7 use Scalar::Util 'blessed';
13 my $instance = bless {}, $class;
15 for my $attribute ($class->meta->attributes) {
16 my $key = $attribute->init_arg;
19 if (!exists($args{$key})) {
20 if (exists($attribute->{default}) || exists($attribute->{builder})) {
21 unless ($attribute->{lazy}) {
22 my $builder = $attribute->{builder};
23 my $default = exists($attribute->{builder})
25 : ref($attribute->{default}) eq 'CODE'
26 ? $attribute->{default}->()
27 : $attribute->{default};
29 $attribute->verify_type_constraint($default)
30 if $attribute->has_type_constraint;
32 $instance->{$key} = $default;
34 Scalar::Util::weaken($instance->{$key})
35 if $attribute->{weak_ref};
39 if ($attribute->{required}) {
40 confess "Attribute '$attribute->{name}' is required";
45 if (exists($args{$key})) {
46 $attribute->verify_type_constraint($args{$key})
47 if $attribute->has_type_constraint;
49 $instance->{$key} = $args{$key};
51 Scalar::Util::weaken($instance->{$key})
52 if $attribute->{weak_ref};
54 if ($attribute->{trigger}) {
55 $attribute->{trigger}->($instance, $args{$key}, $attribute);
60 $instance->BUILDALL(\%args);
65 sub DESTROY { shift->DEMOLISHALL }
71 return unless $self->can('BUILD');
75 for my $class ($self->meta->linearized_isa) {
76 my $code = *{ $class . '::BUILD' }{CODE}
86 return unless $self->can('DEMOLISH');
90 for my $class ($self->meta->linearized_isa) {
91 my $code = *{ $class . '::DEMOLISH' }{CODE}
103 Mouse::Object - we don't need to steenkin' constructor
107 =head2 new arguments -> object
109 Instantiates a new Mouse::Object. This is obviously intended for subclasses.
111 =head2 BUILDALL \%args
113 Calls L</BUILD> on each class in the class hierarchy. This is called at the
118 You may put any business logic initialization in BUILD methods. You don't
119 need to redispatch or return any specific value.
123 Calls L</DEMOLISH> on each class in the class hierarchy. This is called at
128 You may put any business logic deinitialization in DEMOLISH methods. You don't
129 need to redispatch or return any specific value.