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;
39 my ( $class, %options ) = @_;
44 'package_name' => $options{package_name},
45 'name' => $options{name},
46 # specific to this subclass
47 'options' => $options{options} || {},
48 'associated_metaclass' => $options{metaclass},
49 'is_inline' => ($options{is_inline} || 0),
55 sub options { (shift)->{'options'} }
56 sub associated_metaclass { (shift)->{'associated_metaclass'} }
62 $self->{'meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
67 $self->{'attributes'} ||= [ $self->associated_metaclass->compute_all_applicable_attributes ]
74 my $method_name = 'generate_constructor_method';
76 $method_name .= '_inline' if $self->is_inline;
78 $self->{'body'} = $self->$method_name;
81 sub generate_constructor_method {
82 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
85 sub generate_constructor_method_inline {
89 $source .= "\n" . 'my ($class, %params) = @_;';
91 $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(%params)';
92 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
94 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
95 $source .= ";\n" . (join ";\n" => map {
96 $self->_generate_slot_initializer($_)
97 } 0 .. (@{$self->attributes} - 1));
98 $source .= ";\n" . 'return $instance';
99 $source .= ";\n" . '}';
100 warn $source if $self->options->{debug};
105 # create the nessecary lexicals
106 # to be picked up in the eval
107 my $attrs = $self->attributes;
109 $code = eval $source;
110 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
115 sub _generate_slot_initializer {
119 my $attr = $self->attributes->[$index];
122 if ($attr->has_default) {
124 # default values can either be CODE refs
125 # in which case we need to call them. Or
126 # they can be scalars (strings/numbers)
127 # in which case we can just deal with them
128 # in the code we eval.
129 if ($attr->is_default_a_coderef) {
130 $default = '$attrs->[' . $index . ']->default($instance)';
133 $default = $attr->default;
134 # make sure to quote strings ...
135 unless (looks_like_number($default)) {
136 $default = "'$default'";
139 } elsif( $attr->has_builder ) {
140 $default = '$instance->'.$attr->builder;
143 if ( defined $attr->init_arg ) {
145 'if(exists $params{\'' . $attr->init_arg . '\'}){' . "\n" .
146 $self->meta_instance->inline_set_slot_value(
148 ("'" . $attr->name . "'"),
149 '$params{\'' . $attr->init_arg . '\'}' ) . "\n" .
150 '} ' . (!defined $default ? '' : 'else {' . "\n" .
151 $self->meta_instance->inline_set_slot_value(
153 ("'" . $attr->name . "'"),
157 } elsif ( defined $default ) {
159 $self->meta_instance->inline_set_slot_value(
161 ("'" . $attr->name . "'"),
175 Class::MOP::Method::Constructor - Method Meta Object for constructors
179 use Class::MOP::Method::Constructor;
181 my $constructor = Class::MOP::Method::Constructor->new(
182 metaclass => $metaclass,
184 debug => 1, # this is all for now
188 # calling the constructor ...
189 $constructor->body->($metaclass->name, %params);
193 This is a subclass of C<Class::MOP::Method> which deals with
194 class constructors. This is used when making a class immutable
195 to generate an optimized constructor.
201 =item B<new (metaclass => $meta, options => \%options)>
205 This returns the options HASH which is passed into C<new>.
207 =item B<associated_metaclass>
209 This returns the metaclass which is passed into C<new>.
213 This returns the list of attributes which are associated with the
214 metaclass which is passed into C<new>.
216 =item B<meta_instance>
218 This returns the meta instance which is associated with the
219 metaclass which is passed into C<new>.
223 This returns a boolean, but since constructors are very rarely
224 not inlined, this always returns true for now.
226 =item B<initialize_body>
228 This creates the code reference for the constructor itself.
232 =head2 Method Generators
236 =item B<generate_constructor_method>
238 =item B<generate_constructor_method_inline>
244 Stevan Little E<lt>stevan@iinteractive.comE<gt>
246 =head1 COPYRIGHT AND LICENSE
248 Copyright 2006-2008 by Infinity Interactive, Inc.
250 L<http://www.iinteractive.com>
252 This library is free software; you can redistribute it and/or modify
253 it under the same terms as Perl itself.