1 package Mouse::Meta::Method::Constructor;
5 sub generate_constructor_method_inline {
6 my ($class, $meta) = @_;
8 my @attrs = $meta->compute_all_applicable_attributes;
9 my $buildall = $class->_generate_BUILDALL($meta);
10 my $buildargs = $class->_generate_BUILDARGS($meta);
11 my $processattrs = $class->_generate_processattrs($meta, \@attrs);
12 my @compiled_constraints = map { $_ ? $_->{_compiled_type_constraint} : undef } map { $_->{type_constraint} } @attrs;
18 my \$instance = bless {}, \$class;
32 sub _generate_processattrs {
33 my ($class, $meta, $attrs) = @_;
36 for my $index (0 .. @$attrs - 1) {
37 my $attr = $attrs->[$index];
38 my $key = $attr->name;
41 if (defined $attr->init_arg) {
42 my $from = $attr->init_arg;
44 $code .= "if (exists \$args->{'$from'}) {\n";
46 if ($attr->should_coerce && $attr->type_constraint) {
47 $code .= "my \$value = Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, \$args->{'$from'});\n";
50 $code .= "my \$value = \$args->{'$from'};\n";
53 if ($attr->has_type_constraint) {
54 if ($attr->type_constraint->{_compiled_type_constraint}) {
55 $code .= "unless (\$compiled_constraints[$index](\$value)) {";
57 $code .= "unless (\$attrs[$index]->{type_constraint}->check(\$value)) {";
60 \$attrs[$index]->verify_type_constraint_error(
61 '$key', \$_, \$attrs[$index]->type_constraint
67 $code .= "\$instance->{'$key'} = \$value;\n";
69 if ($attr->is_weak_ref) {
70 $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );\n";
73 if ($attr->has_trigger) {
74 $code .= "\$attrs[$index]->{trigger}->( \$instance, \$value );\n";
77 $code .= "\n} else {\n";
80 if ($attr->has_default || $attr->has_builder) {
81 unless ($attr->is_lazy) {
82 my $default = $attr->default;
83 my $builder = $attr->builder;
85 $code .= "my \$value = ";
87 if ($attr->should_coerce && $attr->type_constraint) {
88 $code .= "Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, ";
91 if ($attr->has_builder) {
92 $code .= "\$instance->$builder";
94 elsif (ref($default) eq 'CODE') {
95 $code .= "\$attrs[$index]->{default}->(\$instance)";
97 elsif (!defined($default)) {
100 elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) {
104 $code .= "'$default'";
107 if ($attr->should_coerce) {
114 if ($attr->has_type_constraint) {
116 unless (\$attrs[$index]->{type_constraint}->check(\$value)) {
117 \$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint)
122 $code .= "\$instance->{'$key'} = \$value;\n";
124 if ($attr->is_weak_ref) {
125 $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );\n";
129 elsif ($attr->is_required) {
130 $code .= "Carp::confess('Attribute ($key) is required');";
133 $code .= "}\n" if defined $attr->init_arg;
138 return join "\n", @res;
141 sub _generate_BUILDARGS {
145 if ($meta->name->can('BUILDARGS') && $meta->name->can('BUILDARGS') != Mouse::Object->can('BUILDARGS')) {
146 return 'my $args = $class->BUILDARGS(@_)';
151 if ( scalar @_ == 1 ) {
152 ( ref( $_[0] ) eq 'HASH' )
153 || Carp::confess "Single parameters to new() must be a HASH ref";
154 $args = +{ %{ $_[0] } };
162 sub _generate_BUILDALL {
163 my ($class, $meta) = @_;
164 return '' unless $meta->name->can('BUILD');
167 push @code, q{no strict 'refs';};
168 push @code, q{no warnings 'once';};
171 for my $klass ($meta->linearized_isa) {
172 if (*{ $klass . '::BUILD' }{CODE}) {
173 unshift @code, qq{${klass}::BUILD(\$instance, \$args);};
176 return join "\n", @code;