2 package Class::MOP::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
10 our $VERSION = '0.72';
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),
55 sub can_be_inlined { 1 }
59 sub options { (shift)->{'options'} }
60 sub associated_metaclass { (shift)->{'associated_metaclass'} }
66 $self->{'meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
71 $self->{'attributes'} ||= [ $self->associated_metaclass->compute_all_applicable_attributes ]
78 my $method_name = 'generate_constructor_method';
80 $method_name .= '_inline' if $self->is_inline;
82 $self->{'body'} = $self->$method_name;
85 sub generate_constructor_method {
86 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
89 sub generate_constructor_method_inline {
95 $source .= "\n" . 'my $class = shift;';
97 $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(@_)';
98 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
100 $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};';
102 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
103 $source .= ";\n" . (join ";\n" => map {
104 $self->_generate_slot_initializer($_)
105 } 0 .. (@{$self->attributes} - 1));
106 $source .= ";\n" . 'return $instance';
107 $source .= ";\n" . '}';
108 warn $source if $self->options->{debug};
113 # create the nessecary lexicals
114 # to be picked up in the eval
116 $code = $self->_eval_closure(
117 { '$attrs' => \$self->attributes },
120 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
125 sub _generate_slot_initializer {
129 my $attr = $self->attributes->[$index];
132 if ($attr->has_default) {
134 # default values can either be CODE refs
135 # in which case we need to call them. Or
136 # they can be scalars (strings/numbers)
137 # in which case we can just deal with them
138 # in the code we eval.
139 if ($attr->is_default_a_coderef) {
140 $default = '$attrs->[' . $index . ']->default($instance)';
143 $default = $attr->default;
144 # make sure to quote strings ...
145 unless (looks_like_number($default)) {
146 $default = "'$default'";
149 } elsif( $attr->has_builder ) {
150 $default = '$instance->'.$attr->builder;
153 if ( defined $attr->init_arg ) {
155 'if(exists $params->{\'' . $attr->init_arg . '\'}){' . "\n" .
156 $self->meta_instance->inline_set_slot_value(
158 ("'" . $attr->name . "'"),
159 '$params->{\'' . $attr->init_arg . '\'}' ) . "\n" .
160 '} ' . (!defined $default ? '' : 'else {' . "\n" .
161 $self->meta_instance->inline_set_slot_value(
163 ("'" . $attr->name . "'"),
167 } elsif ( defined $default ) {
169 $self->meta_instance->inline_set_slot_value(
171 ("'" . $attr->name . "'"),
185 Class::MOP::Method::Constructor - Method Meta Object for constructors
189 use Class::MOP::Method::Constructor;
191 my $constructor = Class::MOP::Method::Constructor->new(
192 metaclass => $metaclass,
194 debug => 1, # this is all for now
198 # calling the constructor ...
199 $constructor->body->execute($metaclass->name, %params);
203 This is a subclass of C<Class::MOP::Method> which deals with
204 class constructors. This is used when making a class immutable
205 to generate an optimized constructor.
211 =item B<new (metaclass => $meta, options => \%options)>
215 This returns the options HASH which is passed into C<new>.
217 =item B<associated_metaclass>
219 This returns the metaclass which is passed into C<new>.
223 This returns the list of attributes which are associated with the
224 metaclass which is passed into C<new>.
226 =item B<meta_instance>
228 This returns the meta instance which is associated with the
229 metaclass which is passed into C<new>.
233 This returns a boolean, but since constructors are very rarely
234 not inlined, this always returns true for now.
236 =item B<can_be_inlined>
238 This method always returns true in this class. It exists so that
239 subclasses (like in Moose) can override and do some sort of checking
240 to determine whether or not inlining the constructor is safe.
242 =item B<initialize_body>
244 This creates the code reference for the constructor itself.
248 =head2 Method Generators
252 =item B<generate_constructor_method>
254 =item B<generate_constructor_method_inline>
260 Stevan Little E<lt>stevan@iinteractive.comE<gt>
262 =head1 COPYRIGHT AND LICENSE
264 Copyright 2006-2008 by Infinity Interactive, Inc.
266 L<http://www.iinteractive.com>
268 This library is free software; you can redistribute it and/or modify
269 it under the same terms as Perl itself.