1 package Mouse::Meta::Method::Constructor;
2 use Mouse::Util; # enables strict and warnings
4 sub _generate_constructor_method {
5 my ($class, $metaclass, $args) = @_;
7 my $associated_metaclass_name = $metaclass->name;
8 my @attrs = $metaclass->get_all_attributes;
10 my $buildall = $class->_generate_BUILDALL($metaclass);
11 my $buildargs = $class->_generate_BUILDARGS($metaclass);
12 my $processattrs = $class->_generate_processattrs($metaclass, \@attrs);
14 my @compiled_constraints = map { $_ ? $_->_compiled_type_constraint : undef }
15 map { $_->type_constraint } @attrs;
19 my $source = sprintf("#line %d %s\n", __LINE__, __FILE__).<<"...";
22 return \$class->Mouse::Object::new(\@_)
23 if \$class ne q{$associated_metaclass_name};
25 my \$instance = bless {}, \$class;
40 $metaclass->add_method($args->{constructor_name} => $code);
44 sub _generate_processattrs {
45 my ($class, $metaclass, $attrs) = @_;
50 for my $index (0 .. @$attrs - 1) {
51 my $attr = $attrs->[$index];
52 my $key = $attr->name;
55 if (defined $attr->init_arg) {
56 my $from = $attr->init_arg;
58 $code .= "if (exists \$args->{q{$from}}) {\n";
60 my $value = "\$args->{q{$from}}";
61 if(my $type_constraint = $attr->type_constraint){
62 if($attr->should_coerce && $type_constraint->has_coercion){
63 $code .= "my \$value = \$attrs[$index]->{type_constraint}->coerce(\$args->{q{$from}});\n";
67 $code .= "\$compiled_constraints[$index]->($value)\n";
68 $code .= " or \$attrs[$index]->verify_type_constraint_error(q{$key}, $value, \$attrs[$index]->{type_constraint});\n";
71 $code .= "\$instance->{q{$key}} = $value;\n";
73 if ($attr->is_weak_ref) {
74 $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref($value);\n";
77 if ($attr->has_trigger) {
79 $code .= "push \@triggers, [\$attrs[$index]->{trigger}, $value];\n";
82 $code .= "\n} else {\n";
85 if ($attr->has_default || $attr->has_builder) {
86 unless ($attr->is_lazy) {
87 my $default = $attr->default;
88 my $builder = $attr->builder;
90 $code .= "my \$value = ";
92 if ($attr->should_coerce && $attr->type_constraint) {
93 $code .= "\$attrs[$index]->_coerce_and_verify(";
96 if ($attr->has_builder) {
97 $code .= "\$instance->$builder()";
99 elsif (ref($default) eq 'CODE') {
100 $code .= "\$attrs[$index]->{default}->(\$instance)";
102 elsif (!defined($default)) {
105 elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) {
109 $code .= "'$default'";
112 if ($attr->should_coerce) {
119 if ($attr->has_type_constraint) {
121 unless (\$attrs[$index]->{type_constraint}->check(\$value)) {
122 \$attrs[$index]->verify_type_constraint_error(q{$key}, \$value, \$attrs[$index]->type_constraint)
127 $code .= "\$instance->{q{$key}} = \$value;\n";
129 if ($attr->is_weak_ref) {
130 $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n";
134 elsif ($attr->is_required) {
135 $code .= "Carp::confess('Attribute ($key) is required');";
138 $code .= "}\n" if defined $attr->init_arg;
143 if($metaclass->is_anon_class){
144 push @res, q{$instnace->{__METACLASS__} = $metaclass;};
148 unshift @res, q{my @triggers;};
149 push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
152 return join "\n", @res;
155 sub _generate_BUILDARGS {
156 my($self, $metaclass) = @_;
158 if ($metaclass->name->can('BUILDARGS') && $metaclass->name->can('BUILDARGS') != \&Mouse::Object::BUILDARGS) {
159 return 'my $args = $class->BUILDARGS(@_)';
164 if ( scalar @_ == 1 ) {
165 ( ref( $_[0] ) eq 'HASH' )
166 || Carp::confess "Single parameters to new() must be a HASH ref";
167 $args = +{ %{ $_[0] } };
175 sub _generate_BUILDALL {
176 my ($class, $metaclass) = @_;
178 return '' unless $metaclass->name->can('BUILD');
181 for my $class ($metaclass->linearized_isa) {
185 if (*{ $class . '::BUILD' }{CODE}) {
186 unshift @code, qq{${class}::BUILD(\$instance, \$args);};
189 return join "\n", @code;