2 package Class::MOP::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
10 our $VERSION = '0.64_02';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Class::MOP::Method::Generated';
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;
41 my $options = @_ == 1 ? $_[0] : {@_};
46 'package_name' => $options->{package_name},
47 'name' => $options->{name},
48 # specific to this subclass
49 'options' => $options->{options} || {},
50 'associated_metaclass' => $options->{metaclass},
51 'is_inline' => ($options->{is_inline} || 0),
57 sub options { (shift)->{'options'} }
58 sub associated_metaclass { (shift)->{'associated_metaclass'} }
64 $self->{'meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
69 $self->{'attributes'} ||= [ $self->associated_metaclass->compute_all_applicable_attributes ]
76 my $method_name = 'generate_constructor_method';
78 $method_name .= '_inline' if $self->is_inline;
80 $self->{'body'} = $self->$method_name;
83 sub generate_constructor_method {
84 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
87 sub generate_constructor_method_inline {
91 $source .= "\n" . 'my $class = shift;';
93 $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(@_)';
94 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
96 $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};';
98 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
99 $source .= ";\n" . (join ";\n" => map {
100 $self->_generate_slot_initializer($_)
101 } 0 .. (@{$self->attributes} - 1));
102 $source .= ";\n" . 'return $instance';
103 $source .= ";\n" . '}';
104 warn $source if $self->options->{debug};
109 # create the nessecary lexicals
110 # to be picked up in the eval
111 my $attrs = $self->attributes;
113 $code = eval $source;
114 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
119 sub _generate_slot_initializer {
123 my $attr = $self->attributes->[$index];
126 if ($attr->has_default) {
128 # default values can either be CODE refs
129 # in which case we need to call them. Or
130 # they can be scalars (strings/numbers)
131 # in which case we can just deal with them
132 # in the code we eval.
133 if ($attr->is_default_a_coderef) {
134 $default = '$attrs->[' . $index . ']->default($instance)';
137 $default = $attr->default;
138 # make sure to quote strings ...
139 unless (looks_like_number($default)) {
140 $default = "'$default'";
143 } elsif( $attr->has_builder ) {
144 $default = '$instance->'.$attr->builder;
147 if ( defined $attr->init_arg ) {
149 'if(exists $params->{\'' . $attr->init_arg . '\'}){' . "\n" .
150 $self->meta_instance->inline_set_slot_value(
152 ("'" . $attr->name . "'"),
153 '$params->{\'' . $attr->init_arg . '\'}' ) . "\n" .
154 '} ' . (!defined $default ? '' : 'else {' . "\n" .
155 $self->meta_instance->inline_set_slot_value(
157 ("'" . $attr->name . "'"),
161 } elsif ( defined $default ) {
163 $self->meta_instance->inline_set_slot_value(
165 ("'" . $attr->name . "'"),
179 Class::MOP::Method::Constructor - Method Meta Object for constructors
183 use Class::MOP::Method::Constructor;
185 my $constructor = Class::MOP::Method::Constructor->new(
186 metaclass => $metaclass,
188 debug => 1, # this is all for now
192 # calling the constructor ...
193 $constructor->body->($metaclass->name, %params);
197 This is a subclass of C<Class::MOP::Method> which deals with
198 class constructors. This is used when making a class immutable
199 to generate an optimized constructor.
205 =item B<new (metaclass => $meta, options => \%options)>
209 This returns the options HASH which is passed into C<new>.
211 =item B<associated_metaclass>
213 This returns the metaclass which is passed into C<new>.
217 This returns the list of attributes which are associated with the
218 metaclass which is passed into C<new>.
220 =item B<meta_instance>
222 This returns the meta instance which is associated with the
223 metaclass which is passed into C<new>.
227 This returns a boolean, but since constructors are very rarely
228 not inlined, this always returns true for now.
230 =item B<initialize_body>
232 This creates the code reference for the constructor itself.
236 =head2 Method Generators
240 =item B<generate_constructor_method>
242 =item B<generate_constructor_method_inline>
248 Stevan Little E<lt>stevan@iinteractive.comE<gt>
250 =head1 COPYRIGHT AND LICENSE
252 Copyright 2006-2008 by Infinity Interactive, Inc.
254 L<http://www.iinteractive.com>
256 This library is free software; you can redistribute it and/or modify
257 it under the same terms as Perl itself.