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 (exists $options{options} && ref $options{options} eq 'HASH')
20 || confess "You must pass a hash of options";
25 # specific to this subclass
26 '%!options' => $options{options},
27 '$!associated_metaclass' => $options{metaclass},
28 '$!is_inline' => ($options{is_inline} || 0),
31 # we don't want this creating
32 # a cycle in the code, if not
34 weaken($self->{'$!associated_metaclass'});
36 $self->initialize_body;
43 sub options { (shift)->{'%!options'} }
44 sub associated_metaclass { (shift)->{'$!associated_metaclass'} }
50 $self->{'$!meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
55 $self->{'@!attributes'} ||= [ $self->associated_metaclass->compute_all_applicable_attributes ]
62 my $method_name = 'generate_constructor_method';
64 $method_name .= '_inline' if $self->is_inline;
66 $self->{'&!body'} = $self->$method_name;
69 sub generate_constructor_method {
70 return sub { (shift)->meta->new_object(@_) }
73 sub generate_constructor_method_inline {
77 $source .= "\n" . 'my ($class, %params) = @_;';
79 $source .= "\n" . 'return $class->meta->new_object(%params)';
80 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
82 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
83 $source .= ";\n" . (join ";\n" => map {
84 $self->_generate_slot_initializer($_)
85 } 0 .. (@{$self->attributes} - 1));
86 $source .= ";\n" . 'return $instance';
87 $source .= ";\n" . '}';
88 warn $source if $self->options->{debug};
93 # create the nessecary lexicals
94 # to be picked up in the eval
95 my $attrs = $self->attributes;
98 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
103 sub _generate_slot_initializer {
107 my $attr = $self->attributes->[$index];
110 if ($attr->has_default) {
112 # default values can either be CODE refs
113 # in which case we need to call them. Or
114 # they can be scalars (strings/numbers)
115 # in which case we can just deal with them
116 # in the code we eval.
117 if ($attr->is_default_a_coderef) {
118 $default = '$attrs->[' . $index . ']->default($instance)';
121 $default = $attr->default;
122 # make sure to quote strings ...
123 unless (looks_like_number($default)) {
124 $default = "'$default'";
128 $self->meta_instance->inline_set_slot_value(
130 ("'" . $attr->name . "'"),
131 ('$params{\'' . $attr->init_arg . '\'}' . (defined $default ? (' || ' . $default) : ''))
145 Class::MOP::Method::Constructor - Method Meta Object for constructors
149 use Class::MOP::Method::Constructor;
151 my $constructor = Class::MOP::Method::Constructor->new(
152 metaclass => $metaclass,
154 debug => 1, # this is all for now
158 # calling the constructor ...
159 $constructor->body->($metaclass->name, %params);
163 This is a subclass of C<Class::MOP::Method> which deals with
170 =item B<new (metaclass => $meta, options => \%options)>
174 This returns the options HASH which is passed into C<new>.
176 =item B<associated_metaclass>
178 This returns the metaclass which is passed into C<new>.
182 This returns the list of attributes which are associated with the
183 metaclass which is passed into C<new>.
185 =item B<meta_instance>
187 This returns the meta instance which is associated with the
188 metaclass which is passed into C<new>.
192 This returns a boolean, but since constructors are very rarely
193 not inlined, this always returns true for now.
195 =item B<initialize_body>
197 This creates the code reference for the constructor itself.
201 =head2 Method Generators
205 =item B<generate_constructor_method>
207 =item B<generate_constructor_method_inline>
213 Stevan Little E<lt>stevan@iinteractive.comE<gt>
215 =head1 COPYRIGHT AND LICENSE
217 Copyright 2006, 2007 by Infinity Interactive, Inc.
219 L<http://www.iinteractive.com>
221 This library is free software; you can redistribute it and/or modify
222 it under the same terms as Perl itself.