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 }
16 map { $_->type_constraint } @attrs;
18 my $code = sprintf("#line %d %s\n", __LINE__, __FILE__).<<"...";
21 return \$class->Mouse::Object::new(\@_)
22 if \$class ne q{$associated_metaclass_name};
24 my \$instance = bless {}, \$class;
37 sub _generate_processattrs {
38 my ($class, $metaclass, $attrs) = @_;
43 for my $index (0 .. @$attrs - 1) {
44 my $attr = $attrs->[$index];
45 my $key = $attr->name;
48 if (defined $attr->init_arg) {
49 my $from = $attr->init_arg;
51 $code .= "if (exists \$args->{q{$from}}) {\n";
53 my $value = "\$args->{q{$from}}";
54 if(my $type_constraint = $attr->type_constraint){
55 if($attr->should_coerce && $type_constraint->has_coercion){
56 $code .= "my \$value = \$attrs[$index]->{type_constraint}->coerce(\$args->{q{$from}});\n";
60 $code .= "\$compiled_constraints[$index]->($value)\n";
61 $code .= " or \$attrs[$index]->verify_type_constraint_error(q{$key}, $value, \$attrs[$index]->{type_constraint});\n";
64 $code .= "\$instance->{q{$key}} = $value;\n";
66 if ($attr->is_weak_ref) {
67 $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref($value);\n";
70 if ($attr->has_trigger) {
72 $code .= "push \@triggers, [\$attrs[$index]->{trigger}, $value];\n";
75 $code .= "\n} else {\n";
78 if ($attr->has_default || $attr->has_builder) {
79 unless ($attr->is_lazy) {
80 my $default = $attr->default;
81 my $builder = $attr->builder;
83 $code .= "my \$value = ";
85 if ($attr->should_coerce && $attr->type_constraint) {
86 $code .= "\$attrs[$index]->_coerce_and_verify(";
89 if ($attr->has_builder) {
90 $code .= "\$instance->$builder()";
92 elsif (ref($default) eq 'CODE') {
93 $code .= "\$attrs[$index]->{default}->(\$instance)";
95 elsif (!defined($default)) {
98 elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) {
102 $code .= "'$default'";
105 if ($attr->should_coerce) {
112 if ($attr->has_type_constraint) {
114 unless (\$attrs[$index]->{type_constraint}->check(\$value)) {
115 \$attrs[$index]->verify_type_constraint_error(q{$key}, \$value, \$attrs[$index]->type_constraint)
120 $code .= "\$instance->{q{$key}} = \$value;\n";
122 if ($attr->is_weak_ref) {
123 $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n";
127 elsif ($attr->is_required) {
128 $code .= "Carp::confess('Attribute ($key) is required');";
131 $code .= "}\n" if defined $attr->init_arg;
136 if($metaclass->is_anon_class){
137 push @res, q{$instnace->{__METACLASS__} = $metaclass;};
141 unshift @res, q{my @triggers;};
142 push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
145 return join "\n", @res;
148 sub _generate_BUILDARGS {
149 my($self, $metaclass) = @_;
151 if ($metaclass->name->can('BUILDARGS') && $metaclass->name->can('BUILDARGS') != \&Mouse::Object::BUILDARGS) {
152 return 'my $args = $class->BUILDARGS(@_)';
157 if ( scalar @_ == 1 ) {
158 ( ref( $_[0] ) eq 'HASH' )
159 || Carp::confess "Single parameters to new() must be a HASH ref";
160 $args = +{ %{ $_[0] } };
168 sub _generate_BUILDALL {
169 my ($class, $metaclass) = @_;
171 return '' unless $metaclass->name->can('BUILD');
174 for my $class ($metaclass->linearized_isa) {
178 if (*{ $class . '::BUILD' }{CODE}) {
179 unshift @code, qq{${class}::BUILD(\$instance, \$args);};
182 return join "\n", @code;