2 package Class::MOP::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
10 our $VERSION = '0.02';
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 # specific to this subclass
27 '%!options' => $options{options} || {},
28 '$!associated_metaclass' => $options{metaclass},
29 '$!is_inline' => ($options{is_inline} || 0),
32 # we don't want this creating
33 # a cycle in the code, if not
35 weaken($self->{'$!associated_metaclass'});
37 $self->initialize_body;
44 sub options { (shift)->{'%!options'} }
45 sub associated_metaclass { (shift)->{'$!associated_metaclass'} }
51 $self->{'$!meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
56 $self->{'@!attributes'} ||= [ $self->associated_metaclass->compute_all_applicable_attributes ]
63 my $method_name = 'generate_constructor_method';
65 $method_name .= '_inline' if $self->is_inline;
67 $self->{'&!body'} = $self->$method_name;
70 sub generate_constructor_method {
71 return sub { (shift)->meta->new_object(@_) }
74 sub generate_constructor_method_inline {
78 $source .= "\n" . 'my ($class, %params) = @_;';
80 $source .= "\n" . 'return $class->meta->new_object(%params)';
81 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
83 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
84 $source .= ";\n" . (join ";\n" => map {
85 $self->_generate_slot_initializer($_)
86 } 0 .. (@{$self->attributes} - 1));
87 $source .= ";\n" . 'return $instance';
88 $source .= ";\n" . '}';
89 warn $source if $self->options->{debug};
94 # create the nessecary lexicals
95 # to be picked up in the eval
96 my $attrs = $self->attributes;
99 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
104 sub _generate_slot_initializer {
108 my $attr = $self->attributes->[$index];
111 if ($attr->has_default) {
113 # default values can either be CODE refs
114 # in which case we need to call them. Or
115 # they can be scalars (strings/numbers)
116 # in which case we can just deal with them
117 # in the code we eval.
118 if ($attr->is_default_a_coderef) {
119 $default = '$attrs->[' . $index . ']->default($instance)';
122 $default = $attr->default;
123 # make sure to quote strings ...
124 unless (looks_like_number($default)) {
125 $default = "'$default'";
129 $self->meta_instance->inline_set_slot_value(
131 ("'" . $attr->name . "'"),
132 ('$params{\'' . $attr->init_arg . '\'}' . (defined $default ? (' || ' . $default) : ''))
146 Class::MOP::Method::Constructor - Method Meta Object for constructors
150 use Class::MOP::Method::Constructor;
152 my $constructor = Class::MOP::Method::Constructor->new(
153 metaclass => $metaclass,
155 debug => 1, # this is all for now
159 # calling the constructor ...
160 $constructor->body->($metaclass->name, %params);
164 This is a subclass of C<Class::MOP::Method> which deals with
171 =item B<new (metaclass => $meta, options => \%options)>
175 This returns the options HASH which is passed into C<new>.
177 =item B<associated_metaclass>
179 This returns the metaclass which is passed into C<new>.
183 This returns the list of attributes which are associated with the
184 metaclass which is passed into C<new>.
186 =item B<meta_instance>
188 This returns the meta instance which is associated with the
189 metaclass which is passed into C<new>.
193 This returns a boolean, but since constructors are very rarely
194 not inlined, this always returns true for now.
196 =item B<initialize_body>
198 This creates the code reference for the constructor itself.
202 =head2 Method Generators
206 =item B<generate_constructor_method>
208 =item B<generate_constructor_method_inline>
214 Stevan Little E<lt>stevan@iinteractive.comE<gt>
216 =head1 COPYRIGHT AND LICENSE
218 Copyright 2006, 2007 by Infinity Interactive, Inc.
220 L<http://www.iinteractive.com>
222 This library is free software; you can redistribute it and/or modify
223 it under the same terms as Perl itself.