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 $buildall = $class->_generate_BUILDALL($metaclass);
15 my $buildargs = $class->_generate_BUILDARGS($metaclass);
16 my $initializer = $class->_generate_initialize_object($metaclass);
17 my $source = sprintf(<<'EOT', __LINE__, __FILE__, $metaclass->name, $buildargs, $buildall);
22 return $class->Mouse::Object::new(@_)
23 if $class ne __PACKAGE__;
26 my $instance = bless {}, $class;
27 $metaclass->$initializer($instance, $args, 0);
44 sub _generate_initialize_object {
45 my ($method_class, $metaclass) = @_;
46 my @attrs = $metaclass->get_all_attributes;
48 my @checks = map { $_ && $_->_compiled_type_constraint }
49 map { $_->type_constraint } @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)\n";
85 $post_process .= " or $attr_var->_throw_type_constraint_error($instance_slot, $constraint_var);\n";
88 $post_process = "Scalar::Util::weaken($instance_slot) "
89 . "if ref $instance_slot;\n";
92 # build cde for an attribute
93 if (defined $init_arg) {
94 my $value = "\$args->{q{$init_arg}}";
96 $code .= "if (exists $value) {\n";
99 $value = "$constraint_var->coerce($value)";
102 $code .= "$instance_slot = $value;\n";
103 $code .= $post_process;
105 if ($attr->has_trigger) {
107 $code .= "push \@triggers, [$attr_var\->{trigger}, $instance_slot];\n";
111 $code .= '++$used;' . "\n";
114 $code .= "\n} else {\n"; # $value exists
117 if ($attr->has_default || $attr->has_builder) {
118 unless ($attr->is_lazy) {
119 my $default = $attr->default;
120 my $builder = $attr->builder;
123 if (defined($builder)) {
124 $value = "\$instance->$builder()";
126 elsif (ref($default) eq 'CODE') {
127 $value = "$attr_var\->{default}->(\$instance)";
129 elsif (defined($default)) {
130 $value = "$attr_var\->{default}";
137 $value = "$constraint_var->coerce($value)";
140 $code .= "$instance_slot = $value;\n";
141 $code .= $post_process;
144 elsif ($attr->is_required) {
145 $code .= "\$meta->throw_error('Attribute ($key) is required')";
146 $code .= " unless \$is_cloning;\n";
149 $code .= "}\n" if defined $init_arg;
155 push @res, q{if($used < keys %{$args})}
156 . q{{ $meta->_report_unknown_args(\@attrs, $args) }};
159 if($metaclass->is_anon_class){
160 push @res, q{$instance->{__METACLASS__} = $meta;};
164 unshift @res, q{my @triggers;};
165 push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
168 my $source = sprintf <<'EOT', __LINE__, __FILE__, $metaclass->name, join "\n", @res;
172 my($meta, $instance, $args, $is_cloning) = @_;
177 warn $source if $ENV{MOUSE_DEBUG};
181 $body = eval $source;
188 sub _generate_BUILDARGS {
189 my(undef, $metaclass) = @_;
191 my $class = $metaclass->name;
192 if ( $class->can('BUILDARGS') && $class->can('BUILDARGS') != \&Mouse::Object::BUILDARGS ) {
193 return 'my $args = $class->BUILDARGS(@_)';
198 if ( scalar @_ == 1 ) {
199 ( ref( $_[0] ) eq 'HASH' )
200 || Carp::confess "Single parameters to new() must be a HASH ref";
201 $args = +{ %{ $_[0] } };
209 sub _generate_BUILDALL {
210 my (undef, $metaclass) = @_;
212 return '' unless $metaclass->name->can('BUILD');
215 for my $class ($metaclass->linearized_isa) {
216 if (Mouse::Util::get_code_ref($class, 'BUILD')) {
217 unshift @code, qq{${class}::BUILD(\$instance, \$args);};
220 return join "\n", @code;
228 Mouse::Meta::Method::Constructor - A Mouse method generator for constructors
232 This document describes Mouse version 0.71
236 L<Moose::Meta::Method::Constructor>