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_constructor = $metaclass->__strict_constructor;
57 if($strict_constructor){
58 push @res, 'my $used = 0;';
61 for my $index (0 .. @$attrs - 1) {
64 my $attr = $attrs->[$index];
65 my $key = $attr->name;
67 my $init_arg = $attr->init_arg;
68 my $type_constraint = $attr->type_constraint;
69 my $is_weak_ref = $attr->is_weak_ref;
72 my $instance_slot = $method_class->_inline_slot('$instance', $key);
73 my $attr_var = "\$attrs[$index]";
76 if(defined $type_constraint){
77 $constraint_var = "$attr_var\->{type_constraint}";
78 $need_coercion = ($attr->should_coerce && $type_constraint->has_coercion);
81 $code .= "# initialize $key\n";
83 my $post_process = '';
84 if(defined $type_constraint){
85 $post_process .= "\$checks[$index]->($instance_slot)";
86 $post_process .= " or $attr_var->_throw_type_constraint_error($instance_slot, $constraint_var);\n";
89 $post_process .= "Scalar::Util::weaken($instance_slot) if ref $instance_slot;\n";
92 if (defined $init_arg) {
93 my $value = "\$args->{q{$init_arg}}";
95 $code .= "if (exists $value) {\n";
98 $value = "$constraint_var->coerce($value)";
101 $code .= "$instance_slot = $value;\n";
102 $code .= $post_process;
104 if ($attr->has_trigger) {
106 $code .= "push \@triggers, [$attr_var\->{trigger}, $instance_slot];\n";
109 if ($strict_constructor){
110 $code .= '$used++;' . "\n";
113 $code .= "\n} else {\n"; # $value exists
116 if ($attr->has_default || $attr->has_builder) {
117 unless ($attr->is_lazy) {
118 my $default = $attr->default;
119 my $builder = $attr->builder;
122 if (defined($builder)) {
123 $value = "\$instance->$builder()";
125 elsif (ref($default) eq 'CODE') {
126 $value = "$attr_var\->{default}->(\$instance)";
128 elsif (defined($default)) {
129 $value = "$attr_var\->{default}";
136 $value = "$constraint_var->coerce($value)";
139 $code .= "$instance_slot = $value;\n";
141 $code .= "Scalar::Util::weaken($instance_slot);\n";
145 elsif ($attr->is_required) {
146 $code .= "Carp::confess('Attribute ($key) is required');";
149 $code .= "}\n" if defined $init_arg;
154 if($strict_constructor){
155 push @res, q{if($used < keys %{$args})}
156 . q{{ Mouse::Meta::Method::Constructor::_report_unknown_args($metaclass, \@attrs, $instance, $args) }};
159 if($metaclass->is_anon_class){
160 push @res, q{$instance->{__METACLASS__} = $metaclass;};
164 unshift @res, q{my @triggers;};
165 push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
168 return join "\n", @res;
171 sub _generate_BUILDARGS {
172 my(undef, $metaclass) = @_;
174 my $class = $metaclass->name;
175 if ( $class->can('BUILDARGS') && $class->can('BUILDARGS') != \&Mouse::Object::BUILDARGS ) {
176 return 'my $args = $class->BUILDARGS(@_)';
181 if ( scalar @_ == 1 ) {
182 ( ref( $_[0] ) eq 'HASH' )
183 || Carp::confess "Single parameters to new() must be a HASH ref";
184 $args = +{ %{ $_[0] } };
192 sub _generate_BUILDALL {
193 my (undef, $metaclass) = @_;
195 return '' unless $metaclass->name->can('BUILD');
198 for my $class ($metaclass->linearized_isa) {
199 if (Mouse::Util::get_code_ref($class, 'BUILD')) {
200 unshift @code, qq{${class}::BUILD(\$instance, \$args);};
203 return join "\n", @code;
206 sub _report_unknown_args {
207 my($metaclass, $attrs, $instance, $args) = @_;
211 foreach my $attr(@{$attrs}){
212 my $init_arg = $attr->init_arg;
213 if(defined $init_arg){
214 $init_args{$init_arg}++;
218 while(my $key = each %{$args}){
219 if(!exists $init_args{$key}){
220 push @unknowns, $key;
224 $metaclass->throw_error( sprintf
225 "Unknown attribute passed to the constructor of %s: %s",
226 ref($instance), join ', ', @unknowns
235 Mouse::Meta::Method::Constructor - A Mouse method generator for constructors
239 This document describes Mouse version 0.50_02
243 L<Moose::Meta::Method::Constructor>