1 package Mouse::Meta::Method::Constructor;
5 sub generate_constructor_method_inline {
6 my ($class, $meta) = @_;
8 my $associated_metaclass_name = $meta->name;
9 my @attrs = $meta->get_all_attributes;
10 my $buildall = $class->_generate_BUILDALL($meta);
11 my $buildargs = $class->_generate_BUILDARGS($meta);
12 my $processattrs = $class->_generate_processattrs($meta, \@attrs);
13 my @compiled_constraints = map { $_ ? $_->{_compiled_type_constraint} : undef } map { $_->{type_constraint} } @attrs;
18 return \$class->Mouse::Object::new(\@_)
19 if \$class ne '$associated_metaclass_name';
21 my \$instance = bless {}, \$class;
35 sub _generate_processattrs {
36 my ($class, $meta, $attrs) = @_;
39 for my $index (0 .. @$attrs - 1) {
40 my $attr = $attrs->[$index];
41 my $key = $attr->name;
44 if (defined $attr->init_arg) {
45 my $from = $attr->init_arg;
47 $code .= "if (exists \$args->{'$from'}) {\n";
49 if ($attr->should_coerce && $attr->type_constraint) {
50 $code .= "my \$value = Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, \$args->{'$from'});\n";
53 $code .= "my \$value = \$args->{'$from'};\n";
56 if ($attr->has_type_constraint) {
57 if ($attr->type_constraint->{_compiled_type_constraint}) {
58 $code .= "unless (\$compiled_constraints[$index](\$value)) {";
60 $code .= "unless (\$attrs[$index]->{type_constraint}->check(\$value)) {";
63 \$attrs[$index]->verify_type_constraint_error(
64 q{$key}, \$value, \$attrs[$index]->type_constraint
70 $code .= "\$instance->{q{$key}} = \$value;\n";
72 if ($attr->is_weak_ref) {
73 $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n";
76 if ($attr->has_trigger) {
77 $code .= "push \@triggers, [\$attrs[$index]->{trigger}, \$value];\n";
80 $code .= "\n} else {\n";
83 if ($attr->has_default || $attr->has_builder) {
84 unless ($attr->is_lazy) {
85 my $default = $attr->default;
86 my $builder = $attr->builder;
88 $code .= "my \$value = ";
90 if ($attr->should_coerce && $attr->type_constraint) {
91 $code .= "Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, ";
94 if ($attr->has_builder) {
95 $code .= "\$instance->$builder";
97 elsif (ref($default) eq 'CODE') {
98 $code .= "\$attrs[$index]->{default}->(\$instance)";
100 elsif (!defined($default)) {
103 elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) {
107 $code .= "'$default'";
110 if ($attr->should_coerce) {
117 if ($attr->has_type_constraint) {
119 unless (\$attrs[$index]->{type_constraint}->check(\$value)) {
120 \$attrs[$index]->verify_type_constraint_error(q{$key}, \$value, \$attrs[$index]->type_constraint)
125 $code .= "\$instance->{q{$key}} = \$value;\n";
127 if ($attr->is_weak_ref) {
128 $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n";
132 elsif ($attr->is_required) {
133 $code .= "Carp::confess('Attribute ($key) is required');";
136 $code .= "}\n" if defined $attr->init_arg;
141 return join "\n", q{my @triggers;}, @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
144 sub _generate_BUILDARGS {
148 if ($meta->name->can('BUILDARGS') && $meta->name->can('BUILDARGS') != Mouse::Object->can('BUILDARGS')) {
149 return 'my $args = $class->BUILDARGS(@_)';
154 if ( scalar @_ == 1 ) {
155 ( ref( $_[0] ) eq 'HASH' )
156 || Carp::confess "Single parameters to new() must be a HASH ref";
157 $args = +{ %{ $_[0] } };
165 sub _generate_BUILDALL {
166 my ($class, $meta) = @_;
167 return '' unless $meta->name->can('BUILD');
170 push @code, q{no strict 'refs';};
171 push @code, q{no warnings 'once';};
174 for my $klass ($meta->linearized_isa) {
175 if (*{ $klass . '::BUILD' }{CODE}) {
176 unshift @code, qq{${klass}::BUILD(\$instance, \$args);};
179 return join "\n", @code;