1 package Mouse::Meta::Method::Constructor;
5 sub generate_constructor_method_inline {
6 my ($class, $meta) = @_;
8 my @attrs = $meta->compute_all_applicable_attributes; # this one is using by evaled code
9 my $buildall = $class->_generate_BUILDALL($meta);
10 my $buildargs = $class->_generate_BUILDARGS();
11 my $processattrs = $class->_generate_processattrs($meta, \@attrs);
16 my \$args = $buildargs;
17 my \$instance = bless {}, \$class;
24 warn $code if $ENV{DEBUG};
32 sub _generate_processattrs {
33 my ($class, $meta, $attrs) = @_;
35 for my $index (0..scalar(@$attrs)-1) {
36 my $attr = $attrs->[$index];
37 my $from = $attr->init_arg;
38 my $key = $attr->name;
43 push @code, "my \$value = \$args->{'$from'};";
45 if ($attr->should_coerce) {
46 push @code, "\$value = \$attr->coerce_constraint( \$value );";
49 if ($attr->has_type_constraint) {
50 push @code, "\$attr->verify_type_constraint( \$value );";
53 push @code, "\$instance->{'$key'} = \$value;";
55 if ($attr->is_weak_ref) {
56 push @code, "weaken( \$instance->{'$key'} ) if ref( \$value );";
59 if ( $attr->has_trigger ) {
60 push @code, "\$attr->trigger->( \$instance, \$value, \$attr );";
69 if ( $attr->has_default || $attr->has_builder ) {
70 unless ( $attr->is_lazy ) {
71 my $default = $attr->default;
72 my $builder = $attr->builder;
74 push @code, "my \$value = ";
76 if ($attr->should_coerce) {
77 push @code, "\$attr->coerce_constraint(";
80 if ($attr->has_builder) {
81 push @code, "\$instance->$builder";
83 elsif (ref($default) eq 'CODE') {
84 push @code, "\$attr->default()->()";
87 push @code, "\$attr->default()";
90 if ($attr->should_coerce) {
97 if ($attr->has_type_constraint) {
98 push @code, "\$attr->verify_type_constraint(\$value);";
101 push @code, "\$instance->{'$key'} = \$value;";
103 if ($attr->is_weak_ref) {
104 push @code, "weaken( \$instance->{'$key'} ) if ref( \$value );";
110 if ( $attr->is_required ) {
111 qq{Carp::confess("Attribute ($key) is required");};
119 my \$attr = \$attrs[$index];
120 if (exists(\$args->{'$from'})) {
129 return join "\n", @res;
132 sub _generate_BUILDARGS {
135 if ( scalar @_ == 1 ) {
136 if ( defined $_[0] ) {
137 ( ref( $_[0] ) eq 'HASH' )
138 || Carp::confess "Single parameters to new() must be a HASH ref";
152 sub _generate_BUILDALL {
153 my ($class, $meta) = @_;
154 return '' unless $meta->name->can('BUILD');
157 push @code, q{no strict 'refs';};
158 push @code, q{no warnings 'once';};
160 for my $class ($meta->linearized_isa) {
161 if (*{ $class . '::BUILD' }{CODE}) {
162 push @code, qq{${class}::BUILD->(\$instance, \$args);};
165 return join "\n", @code;