1 package Mouse::Meta::Method::Constructor;
2 use Mouse::Util; # enables strict and warnings
4 sub _generate_constructor {
5 my ($class, $metaclass, $args) = @_;
7 my $associated_metaclass_name = $metaclass->name;
9 my @attrs = $metaclass->get_all_attributes;
11 my $buildall = $class->_generate_BUILDALL($metaclass);
12 my $buildargs = $class->_generate_BUILDARGS($metaclass);
13 my $processattrs = $class->_generate_processattrs($metaclass, \@attrs);
15 my @checks = map { $_ && $_->_compiled_type_constraint }
16 map { $_->type_constraint } @attrs;
18 my $source = sprintf("#line %d %s\n", __LINE__, __FILE__).<<"...";
21 return \$class->Mouse::Object::new(\@_)
22 if \$class ne q{$associated_metaclass_name};
25 my \$instance = bless {}, \$class;
44 sub _generate_processattrs {
45 my ($class, $metaclass, $attrs) = @_;
50 for my $index (0 .. @$attrs - 1) {
53 my $attr = $attrs->[$index];
54 my $key = $attr->name;
56 my $init_arg = $attr->init_arg;
57 my $type_constraint = $attr->type_constraint;
60 my $instance_slot = "\$instance->{q{$key}}";
61 my $attr_var = "\$attrs[$index]";
64 if(defined $type_constraint){
65 $constraint_var = "$attr_var\->{type_constraint}";
66 $need_coercion = ($attr->should_coerce && $type_constraint->has_coercion);
69 $code .= "# initialize $key\n";
71 my $post_process = '';
72 if(defined $type_constraint){
73 $post_process .= "\$checks[$index]->($instance_slot)";
74 $post_process .= " or $attr_var->verify_type_constraint_error(q{$key}, $instance_slot, $constraint_var);\n";
76 if($attr->is_weak_ref){
77 $post_process .= "Scalar::Util::weaken($instance_slot) if ref $instance_slot;\n";
80 if (defined $init_arg) {
81 my $value = "\$args->{q{$init_arg}}";
83 $code .= "if (exists $value) {\n";
86 $value = "$constraint_var->coerce($value)";
89 $code .= "$instance_slot = $value;\n";
90 $code .= $post_process;
92 if ($attr->has_trigger) {
94 $code .= "push \@triggers, [$attr_var\->{trigger}, $instance_slot];\n";
97 $code .= "\n} else {\n";
100 if ($attr->has_default || $attr->has_builder) {
101 unless ($attr->is_lazy) {
102 my $default = $attr->default;
103 my $builder = $attr->builder;
106 if (defined($builder)) {
107 $value = "\$instance->$builder()";
109 elsif (ref($default) eq 'CODE') {
110 $value = "$attr_var\->{default}->(\$instance)";
112 elsif (defined($default)) {
113 $value = "$attr_var\->{default}";
120 $value = "$constraint_var->coerce($value)";
123 $code .= "$instance_slot = $value;\n";
126 elsif ($attr->is_required) {
127 $code .= "Carp::confess('Attribute ($key) is required');";
130 $code .= "}\n" if defined $init_arg;
135 if($metaclass->is_anon_class){
136 push @res, q{$instance->{__METACLASS__} = $metaclass;};
140 unshift @res, q{my @triggers;};
141 push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
144 return join "\n", @res;
147 sub _generate_BUILDARGS {
148 my(undef, $metaclass) = @_;
150 my $class = $metaclass->name;
151 if ( $class->can('BUILDARGS') && $class->can('BUILDARGS') != \&Mouse::Object::BUILDARGS ) {
152 return 'my $args = $class->BUILDARGS(@_)';
157 if ( scalar @_ == 1 ) {
158 ( ref( $_[0] ) eq 'HASH' )
159 || Carp::confess "Single parameters to new() must be a HASH ref";
160 $args = +{ %{ $_[0] } };
168 sub _generate_BUILDALL {
169 my (undef, $metaclass) = @_;
171 return '' unless $metaclass->name->can('BUILD');
174 for my $class ($metaclass->linearized_isa) {
178 if (*{ $class . '::BUILD' }{CODE}) {
179 unshift @code, qq{${class}::BUILD(\$instance, \$args);};
182 return join "\n", @code;
190 Mouse::Meta::Method::Constructor - A Mouse method generator for constructors
194 This document describes Mouse version 0.39
198 L<Moose::Meta::Method::Constructor>