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 = $metaclass->{_mouse_cache}{_initialize_object} ||=
17 $class->_generate_initialize_object($metaclass);
19 my $source = sprintf(<<'EOT', __LINE__, __FILE__, $metaclass->name, $buildargs, $buildall);
24 return $class->Mouse::Object::new(@_)
25 if $class ne __PACKAGE__;
28 my $instance = bless {}, $class;
29 $metaclass->$initializer($instance, $args, 0);
46 sub _generate_initialize_object {
47 my ($method_class, $metaclass) = @_;
48 my @attrs = $metaclass->get_all_attributes;
50 my @checks = map { $_ && $_->_compiled_type_constraint }
51 map { $_->type_constraint } @attrs;
56 my $strict = $metaclass->strict_constructor;
59 push @res, 'my $used = 0;';
62 for my $index (0 .. @attrs - 1) {
65 my $attr = $attrs[$index];
66 my $key = $attr->name;
68 my $init_arg = $attr->init_arg;
69 my $type_constraint = $attr->type_constraint;
70 my $is_weak_ref = $attr->is_weak_ref;
73 my $instance_slot = $method_class->_inline_slot('$instance', $key);
74 my $attr_var = "\$attrs[$index]";
77 if(defined $type_constraint){
78 $constraint_var = "$attr_var\->{type_constraint}";
79 $need_coercion = ($attr->should_coerce && $type_constraint->has_coercion);
82 $code .= "# initialize $key\n";
84 my $post_process = '';
85 if(defined $type_constraint){
86 $post_process .= "\$checks[$index]->($instance_slot)\n";
87 $post_process .= " or $attr_var->_throw_type_constraint_error($instance_slot, $constraint_var);\n";
90 $post_process = "Scalar::Util::weaken($instance_slot) "
91 . "if ref $instance_slot;\n";
94 # build cde for an attribute
95 if (defined $init_arg) {
96 my $value = "\$args->{q{$init_arg}}";
98 $code .= "if (exists $value) {\n";
101 $value = "$constraint_var->coerce($value)";
104 $code .= "$instance_slot = $value;\n";
105 $code .= $post_process;
107 if ($attr->has_trigger) {
109 $code .= "push \@triggers, [$attr_var\->{trigger}, $instance_slot];\n";
113 $code .= '++$used;' . "\n";
116 $code .= "\n} else {\n"; # $value exists
119 if ($attr->has_default || $attr->has_builder) {
120 unless ($attr->is_lazy) {
121 my $default = $attr->default;
122 my $builder = $attr->builder;
125 if (defined($builder)) {
126 $value = "\$instance->$builder()";
128 elsif (ref($default) eq 'CODE') {
129 $value = "$attr_var\->{default}->(\$instance)";
131 elsif (defined($default)) {
132 $value = "$attr_var\->{default}";
139 $value = "$constraint_var->coerce($value)";
142 $code .= "$instance_slot = $value;\n";
143 $code .= $post_process;
146 elsif ($attr->is_required) {
147 $code .= "\$meta->throw_error('Attribute ($key) is required')";
148 $code .= " unless \$is_cloning;\n";
151 $code .= "}\n" if defined $init_arg;
157 push @res, q{if($used < keys %{$args})}
158 . q{{ $meta->_report_unknown_args(\@attrs, $args) }};
161 if($metaclass->is_anon_class){
162 push @res, q{$instance->{__METACLASS__} = $meta;};
166 unshift @res, q{my @triggers;};
167 push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
170 my $source = sprintf <<'EOT', __LINE__, __FILE__, $metaclass->name, join "\n", @res;
174 my($meta, $instance, $args, $is_cloning) = @_;
179 warn $source if $ENV{MOUSE_DEBUG};
183 $body = eval $source;
190 sub _generate_BUILDARGS {
191 my(undef, $metaclass) = @_;
193 my $class = $metaclass->name;
194 if ( $class->can('BUILDARGS') && $class->can('BUILDARGS') != \&Mouse::Object::BUILDARGS ) {
195 return 'my $args = $class->BUILDARGS(@_)';
200 if ( scalar @_ == 1 ) {
201 ( ref( $_[0] ) eq 'HASH' )
202 || Carp::confess "Single parameters to new() must be a HASH ref";
203 $args = +{ %{ $_[0] } };
211 sub _generate_BUILDALL {
212 my (undef, $metaclass) = @_;
214 return '' unless $metaclass->name->can('BUILD');
217 for my $class ($metaclass->linearized_isa) {
218 if (Mouse::Util::get_code_ref($class, 'BUILD')) {
219 unshift @code, qq{${class}::BUILD(\$instance, \$args);};
222 return join "\n", @code;
230 Mouse::Meta::Method::Constructor - A Mouse method generator for constructors
234 This document describes Mouse version 0.73
238 L<Moose::Meta::Method::Constructor>