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) = @_;
54 my $strict = $metaclass->__strict_constructor;
57 push @res, 'my $used = 0;';
60 for my $index (0 .. @$attrs - 1) {
63 my $attr = $attrs->[$index];
64 my $key = $attr->name;
66 my $init_arg = $attr->init_arg;
67 my $type_constraint = $attr->type_constraint;
68 my $is_weak_ref = $attr->is_weak_ref;
71 my $instance_slot = $method_class->_inline_slot('$instance', $key);
72 my $attr_var = "\$attrs[$index]";
75 if(defined $type_constraint){
76 $constraint_var = "$attr_var\->{type_constraint}";
77 $need_coercion = ($attr->should_coerce && $type_constraint->has_coercion);
80 $code .= "# initialize $key\n";
82 my $post_process = '';
83 if(defined $type_constraint){
84 $post_process .= "\$checks[$index]->($instance_slot)";
85 $post_process .= " or $attr_var->_throw_type_constraint_error($instance_slot, $constraint_var);\n";
88 $post_process .= "Scalar::Util::weaken($instance_slot) if ref $instance_slot;\n";
91 if (defined $init_arg) {
92 my $value = "\$args->{q{$init_arg}}";
94 $code .= "if (exists $value) {\n";
97 $value = "$constraint_var->coerce($value)";
100 $code .= "$instance_slot = $value;\n";
101 $code .= $post_process;
103 if ($attr->has_trigger) {
105 $code .= "push \@triggers, [$attr_var\->{trigger}, $instance_slot];\n";
109 $code .= '++$used;' . "\n";
112 $code .= "\n} else {\n"; # $value exists
115 if ($attr->has_default || $attr->has_builder) {
116 unless ($attr->is_lazy) {
117 my $default = $attr->default;
118 my $builder = $attr->builder;
121 if (defined($builder)) {
122 $value = "\$instance->$builder()";
124 elsif (ref($default) eq 'CODE') {
125 $value = "$attr_var\->{default}->(\$instance)";
127 elsif (defined($default)) {
128 $value = "$attr_var\->{default}";
135 $value = "$constraint_var->coerce($value)";
138 $code .= "$instance_slot = $value;\n";
140 $code .= "Scalar::Util::weaken($instance_slot);\n";
144 elsif ($attr->is_required) {
145 $code .= "Carp::confess('Attribute ($key) is required');";
148 $code .= "}\n" if defined $init_arg;
154 push @res, q{if($used < keys %{$args})}
155 . sprintf q{{ %s->_report_unknown_args($metaclass, \@attrs, $args) }}, $method_class;
158 if($metaclass->is_anon_class){
159 push @res, q{$instance->{__METACLASS__} = $metaclass;};
163 unshift @res, q{my @triggers;};
164 push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
167 return join "\n", @res;
170 sub _generate_BUILDARGS {
171 my(undef, $metaclass) = @_;
173 my $class = $metaclass->name;
174 if ( $class->can('BUILDARGS') && $class->can('BUILDARGS') != \&Mouse::Object::BUILDARGS ) {
175 return 'my $args = $class->BUILDARGS(@_)';
180 if ( scalar @_ == 1 ) {
181 ( ref( $_[0] ) eq 'HASH' )
182 || Carp::confess "Single parameters to new() must be a HASH ref";
183 $args = +{ %{ $_[0] } };
191 sub _generate_BUILDALL {
192 my (undef, $metaclass) = @_;
194 return '' unless $metaclass->name->can('BUILD');
197 for my $class ($metaclass->linearized_isa) {
198 if (Mouse::Util::get_code_ref($class, 'BUILD')) {
199 unshift @code, qq{${class}::BUILD(\$instance, \$args);};
202 return join "\n", @code;
205 sub _report_unknown_args {
206 my(undef, $metaclass, $attrs, $args) = @_;
210 foreach my $attr(@{$attrs}){
211 my $init_arg = $attr->init_arg;
212 if(defined $init_arg){
213 $init_args{$init_arg}++;
217 while(my $key = each %{$args}){
218 if(!exists $init_args{$key}){
219 push @unknowns, $key;
223 $metaclass->throw_error( sprintf
224 "Unknown attribute passed to the constructor of %s: %s",
225 $metaclass->name, Mouse::Util::english_list(@unknowns),
234 Mouse::Meta::Method::Constructor - A Mouse method generator for constructors
238 This document describes Mouse version 0.57
242 L<Moose::Meta::Method::Constructor>