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";
22 (blessed $options{meta_instance} && $options{meta_instance}->isa('Class::MOP::Instance'))
23 || confess "You must supply a meta-instance";
25 (exists $options{attributes} && ref $options{attributes} eq 'ARRAY')
26 || confess "You must pass an array of options";
28 (blessed($_) && $_->isa('Class::MOP::Attribute'))
29 || confess "You must supply a list of attributes which is a 'Class::MOP::Attribute' instance"
30 for @{$options{attributes}};
35 # specific to this subclass
36 options => $options{options},
37 meta_instance => $options{meta_instance},
38 attributes => $options{attributes},
41 # we don't want this creating
42 # a cycle in the code, if not
44 weaken($self->{meta_instance});
46 $self->intialize_body;
53 sub options { (shift)->{options} }
54 sub meta_instance { (shift)->{meta_instance} }
55 sub attributes { (shift)->{attributes} }
62 # the %options should also include a both
63 # a call 'initializer' and call 'SUPER::'
64 # options, which should cover approx 90%
65 # of the possible use cases (even if it
66 # requires some adaption on the part of
67 # the author, after all, nothing is free)
69 $source .= "\n" . 'my ($class, %params) = @_;';
70 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
71 $source .= ";\n" . (join ";\n" => map {
72 $self->_generate_slot_initializer($_)
73 } 0 .. (@{$self->attributes} - 1));
74 $source .= ";\n" . 'return $instance';
75 $source .= ";\n" . '}';
76 warn $source if $self->options->{debug};
81 # create the nessecary lexicals
82 # to be picked up in the eval
83 my $attrs = $self->attributes;
86 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
88 $self->{body} = $code;
91 sub _generate_slot_initializer {
95 my $attr = $self->attributes->[$index];
98 if ($attr->has_default) {
100 # default values can either be CODE refs
101 # in which case we need to call them. Or
102 # they can be scalars (strings/numbers)
103 # in which case we can just deal with them
104 # in the code we eval.
105 if ($attr->is_default_a_coderef) {
106 $default = '$attrs->[' . $index . ']->default($instance)';
109 $default = $attr->default;
110 # make sure to quote strings ...
111 unless (looks_like_number($default)) {
112 $default = "'$default'";
116 $self->meta_instance->inline_set_slot_value(
118 ("'" . $attr->name . "'"),
119 ('$params{\'' . $attr->init_arg . '\'}' . (defined $default ? (' || ' . $default) : ''))
133 Class::MOP::Method::Constructor - Method Meta Object for constructors
147 =item B<meta_instance>
151 =item B<intialize_body>
157 Stevan Little E<lt>stevan@iinteractive.comE<gt>
159 =head1 COPYRIGHT AND LICENSE
161 Copyright 2006 by Infinity Interactive, Inc.
163 L<http://www.iinteractive.com>
165 This library is free software; you can redistribute it and/or modify
166 it under the same terms as Perl itself.