2 package Class::MOP::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
10 our $VERSION = '0.65';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Class::MOP::Method::Generated';
19 (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
20 || confess "You must pass a metaclass instance if you want to inline"
21 if $options{is_inline};
23 ($options{package_name} && $options{name})
24 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
26 my $self = $class->_new(\%options);
28 # we don't want this creating
29 # a cycle in the code, if not
31 weaken($self->{'associated_metaclass'});
33 $self->initialize_body;
40 my $options = @_ == 1 ? $_[0] : {@_};
45 'package_name' => $options->{package_name},
46 'name' => $options->{name},
47 # specific to this subclass
48 'options' => $options->{options} || {},
49 'associated_metaclass' => $options->{metaclass},
50 'is_inline' => ($options->{is_inline} || 0),
56 sub options { (shift)->{'options'} }
57 sub associated_metaclass { (shift)->{'associated_metaclass'} }
63 $self->{'meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
68 $self->{'attributes'} ||= [ $self->associated_metaclass->compute_all_applicable_attributes ]
75 my $method_name = 'generate_constructor_method';
77 $method_name .= '_inline' if $self->is_inline;
79 $self->{'body'} = $self->$method_name;
82 sub generate_constructor_method {
83 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
86 sub generate_constructor_method_inline {
90 $source .= "\n" . 'my $class = shift;';
92 $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(@_)';
93 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
95 $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};';
97 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
98 $source .= ";\n" . (join ";\n" => map {
99 $self->_generate_slot_initializer($_)
100 } 0 .. (@{$self->attributes} - 1));
101 $source .= ";\n" . 'return $instance';
102 $source .= ";\n" . '}';
103 warn $source if $self->options->{debug};
108 # create the nessecary lexicals
109 # to be picked up in the eval
110 my $attrs = $self->attributes;
112 $code = eval $source;
113 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
118 sub _generate_slot_initializer {
122 my $attr = $self->attributes->[$index];
125 if ($attr->has_default) {
127 # default values can either be CODE refs
128 # in which case we need to call them. Or
129 # they can be scalars (strings/numbers)
130 # in which case we can just deal with them
131 # in the code we eval.
132 if ($attr->is_default_a_coderef) {
133 $default = '$attrs->[' . $index . ']->default($instance)';
136 $default = $attr->default;
137 # make sure to quote strings ...
138 unless (looks_like_number($default)) {
139 $default = "'$default'";
142 } elsif( $attr->has_builder ) {
143 $default = '$instance->'.$attr->builder;
146 if ( defined $attr->init_arg ) {
148 'if(exists $params->{\'' . $attr->init_arg . '\'}){' . "\n" .
149 $self->meta_instance->inline_set_slot_value(
151 ("'" . $attr->name . "'"),
152 '$params->{\'' . $attr->init_arg . '\'}' ) . "\n" .
153 '} ' . (!defined $default ? '' : 'else {' . "\n" .
154 $self->meta_instance->inline_set_slot_value(
156 ("'" . $attr->name . "'"),
160 } elsif ( defined $default ) {
162 $self->meta_instance->inline_set_slot_value(
164 ("'" . $attr->name . "'"),
178 Class::MOP::Method::Constructor - Method Meta Object for constructors
182 use Class::MOP::Method::Constructor;
184 my $constructor = Class::MOP::Method::Constructor->new(
185 metaclass => $metaclass,
187 debug => 1, # this is all for now
191 # calling the constructor ...
192 $constructor->body->($metaclass->name, %params);
196 This is a subclass of C<Class::MOP::Method> which deals with
197 class constructors. This is used when making a class immutable
198 to generate an optimized constructor.
204 =item B<new (metaclass => $meta, options => \%options)>
208 This returns the options HASH which is passed into C<new>.
210 =item B<associated_metaclass>
212 This returns the metaclass which is passed into C<new>.
216 This returns the list of attributes which are associated with the
217 metaclass which is passed into C<new>.
219 =item B<meta_instance>
221 This returns the meta instance which is associated with the
222 metaclass which is passed into C<new>.
226 This returns a boolean, but since constructors are very rarely
227 not inlined, this always returns true for now.
229 =item B<initialize_body>
231 This creates the code reference for the constructor itself.
235 =head2 Method Generators
239 =item B<generate_constructor_method>
241 =item B<generate_constructor_method_inline>
247 Stevan Little E<lt>stevan@iinteractive.comE<gt>
249 =head1 COPYRIGHT AND LICENSE
251 Copyright 2006-2008 by Infinity Interactive, Inc.
253 L<http://www.iinteractive.com>
255 This library is free software; you can redistribute it and/or modify
256 it under the same terms as Perl itself.