2 package Class::MOP::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
10 our $VERSION = '0.06';
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";
29 '$!package_name' => $options{package_name},
30 '$!name' => $options{name},
31 # specific to this subclass
32 '%!options' => $options{options} || {},
33 '$!associated_metaclass' => $options{metaclass},
34 '$!is_inline' => ($options{is_inline} || 0),
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 options { (shift)->{'%!options'} }
50 sub associated_metaclass { (shift)->{'$!associated_metaclass'} }
56 $self->{'$!meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
61 $self->{'@!attributes'} ||= [ $self->associated_metaclass->compute_all_applicable_attributes ]
68 my $method_name = 'generate_constructor_method';
70 $method_name .= '_inline' if $self->is_inline;
72 $self->{'&!body'} = $self->$method_name;
75 sub generate_constructor_method {
76 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
79 sub generate_constructor_method_inline {
83 $source .= "\n" . 'my ($class, %params) = @_;';
85 $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(%params)';
86 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
88 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
89 $source .= ";\n" . (join ";\n" => map {
90 $self->_generate_slot_initializer($_)
91 } 0 .. (@{$self->attributes} - 1));
92 $source .= ";\n" . 'return $instance';
93 $source .= ";\n" . '}';
94 warn $source if $self->options->{debug};
99 # create the nessecary lexicals
100 # to be picked up in the eval
101 my $attrs = $self->attributes;
103 $code = eval $source;
104 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
109 sub _generate_slot_initializer {
113 my $attr = $self->attributes->[$index];
116 if ($attr->has_default) {
118 # default values can either be CODE refs
119 # in which case we need to call them. Or
120 # they can be scalars (strings/numbers)
121 # in which case we can just deal with them
122 # in the code we eval.
123 if ($attr->is_default_a_coderef) {
124 $default = '$attrs->[' . $index . ']->default($instance)';
127 $default = $attr->default;
128 # make sure to quote strings ...
129 unless (looks_like_number($default)) {
130 $default = "'$default'";
133 } elsif( $attr->has_builder ) {
134 $default = '$instance->'.$attr->builder;
137 if ( defined $attr->init_arg ) {
139 'if(exists $params{\'' . $attr->init_arg . '\'}){' . "\n" .
140 $self->meta_instance->inline_set_slot_value(
142 ("'" . $attr->name . "'"),
143 '$params{\'' . $attr->init_arg . '\'}' ) . "\n" .
144 '} ' . (!defined $default ? '' : 'else {' . "\n" .
145 $self->meta_instance->inline_set_slot_value(
147 ("'" . $attr->name . "'"),
151 } elsif ( defined $default ) {
153 $self->meta_instance->inline_set_slot_value(
155 ("'" . $attr->name . "'"),
171 Class::MOP::Method::Constructor - Method Meta Object for constructors
175 use Class::MOP::Method::Constructor;
177 my $constructor = Class::MOP::Method::Constructor->new(
178 metaclass => $metaclass,
180 debug => 1, # this is all for now
184 # calling the constructor ...
185 $constructor->body->($metaclass->name, %params);
189 This is a subclass of C<Class::MOP::Method> which deals with
190 class constructors. This is used when making a class immutable
191 to generate an optimized constructor.
197 =item B<new (metaclass => $meta, options => \%options)>
201 This returns the options HASH which is passed into C<new>.
203 =item B<associated_metaclass>
205 This returns the metaclass which is passed into C<new>.
209 This returns the list of attributes which are associated with the
210 metaclass which is passed into C<new>.
212 =item B<meta_instance>
214 This returns the meta instance which is associated with the
215 metaclass which is passed into C<new>.
219 This returns a boolean, but since constructors are very rarely
220 not inlined, this always returns true for now.
222 =item B<initialize_body>
224 This creates the code reference for the constructor itself.
228 =head2 Method Generators
232 =item B<generate_constructor_method>
234 =item B<generate_constructor_method_inline>
240 Stevan Little E<lt>stevan@iinteractive.comE<gt>
242 =head1 COPYRIGHT AND LICENSE
244 Copyright 2006-2008 by Infinity Interactive, Inc.
246 L<http://www.iinteractive.com>
248 This library is free software; you can redistribute it and/or modify
249 it under the same terms as Perl itself.