2 package Moose::Meta::Method::Constructor;
8 use List::MoreUtils 'any';
9 use Scalar::Util 'blessed', 'weaken', 'looks_like_number', 'refaddr';
12 use base 'Moose::Meta::Method',
13 'Class::MOP::Method::Constructor';
19 my $meta = $options{metaclass};
21 (ref $options{options} eq 'HASH')
22 || $class->throw_error("You must pass a hash of options", data => $options{options});
24 ($options{package_name} && $options{name})
25 || $class->throw_error("You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT");
29 'package_name' => $options{package_name},
30 'name' => $options{name},
31 'options' => $options{options},
32 'associated_metaclass' => $meta,
33 'definition_context' => $options{definition_context},
34 '_expected_method_class' => $options{_expected_method_class} || 'Moose::Object',
37 # we don't want this creating
38 # a cycle in the code, if not
40 weaken($self->{'associated_metaclass'});
42 $self->_initialize_body;
49 sub _initialize_body {
51 $self->{'body'} = $self->_generate_constructor_method_inline;
54 sub _eval_environment {
57 my $attrs = $self->_attributes;
59 my $defaults = [map { $_->default } @$attrs];
61 map { $_->can('has_trigger') && $_->has_trigger ? $_->trigger : undef }
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
74 my @type_constraints = map {
75 $_->can('type_constraint') ? $_->type_constraint : undef
78 my @type_constraint_bodies = map {
79 defined $_ ? $_->_compiled_type_constraint : undef;
82 my @type_coercions = map {
83 defined $_ && $_->has_coercion
84 ? $_->coercion->_compiled_type_coercion
90 ((any { defined && $_->has_initializer } @$attrs)
91 ? ('$attrs' => \$attrs)
93 '$defaults' => \$defaults,
94 '$triggers' => \$triggers,
95 '@type_constraints' => \@type_constraints,
96 '@type_coercions' => \@type_coercions,
97 '@type_constraint_bodies' => \@type_constraint_bodies,
98 ( map { defined($_) ? %{ $_->inline_environment } : () }
105 # ABSTRACT: Method Meta Object for constructors
113 This class is a subclass of L<Class::MOP::Method::Constructor> that
114 provides additional Moose-specific functionality
116 To understand this class, you should read the the
117 L<Class::MOP::Method::Constructor> documentation as well.
121 C<Moose::Meta::Method::Constructor> is a subclass of
122 L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Constructor>.
126 See L<Moose/BUGS> for details on reporting bugs.