1 package Mouse::Meta::Method::Constructor;
5 sub generate_constructor_method_inline {
6 my ($class, $metaclass) = @_;
8 my $associated_metaclass_name = $metaclass->name;
9 my @attrs = $metaclass->get_all_attributes;
11 my $buildall = $class->_generate_BUILDALL($metaclass);
12 my $buildargs = $class->_generate_BUILDARGS($metaclass);
13 my $processattrs = $class->_generate_processattrs($metaclass, \@attrs);
15 my @compiled_constraints = map { $_ ? $_->{_compiled_type_constraint} : undef } map { $_->{type_constraint} } @attrs;
20 return \$class->Mouse::Object::new(\@_)
21 if \$class ne q{$associated_metaclass_name};
23 my \$instance = bless {}, \$class;
36 sub _generate_processattrs {
37 my ($class, $metaclass, $attrs) = @_;
42 for my $index (0 .. @$attrs - 1) {
43 my $attr = $attrs->[$index];
44 my $key = $attr->name;
47 if (defined $attr->init_arg) {
48 my $from = $attr->init_arg;
50 $code .= "if (exists \$args->{'$from'}) {\n";
52 if ($attr->should_coerce && $attr->type_constraint) {
53 $code .= "my \$value = Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, \$args->{'$from'});\n";
56 $code .= "my \$value = \$args->{'$from'};\n";
59 if ($attr->has_type_constraint) {
60 if ($attr->type_constraint->{_compiled_type_constraint}) {
61 $code .= "unless (\$compiled_constraints[$index](\$value)) {";
63 $code .= "unless (\$attrs[$index]->{type_constraint}->check(\$value)) {";
66 \$attrs[$index]->verify_type_constraint_error(
67 q{$key}, \$value, \$attrs[$index]->type_constraint
73 $code .= "\$instance->{q{$key}} = \$value;\n";
75 if ($attr->is_weak_ref) {
76 $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n";
79 if ($attr->has_trigger) {
81 $code .= "push \@triggers, [\$attrs[$index]->{trigger}, \$value];\n";
84 $code .= "\n} else {\n";
87 if ($attr->has_default || $attr->has_builder) {
88 unless ($attr->is_lazy) {
89 my $default = $attr->default;
90 my $builder = $attr->builder;
92 $code .= "my \$value = ";
94 if ($attr->should_coerce && $attr->type_constraint) {
95 $code .= "Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, ";
98 if ($attr->has_builder) {
99 $code .= "\$instance->$builder";
101 elsif (ref($default) eq 'CODE') {
102 $code .= "\$attrs[$index]->{default}->(\$instance)";
104 elsif (!defined($default)) {
107 elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) {
111 $code .= "'$default'";
114 if ($attr->should_coerce) {
121 if ($attr->has_type_constraint) {
123 unless (\$attrs[$index]->{type_constraint}->check(\$value)) {
124 \$attrs[$index]->verify_type_constraint_error(q{$key}, \$value, \$attrs[$index]->type_constraint)
129 $code .= "\$instance->{q{$key}} = \$value;\n";
131 if ($attr->is_weak_ref) {
132 $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n";
136 elsif ($attr->is_required) {
137 $code .= "Carp::confess('Attribute ($key) is required');";
140 $code .= "}\n" if defined $attr->init_arg;
145 if($metaclass->is_anon_class){
146 push @res, q{$instnace->{__METACLASS__} = $metaclass;};
150 unshift @res, q{my @triggers;};
151 push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
154 return join "\n", @res;
157 sub _generate_BUILDARGS {
158 my($self, $metaclass) = @_;
160 if ($metaclass->name->can('BUILDARGS') && $metaclass->name->can('BUILDARGS') != \&Mouse::Object::BUILDARGS) {
161 return 'my $args = $class->BUILDARGS(@_)';
166 if ( scalar @_ == 1 ) {
167 ( ref( $_[0] ) eq 'HASH' )
168 || Carp::confess "Single parameters to new() must be a HASH ref";
169 $args = +{ %{ $_[0] } };
177 sub _generate_BUILDALL {
178 my ($class, $metaclass) = @_;
180 return '' unless $metaclass->name->can('BUILD');
183 for my $class ($metaclass->linearized_isa) {
186 if (*{ $class . '::BUILD' }{CODE}) {
187 unshift @code, qq{${class}::BUILD(\$instance, \$args);};
190 return join "\n", @code;