2 package Class::MOP::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
10 our $VERSION = '0.04';
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};
26 '$!package_name' => $options{package_name},
27 '$!name' => $options{name},
28 # specific to this subclass
29 '%!options' => $options{options} || {},
30 '$!associated_metaclass' => $options{metaclass},
31 '$!is_inline' => ($options{is_inline} || 0),
34 # we don't want this creating
35 # a cycle in the code, if not
37 weaken($self->{'$!associated_metaclass'});
39 $self->initialize_body;
46 sub options { (shift)->{'%!options'} }
47 sub associated_metaclass { (shift)->{'$!associated_metaclass'} }
53 $self->{'$!meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
58 $self->{'@!attributes'} ||= [ $self->associated_metaclass->compute_all_applicable_attributes ]
65 my $method_name = 'generate_constructor_method';
67 $method_name .= '_inline' if $self->is_inline;
69 $self->{'&!body'} = $self->$method_name;
72 sub generate_constructor_method {
73 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
76 sub generate_constructor_method_inline {
80 $source .= "\n" . 'my ($class, %params) = @_;';
82 $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(%params)';
83 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
85 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
86 $source .= ";\n" . (join ";\n" => map {
87 $self->_generate_slot_initializer($_)
88 } 0 .. (@{$self->attributes} - 1));
89 $source .= ";\n" . 'return $instance';
90 $source .= ";\n" . '}';
91 warn $source if $self->options->{debug};
96 # create the nessecary lexicals
97 # to be picked up in the eval
98 my $attrs = $self->attributes;
100 $code = eval $source;
101 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
106 sub _generate_slot_initializer {
110 my $attr = $self->attributes->[$index];
113 if ($attr->has_default) {
115 # default values can either be CODE refs
116 # in which case we need to call them. Or
117 # they can be scalars (strings/numbers)
118 # in which case we can just deal with them
119 # in the code we eval.
120 if ($attr->is_default_a_coderef) {
121 $default = '$attrs->[' . $index . ']->default($instance)';
124 $default = $attr->default;
125 # make sure to quote strings ...
126 unless (looks_like_number($default)) {
127 $default = "'$default'";
130 } elsif( $attr->has_builder ) {
131 $default = '$instance->'.$attr->builder;
134 if ( defined $attr->init_arg ) {
136 'if(exists $params{\'' . $attr->init_arg . '\'}){' . "\n" .
137 $self->meta_instance->inline_set_slot_value(
139 ("'" . $attr->name . "'"),
140 '$params{\'' . $attr->init_arg . '\'}' ) . "\n" .
141 '} ' . (!defined $default ? '' : 'else {' . "\n" .
142 $self->meta_instance->inline_set_slot_value(
144 ("'" . $attr->name . "'"),
148 } elsif ( defined $default ) {
150 $self->meta_instance->inline_set_slot_value(
152 ("'" . $attr->name . "'"),
168 Class::MOP::Method::Constructor - Method Meta Object for constructors
172 use Class::MOP::Method::Constructor;
174 my $constructor = Class::MOP::Method::Constructor->new(
175 metaclass => $metaclass,
177 debug => 1, # this is all for now
181 # calling the constructor ...
182 $constructor->body->($metaclass->name, %params);
186 This is a subclass of C<Class::MOP::Method> which deals with
187 class constructors. This is used when making a class immutable
188 to generate an optimized constructor.
194 =item B<new (metaclass => $meta, options => \%options)>
198 This returns the options HASH which is passed into C<new>.
200 =item B<associated_metaclass>
202 This returns the metaclass which is passed into C<new>.
206 This returns the list of attributes which are associated with the
207 metaclass which is passed into C<new>.
209 =item B<meta_instance>
211 This returns the meta instance which is associated with the
212 metaclass which is passed into C<new>.
216 This returns a boolean, but since constructors are very rarely
217 not inlined, this always returns true for now.
219 =item B<initialize_body>
221 This creates the code reference for the constructor itself.
225 =head2 Method Generators
229 =item B<generate_constructor_method>
231 =item B<generate_constructor_method_inline>
237 Stevan Little E<lt>stevan@iinteractive.comE<gt>
239 =head1 COPYRIGHT AND LICENSE
241 Copyright 2006-2008 by Infinity Interactive, Inc.
243 L<http://www.iinteractive.com>
245 This library is free software; you can redistribute it and/or modify
246 it under the same terms as Perl itself.