1 package Mouse::Meta::Method::Constructor;
2 use Mouse::Util qw(:meta); # enables strict and warnings
5 my(undef, $self_var, $attr_name) = @_;
6 return sprintf '%s->{q{%s}}', $self_var, $attr_name;
9 sub _generate_constructor {
10 my ($class, $metaclass, $args) = @_;
12 my $associated_metaclass_name = $metaclass->name;
14 my @attrs = $metaclass->get_all_attributes;
16 my $buildall = $class->_generate_BUILDALL($metaclass);
17 my $buildargs = $class->_generate_BUILDARGS($metaclass);
18 my $processattrs = $class->_generate_processattrs($metaclass, \@attrs);
20 my @checks = map { $_ && $_->_compiled_type_constraint }
21 map { $_->type_constraint } @attrs;
23 my $source = sprintf("#line %d %s\n", __LINE__, __FILE__).<<"...";
26 return \$class->Mouse::Object::new(\@_)
27 if \$class ne q{$associated_metaclass_name};
30 my \$instance = bless {}, \$class;
49 sub _generate_processattrs {
50 my ($method_class, $metaclass, $attrs) = @_;
55 for my $index (0 .. @$attrs - 1) {
58 my $attr = $attrs->[$index];
59 my $key = $attr->name;
61 my $init_arg = $attr->init_arg;
62 my $type_constraint = $attr->type_constraint;
63 my $is_weak_ref = $attr->is_weak_ref;
66 my $instance_slot = $method_class->_inline_slot('$instance', $key);
67 my $attr_var = "\$attrs[$index]";
70 if(defined $type_constraint){
71 $constraint_var = "$attr_var\->{type_constraint}";
72 $need_coercion = ($attr->should_coerce && $type_constraint->has_coercion);
75 $code .= "# initialize $key\n";
77 my $post_process = '';
78 if(defined $type_constraint){
79 $post_process .= "\$checks[$index]->($instance_slot)";
80 $post_process .= " or $attr_var->_throw_type_constraint_error($instance_slot, $constraint_var);\n";
83 $post_process .= "Scalar::Util::weaken($instance_slot) if ref $instance_slot;\n";
86 if (defined $init_arg) {
87 my $value = "\$args->{q{$init_arg}}";
89 $code .= "if (exists $value) {\n";
92 $value = "$constraint_var->coerce($value)";
95 $code .= "$instance_slot = $value;\n";
96 $code .= $post_process;
98 if ($attr->has_trigger) {
100 $code .= "push \@triggers, [$attr_var\->{trigger}, $instance_slot];\n";
103 $code .= "\n} else {\n";
106 if ($attr->has_default || $attr->has_builder) {
107 unless ($attr->is_lazy) {
108 my $default = $attr->default;
109 my $builder = $attr->builder;
112 if (defined($builder)) {
113 $value = "\$instance->$builder()";
115 elsif (ref($default) eq 'CODE') {
116 $value = "$attr_var\->{default}->(\$instance)";
118 elsif (defined($default)) {
119 $value = "$attr_var\->{default}";
126 $value = "$constraint_var->coerce($value)";
129 $code .= "$instance_slot = $value;\n";
131 $code .= "Scalar::Util::weaken($instance_slot);\n";
135 elsif ($attr->is_required) {
136 $code .= "Carp::confess('Attribute ($key) is required');";
139 $code .= "}\n" if defined $init_arg;
144 if($metaclass->is_anon_class){
145 push @res, q{$instance->{__METACLASS__} = $metaclass;};
149 unshift @res, q{my @triggers;};
150 push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
153 return join "\n", @res;
156 sub _generate_BUILDARGS {
157 my(undef, $metaclass) = @_;
159 my $class = $metaclass->name;
160 if ( $class->can('BUILDARGS') && $class->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 (undef, $metaclass) = @_;
180 return '' unless $metaclass->name->can('BUILD');
183 for my $class ($metaclass->linearized_isa) {
184 if (Mouse::Util::get_code_ref($class, 'BUILD')) {
185 unshift @code, qq{${class}::BUILD(\$instance, \$args);};
188 return join "\n", @code;
196 Mouse::Meta::Method::Constructor - A Mouse method generator for constructors
200 This document describes Mouse version 0.46
204 L<Moose::Meta::Method::Constructor>