1 package Mouse::Meta::Method::Constructor;
2 use Mouse::Util qw(get_code_ref); # 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) = @_;
55 for my $index (0 .. @$attrs - 1) {
58 my $attr = $attrs->[$index];
59 my $key = $attr->name;
61 my $init_arg = $attr->init_arg;
62 my $type_constraint = $attr->type_constraint;
65 my $instance_slot = $method_class->_inline_slot('$instance', $key);
66 my $attr_var = "\$attrs[$index]";
69 if(defined $type_constraint){
70 $constraint_var = "$attr_var\->{type_constraint}";
71 $need_coercion = ($attr->should_coerce && $type_constraint->has_coercion);
74 $code .= "# initialize $key\n";
76 my $post_process = '';
77 if(defined $type_constraint){
78 $post_process .= "\$checks[$index]->($instance_slot)";
79 $post_process .= " or $attr_var->verify_type_constraint_error(q{$key}, $instance_slot, $constraint_var);\n";
81 if($attr->is_weak_ref){
82 $post_process .= "Scalar::Util::weaken($instance_slot) if ref $instance_slot;\n";
85 if (defined $init_arg) {
86 my $value = "\$args->{q{$init_arg}}";
88 $code .= "if (exists $value) {\n";
91 $value = "$constraint_var->coerce($value)";
94 $code .= "$instance_slot = $value;\n";
95 $code .= $post_process;
97 if ($attr->has_trigger) {
99 $code .= "push \@triggers, [$attr_var\->{trigger}, $instance_slot];\n";
102 $code .= "\n} else {\n";
105 if ($attr->has_default || $attr->has_builder) {
106 unless ($attr->is_lazy) {
107 my $default = $attr->default;
108 my $builder = $attr->builder;
111 if (defined($builder)) {
112 $value = "\$instance->$builder()";
114 elsif (ref($default) eq 'CODE') {
115 $value = "$attr_var\->{default}->(\$instance)";
117 elsif (defined($default)) {
118 $value = "$attr_var\->{default}";
125 $value = "$constraint_var->coerce($value)";
128 $code .= "$instance_slot = $value;\n";
131 elsif ($attr->is_required) {
132 $code .= "Carp::confess('Attribute ($key) is required');";
135 $code .= "}\n" if defined $init_arg;
140 if($metaclass->is_anon_class){
141 push @res, q{$instance->{__METACLASS__} = $metaclass;};
145 unshift @res, q{my @triggers;};
146 push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
149 return join "\n", @res;
152 sub _generate_BUILDARGS {
153 my(undef, $metaclass) = @_;
155 my $class = $metaclass->name;
156 if ( $class->can('BUILDARGS') && $class->can('BUILDARGS') != \&Mouse::Object::BUILDARGS ) {
157 return 'my $args = $class->BUILDARGS(@_)';
162 if ( scalar @_ == 1 ) {
163 ( ref( $_[0] ) eq 'HASH' )
164 || Carp::confess "Single parameters to new() must be a HASH ref";
165 $args = +{ %{ $_[0] } };
173 sub _generate_BUILDALL {
174 my (undef, $metaclass) = @_;
176 return '' unless $metaclass->name->can('BUILD');
179 for my $class ($metaclass->linearized_isa) {
180 if (get_code_ref($class, 'BUILD')) {
181 unshift @code, qq{${class}::BUILD(\$instance, \$args);};
184 return join "\n", @code;
192 Mouse::Meta::Method::Constructor - A Mouse method generator for constructors
196 This document describes Mouse version 0.40_04
200 L<Moose::Meta::Method::Constructor>