2 package Class::MOP::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
10 our $VERSION = '0.89';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Class::MOP::Method::Inlined';
20 (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
21 || confess "You must pass a metaclass instance if you want to inline"
22 if $options{is_inline};
24 ($options{package_name} && $options{name})
25 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
27 my $self = $class->_new(\%options);
29 # we don't want this creating
30 # a cycle in the code, if not
32 weaken($self->{'associated_metaclass'});
34 $self->_initialize_body;
42 return Class::MOP::Class->initialize($class)->new_object(@_)
43 if $class ne __PACKAGE__;
45 my $params = @_ == 1 ? $_[0] : {@_};
48 # inherited from Class::MOP::Method
49 body => $params->{body},
50 # associated_metaclass => $params->{associated_metaclass}, # overriden
51 package_name => $params->{package_name},
52 name => $params->{name},
53 original_method => $params->{original_method},
55 # inherited from Class::MOP::Generated
56 is_inline => $params->{is_inline} || 0,
57 definition_context => $params->{definition_context},
59 # inherited from Class::MOP::Inlined
60 _expected_method_class => $params->{_expected_method_class},
62 # defined in this subclass
63 options => $params->{options} || {},
64 associated_metaclass => $params->{metaclass},
70 sub options { (shift)->{'options'} }
71 sub associated_metaclass { (shift)->{'associated_metaclass'} }
76 Carp::cluck('The meta_instance method has been made private.'
77 . " The public version is deprecated and will be removed in a future release.\n");
78 shift->_meta_instance;
83 $self->{'meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
87 Carp::cluck('The attributes method has been made private.'
88 . " The public version is deprecated and will be removed in a future release.\n");
90 return shift->_attributes;
95 $self->{'attributes'} ||= [ $self->associated_metaclass->get_all_attributes ]
100 sub initialize_body {
101 Carp::cluck('The initialize_body method has been made private.'
102 . " The public version is deprecated and will be removed in a future release.\n");
103 shift->_initialize_body;
106 sub _initialize_body {
108 my $method_name = '_generate_constructor_method';
110 $method_name .= '_inline' if $self->is_inline;
112 $self->{'body'} = $self->$method_name;
115 sub generate_constructor_method {
116 Carp::cluck('The generate_constructor_method method has been made private.'
117 . " The public version is deprecated and will be removed in a future release.\n");
118 shift->_generate_constructor_method;
121 sub _generate_constructor_method {
122 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
125 sub generate_constructor_method_inline {
126 Carp::cluck('The generate_constructor_method_inline method has been made private.'
127 . " The public version is deprecated and will be removed in a future release.\n");
128 shift->_generate_constructor_method_inline;
131 sub _generate_constructor_method_inline {
136 my $source = 'sub {';
137 $source .= "\n" . 'my $class = shift;';
139 $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(@_)';
140 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
142 $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};';
144 $source .= "\n" . 'my $instance = ' . $self->_meta_instance->inline_create_instance('$class');
145 $source .= ";\n" . (join ";\n" => map {
146 $self->_generate_slot_initializer($_, $close_over)
147 } @{ $self->_attributes });
148 $source .= ";\n" . 'return $instance';
149 $source .= ";\n" . '}';
150 warn $source if $self->options->{debug};
152 my ( $code, $e ) = $self->_eval_closure(
156 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e" if $e;
161 sub _generate_slot_initializer {
167 if ($attr->has_default) {
169 # default values can either be CODE refs
170 # in which case we need to call them. Or
171 # they can be scalars (strings/numbers)
172 # in which case we can just deal with them
173 # in the code we eval.
174 if ($attr->is_default_a_coderef) {
175 my $idx = @{$close->{'@defaults'}||=[]};
176 push(@{$close->{'@defaults'}}, $attr->default);
177 $default = '$defaults[' . $idx . ']->($instance)';
180 $default = $attr->default;
181 # make sure to quote strings ...
182 unless (looks_like_number($default)) {
183 $default = "'$default'";
186 } elsif( $attr->has_builder ) {
187 $default = '$instance->'.$attr->builder;
190 if ( defined(my $init_arg = $attr->init_arg) ) {
192 'if(exists $params->{\'' . $init_arg . '\'}){' . "\n" .
193 $self->_meta_instance->inline_set_slot_value(
196 '$params->{\'' . $init_arg . '\'}' ) . "\n" .
197 '} ' . (!defined $default ? '' : 'else {' . "\n" .
198 $self->_meta_instance->inline_set_slot_value(
204 } elsif ( defined $default ) {
206 $self->_meta_instance->inline_set_slot_value(
222 Class::MOP::Method::Constructor - Method Meta Object for constructors
226 use Class::MOP::Method::Constructor;
228 my $constructor = Class::MOP::Method::Constructor->new(
229 metaclass => $metaclass,
231 debug => 1, # this is all for now
235 # calling the constructor ...
236 $constructor->body->execute($metaclass->name, %params);
240 This is a subclass of C<Class::MOP::Method> which generates
247 =item B<< Class::MOP::Method::Constructor->new(%options) >>
249 This creates a new constructor object. It accepts a hash reference of
256 This should be a L<Class::MOP::Class> object. It is required.
260 The method name (without a package name). This is required.
264 The package name for the method. This is required.
268 This indicates whether or not the constructor should be inlined. This
273 =item B<< $metamethod->is_inline >>
275 Returns a boolean indicating whether or not the constructor is
278 =item B<< $metamethod->associated_metaclass >>
280 This returns the L<Class::MOP::Class> object for the method.
286 Stevan Little E<lt>stevan@iinteractive.comE<gt>
288 =head1 COPYRIGHT AND LICENSE
290 Copyright 2006-2009 by Infinity Interactive, Inc.
292 L<http://www.iinteractive.com>
294 This library is free software; you can redistribute it and/or modify
295 it under the same terms as Perl itself.