1 package Mouse::Meta::Method::Constructor;
2 use Mouse::Util qw(:meta); # enables strict and warnings
4 use constant _MOUSE_DEBUG => $ENV{MOUSE_DEBUG} ? 1 : 0;
7 my(undef, $self_var, $attr_name) = @_;
8 return sprintf '%s->{q{%s}}', $self_var, $attr_name;
11 sub _generate_constructor {
12 my ($class, $metaclass, $args) = @_;
14 my $associated_metaclass_name = $metaclass->name;
16 my $buildall = $class->_generate_BUILDALL($metaclass);
17 my $buildargs = $class->_generate_BUILDARGS($metaclass);
18 my $initializer = $metaclass->{_mouse_cache}{_initialize_object} ||=
19 $class->_generate_initialize_object($metaclass);
20 my $source = sprintf(<<'EOT', __FILE__, $metaclass->name, $buildargs, $buildall);
25 return $class->Mouse::Object::new(@_)
26 if $class ne __PACKAGE__;
29 my $instance = bless {}, $class;
30 $metaclass->$initializer($instance, $args, 0);
36 warn $source if _MOUSE_DEBUG;
47 sub _generate_initialize_object {
48 my ($method_class, $metaclass) = @_;
49 my @attrs = $metaclass->get_all_attributes;
51 my @checks = map { $_ && $_->_compiled_type_constraint }
52 map { $_->type_constraint } @attrs;
57 my $strict = $metaclass->strict_constructor;
60 push @res, 'my $used = 0;';
63 for my $index (0 .. @attrs - 1) {
66 my $attr = $attrs[$index];
67 my $key = $attr->name;
69 my $init_arg = $attr->init_arg;
70 my $type_constraint = $attr->type_constraint;
71 my $is_weak_ref = $attr->is_weak_ref;
74 my $instance_slot = $method_class->_inline_slot('$instance', $key);
75 my $attr_var = "\$attrs[$index]";
78 if(defined $type_constraint){
79 $constraint_var = "$attr_var\->{type_constraint}";
80 $need_coercion = ($attr->should_coerce && $type_constraint->has_coercion);
83 $code .= "# initialize $key\n";
85 my $post_process = '';
86 if(defined $type_constraint){
87 $post_process .= "\$checks[$index]->($instance_slot)\n";
88 $post_process .= " or $attr_var->_throw_type_constraint_error($instance_slot, $constraint_var);\n";
91 # build cde for an attribute
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";
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";
140 $code .= $post_process;
143 elsif ($attr->is_required) {
144 $code .= "\$meta->throw_error('Attribute ($key) is required')";
145 $code .= " unless \$is_cloning;\n";
148 $code .= "}\n" if defined $init_arg;
151 $code .= "Scalar::Util::weaken($instance_slot) "
152 . "if ref $instance_slot;\n";
159 push @res, q{if($used < keys %{$args})}
160 . q{{ $meta->_report_unknown_args(\@attrs, $args) }};
163 if($metaclass->is_anon_class){
164 push @res, q{$instance->{__METACLASS__} = $meta;};
168 unshift @res, q{my @triggers;};
169 push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
172 my $source = sprintf <<'EOT', __FILE__, $metaclass->name, join "\n", @res;
176 my($meta, $instance, $args, $is_cloning) = @_;
181 warn $source if _MOUSE_DEBUG;
185 $body = eval $source;
192 sub _generate_BUILDARGS {
193 my(undef, $metaclass) = @_;
195 my $class = $metaclass->name;
196 if ( $class->can('BUILDARGS') && $class->can('BUILDARGS') != \&Mouse::Object::BUILDARGS ) {
197 return 'my $args = $class->BUILDARGS(@_)';
202 if ( scalar @_ == 1 ) {
203 ( ref( $_[0] ) eq 'HASH' )
204 || Carp::confess "Single parameters to new() must be a HASH ref";
205 $args = +{ %{ $_[0] } };
213 sub _generate_BUILDALL {
214 my (undef, $metaclass) = @_;
216 return '' unless $metaclass->name->can('BUILD');
219 for my $class ($metaclass->linearized_isa) {
220 if (Mouse::Util::get_code_ref($class, 'BUILD')) {
221 unshift @code, qq{${class}::BUILD(\$instance, \$args);};
224 return join "\n", @code;
232 Mouse::Meta::Method::Constructor - A Mouse method generator for constructors
236 This document describes Mouse version 0.92
240 L<Moose::Meta::Method::Constructor>