don't close over attrs in the constructor unless necessary
[gitmo/Moose.git] / lib / Moose / Meta / Method / Constructor.pm
CommitLineData
5cf3dbcf 1
2package Moose::Meta::Method::Constructor;
3
4use strict;
5use warnings;
6
0d922627 7use Carp ();
3eeaf081 8use List::MoreUtils 'any';
0fa70d03 9use Scalar::Util 'blessed', 'weaken', 'looks_like_number', 'refaddr';
55c361dc 10use Try::Tiny;
5cf3dbcf 11
badb7e89 12use base 'Moose::Meta::Method',
bc89e9b5 13 'Class::MOP::Method::Constructor';
5cf3dbcf 14
15sub new {
16 my $class = shift;
17 my %options = @_;
7a5b07b3 18
3e504337 19 my $meta = $options{metaclass};
20
21 (ref $options{options} eq 'HASH')
a9538ac9 22 || $class->throw_error("You must pass a hash of options", data => $options{options});
7a5b07b3 23
1b2aea39 24 ($options{package_name} && $options{name})
a9538ac9 25 || $class->throw_error("You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT");
1b2aea39 26
5cf3dbcf 27 my $self = bless {
d03bd989 28 'body' => undef,
e606ae5f 29 'package_name' => $options{package_name},
30 'name' => $options{name},
e606ae5f 31 'options' => $options{options},
e606ae5f 32 'associated_metaclass' => $meta,
0f1a71fc 33 'definition_context' => $options{definition_context},
0fa70d03 34 '_expected_method_class' => $options{_expected_method_class} || 'Moose::Object',
5cf3dbcf 35 } => $class;
36
7a5b07b3 37 # we don't want this creating
38 # a cycle in the code, if not
5cf3dbcf 39 # needed
e606ae5f 40 weaken($self->{'associated_metaclass'});
5cf3dbcf 41
f5b0af77 42 $self->_initialize_body;
5cf3dbcf 43
7a5b07b3 44 return $self;
5cf3dbcf 45}
46
5cf3dbcf 47## method
48
f5b0af77 49sub _initialize_body {
5cf3dbcf 50 my $self = shift;
e247d17c 51 $self->{'body'} = $self->_generate_constructor_method_inline;
52}
53
54sub _eval_environment {
55 my $self = shift;
56
57 my $attrs = $self->_attributes;
58
59 my $defaults = [map { $_->default } @$attrs];
3eeaf081 60 my $triggers = [
61 map { $_->can('has_trigger') && $_->has_trigger ? $_->trigger : undef }
62 @$attrs
63 ];
e247d17c 64
65 # We need to check if the attribute ->can('type_constraint')
66 # since we may be trying to immutabilize a Moose meta class,
67 # which in turn has attributes which are Class::MOP::Attribute
68 # objects, rather than Moose::Meta::Attribute. And
69 # Class::MOP::Attribute attributes have no type constraints.
70 # However we need to make sure we leave an undef value there
71 # because the inlined code is using the index of the attributes
72 # to determine where to find the type constraint
73
74 my @type_constraints = map {
75 $_->can('type_constraint') ? $_->type_constraint : undef
76 } @$attrs;
77
78 my @type_constraint_bodies = map {
79 defined $_ ? $_->_compiled_type_constraint : undef;
80 } @type_constraints;
81
c40e4359 82 my @type_coercions = map {
83 defined $_ && $_->has_coercion
84 ? $_->coercion->_compiled_type_coercion
85 : undef
86 } @type_constraints;
87
e247d17c 88 return {
89 '$meta' => \$self,
3eeaf081 90 ((any { defined && $_->has_initializer } @$attrs)
91 ? ('$attrs' => \$attrs)
92 : ()),
e247d17c 93 '$defaults' => \$defaults,
3eeaf081 94 '$triggers' => \$triggers,
e247d17c 95 '@type_constraints' => \@type_constraints,
c40e4359 96 '@type_coercions' => \@type_coercions,
e247d17c 97 '@type_constraint_bodies' => \@type_constraint_bodies,
78fa4d7d 98 ( map { defined($_) ? %{ $_->inline_environment } : () }
99 @type_constraints ),
e247d17c 100 };
101}
102
5cf3dbcf 1031;
104
ad46f524 105# ABSTRACT: Method Meta Object for constructors
106
5cf3dbcf 107__END__
108
109=pod
110
5cf3dbcf 111=head1 DESCRIPTION
112
cec39889 113This class is a subclass of L<Class::MOP::Method::Constructor> that
cefc9e36 114provides additional Moose-specific functionality
115
116To understand this class, you should read the the
cec39889 117L<Class::MOP::Method::Constructor> documentation as well.
d44714be 118
bc89e9b5 119=head1 INHERITANCE
120
121C<Moose::Meta::Method::Constructor> is a subclass of
122L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Constructor>.
123
c5fc2c21 124=head1 BUGS
125
126See L<Moose/BUGS> for details on reporting bugs.
127
5cf3dbcf 128=cut
129