2 package Class::MOP::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken';
10 our $VERSION = '1.12';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Class::MOP::Method::Inlined';
20 (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
21 || confess "You must pass a metaclass instance if you want to inline"
22 if $options{is_inline};
24 ($options{package_name} && $options{name})
25 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
27 my $self = $class->_new(\%options);
29 # we don't want this creating
30 # a cycle in the code, if not
32 weaken($self->{'associated_metaclass'});
34 $self->_initialize_body;
42 return Class::MOP::Class->initialize($class)->new_object(@_)
43 if $class ne __PACKAGE__;
45 my $params = @_ == 1 ? $_[0] : {@_};
48 # inherited from Class::MOP::Method
49 body => $params->{body},
50 # associated_metaclass => $params->{associated_metaclass}, # overriden
51 package_name => $params->{package_name},
52 name => $params->{name},
53 original_method => $params->{original_method},
55 # inherited from Class::MOP::Generated
56 is_inline => $params->{is_inline} || 0,
57 definition_context => $params->{definition_context},
59 # inherited from Class::MOP::Inlined
60 _expected_method_class => $params->{_expected_method_class},
62 # defined in this subclass
63 options => $params->{options} || {},
64 associated_metaclass => $params->{metaclass},
70 sub options { (shift)->{'options'} }
71 sub associated_metaclass { (shift)->{'associated_metaclass'} }
77 $self->{'attributes'} ||= [ $self->associated_metaclass->get_all_attributes ]
82 sub _initialize_body {
84 my $method_name = '_generate_constructor_method';
86 $method_name .= '_inline' if $self->is_inline;
88 $self->{'body'} = $self->$method_name;
91 sub _generate_constructor_method {
92 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
95 sub _generate_constructor_method_inline {
98 my $defaults = [map { $_->default } @{ $self->_attributes }];
101 '$defaults' => \$defaults,
104 my $source = 'sub {';
105 $source .= "\n" . 'my $class = shift;';
107 $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(@_)';
108 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
110 $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};';
112 $source .= "\n" . 'my $instance = ' . $self->associated_metaclass->inline_create_instance('$class');
114 $source .= ";\n" . (join ";\n" => map {
115 $self->_generate_slot_initializer($_, $idx++)
116 } @{ $self->_attributes });
117 if (Class::MOP::metaclass_is_weak($self->associated_metaclass->name)) {
118 $source .= ";\n" . $self->associated_metaclass->_inline_set_mop_slot('$instance', 'Class::MOP::class_of($class)');
120 $source .= ";\n" . 'return $instance';
121 $source .= ";\n" . '}';
122 warn $source if $self->options->{debug};
124 my ( $code, $e ) = $self->_eval_closure(
128 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e" if $e;
133 sub _generate_slot_initializer {
139 if ($attr->has_default) {
140 $default = $self->_generate_default_value($attr, $idx);
141 } elsif( $attr->has_builder ) {
142 $default = '$instance->'.$attr->builder;
145 if ( defined( my $init_arg = $attr->init_arg ) ) {
147 'if(exists $params->{\''
148 . $init_arg . '\'}){' . "\n"
151 '$params->{\'' . $init_arg . '\'}'
155 !defined $default ? '' : 'else {' . "\n"
164 elsif ( defined $default ) {
178 sub _generate_default_value {
179 my ($self, $attr, $index) = @_;
181 # default values can either be CODE refs
182 # in which case we need to call them. Or
183 # they can be scalars (strings/numbers)
184 # in which case we can just deal with them
185 # in the code we eval.
186 if ($attr->is_default_a_coderef) {
187 return '$defaults->[' . $index . ']->($instance)';
190 return '$defaults->[' . $index . ']';
202 Class::MOP::Method::Constructor - Method Meta Object for constructors
206 use Class::MOP::Method::Constructor;
208 my $constructor = Class::MOP::Method::Constructor->new(
209 metaclass => $metaclass,
211 debug => 1, # this is all for now
215 # calling the constructor ...
216 $constructor->body->execute($metaclass->name, %params);
220 This is a subclass of C<Class::MOP::Method> which generates
227 =item B<< Class::MOP::Method::Constructor->new(%options) >>
229 This creates a new constructor object. It accepts a hash reference of
236 This should be a L<Class::MOP::Class> object. It is required.
240 The method name (without a package name). This is required.
244 The package name for the method. This is required.
248 This indicates whether or not the constructor should be inlined. This
253 =item B<< $metamethod->is_inline >>
255 Returns a boolean indicating whether or not the constructor is
258 =item B<< $metamethod->associated_metaclass >>
260 This returns the L<Class::MOP::Class> object for the method.
266 Stevan Little E<lt>stevan@iinteractive.comE<gt>
268 =head1 COPYRIGHT AND LICENSE
270 Copyright 2006-2010 by Infinity Interactive, Inc.
272 L<http://www.iinteractive.com>
274 This library is free software; you can redistribute it and/or modify
275 it under the same terms as Perl itself.