2 package Class::MOP::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
10 our $VERSION = '0.01';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Class::MOP::Method';
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 '$!meta_instance' => $options{metaclass}->get_meta_instance,
28 '@!attributes' => [ $options{metaclass}->compute_all_applicable_attributes ],
30 '$!associated_metaclass' => $options{metaclass},
33 # we don't want this creating
34 # a cycle in the code, if not
36 weaken($self->{'$!associated_metaclass'});
38 $self->intialize_body;
46 # if it is blessed into this class,
47 # then it is always inlined, that is
48 # pretty much what this class is for.
53 sub options { (shift)->{'%!options'} }
54 sub meta_instance { (shift)->{'$!meta_instance'} }
55 sub attributes { (shift)->{'@!attributes'} }
57 sub associated_metaclass { (shift)->{'$!associated_metaclass'} }
64 # the %options should also include a both
65 # a call 'initializer' and call 'SUPER::'
66 # options, which should cover approx 90%
67 # of the possible use cases (even if it
68 # requires some adaption on the part of
69 # the author, after all, nothing is free)
71 $source .= "\n" . 'my ($class, %params) = @_;';
72 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
73 $source .= ";\n" . (join ";\n" => map {
74 $self->_generate_slot_initializer($_)
75 } 0 .. (@{$self->attributes} - 1));
76 $source .= ";\n" . 'return $instance';
77 $source .= ";\n" . '}';
78 warn $source if $self->options->{debug};
83 # create the nessecary lexicals
84 # to be picked up in the eval
85 my $attrs = $self->attributes;
88 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
90 $self->{'&!body'} = $code;
93 sub _generate_slot_initializer {
97 my $attr = $self->attributes->[$index];
100 if ($attr->has_default) {
102 # default values can either be CODE refs
103 # in which case we need to call them. Or
104 # they can be scalars (strings/numbers)
105 # in which case we can just deal with them
106 # in the code we eval.
107 if ($attr->is_default_a_coderef) {
108 $default = '$attrs->[' . $index . ']->default($instance)';
111 $default = $attr->default;
112 # make sure to quote strings ...
113 unless (looks_like_number($default)) {
114 $default = "'$default'";
118 $self->meta_instance->inline_set_slot_value(
120 ("'" . $attr->name . "'"),
121 ('$params{\'' . $attr->init_arg . '\'}' . (defined $default ? (' || ' . $default) : ''))
135 Class::MOP::Method::Constructor - Method Meta Object for constructors
139 use Class::MOP::Method::Constructor;
141 my $constructor = Class::MOP::Method::Constructor->new(
142 metaclass => $metaclass,
144 debug => 1, # this is all for now
148 # calling the constructor ...
149 $constructor->body->($metaclass->name, %params);
153 This is a subclass of C<Class::MOP::Method> which deals with
160 =item B<new (metaclass => $meta, options => \%options)>
164 This returns the options HASH which is passed into C<new>.
166 =item B<associated_metaclass>
168 This returns the metaclass which is passed into C<new>.
172 This returns the list of attributes which are associated with the
173 metaclass which is passed into C<new>.
175 =item B<meta_instance>
177 This returns the meta instance which is associated with the
178 metaclass which is passed into C<new>.
182 This returns a boolean, but since constructors are very rarely
183 not inlined, this always returns true for now.
185 =item B<intialize_body>
187 This creates the code reference for the constructor itself.
193 Stevan Little E<lt>stevan@iinteractive.comE<gt>
195 =head1 COPYRIGHT AND LICENSE
197 Copyright 2006, 2007 by Infinity Interactive, Inc.
199 L<http://www.iinteractive.com>
201 This library is free software; you can redistribute it and/or modify
202 it under the same terms as Perl itself.