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->{'$!meta_instance'});
37 weaken($self->{'$!associated_metaclass'});
39 $self->intialize_body;
47 # if it is blessed into this class,
48 # then it is always inlined, that is
49 # pretty much what this class is for.
54 sub options { (shift)->{'%!options'} }
55 sub meta_instance { (shift)->{'$!meta_instance'} }
56 sub attributes { (shift)->{'@!attributes'} }
58 sub associated_metaclass { (shift)->{'$!associated_metaclass'} }
65 # the %options should also include a both
66 # a call 'initializer' and call 'SUPER::'
67 # options, which should cover approx 90%
68 # of the possible use cases (even if it
69 # requires some adaption on the part of
70 # the author, after all, nothing is free)
72 $source .= "\n" . 'my ($class, %params) = @_;';
73 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
74 $source .= ";\n" . (join ";\n" => map {
75 $self->_generate_slot_initializer($_)
76 } 0 .. (@{$self->attributes} - 1));
77 $source .= ";\n" . 'return $instance';
78 $source .= ";\n" . '}';
79 warn $source if $self->options->{debug};
84 # create the nessecary lexicals
85 # to be picked up in the eval
86 my $attrs = $self->attributes;
89 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
91 $self->{'&!body'} = $code;
94 sub _generate_slot_initializer {
98 my $attr = $self->attributes->[$index];
101 if ($attr->has_default) {
103 # default values can either be CODE refs
104 # in which case we need to call them. Or
105 # they can be scalars (strings/numbers)
106 # in which case we can just deal with them
107 # in the code we eval.
108 if ($attr->is_default_a_coderef) {
109 $default = '$attrs->[' . $index . ']->default($instance)';
112 $default = $attr->default;
113 # make sure to quote strings ...
114 unless (looks_like_number($default)) {
115 $default = "'$default'";
119 $self->meta_instance->inline_set_slot_value(
121 ("'" . $attr->name . "'"),
122 ('$params{\'' . $attr->init_arg . '\'}' . (defined $default ? (' || ' . $default) : ''))
136 Class::MOP::Method::Constructor - Method Meta Object for constructors
152 =item B<meta_instance>
154 =item B<associated_metaclass>
158 =item B<intialize_body>
164 Stevan Little E<lt>stevan@iinteractive.comE<gt>
166 =head1 COPYRIGHT AND LICENSE
168 Copyright 2006 by Infinity Interactive, Inc.
170 L<http://www.iinteractive.com>
172 This library is free software; you can redistribute it and/or modify
173 it under the same terms as Perl itself.