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;
41 return Class::MOP::Class->initialize($class)->new_object(@_)
42 if $class ne __PACKAGE__;
44 my $params = @_ == 1 ? $_[0] : {@_};
47 # inherited from Class::MOP::Method
48 body => $params->{body},
49 # associated_metaclass => $params->{associated_metaclass}, # overriden
50 package_name => $params->{package_name},
51 name => $params->{name},
52 original_method => $params->{original_method},
54 # inherited from Class::MOP::Generated
55 is_inline => $params->{is_inline} || 0,
56 definition_context => $params->{definition_context},
58 # inherited from Class::MOP::Inlined
59 _expected_method_class => $params->{_expected_method_class},
61 # defined in this subclass
62 options => $params->{options} || {},
63 associated_metaclass => $params->{metaclass},
69 sub options { (shift)->{'options'} }
70 sub associated_metaclass { (shift)->{'associated_metaclass'} }
75 Carp::cluck('The meta_instance method has been made private.'
76 . " The public version is deprecated and will be removed in a future release.\n");
77 shift->_meta_instance;
82 $self->{'meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
86 Carp::cluck('The attributes method has been made private.'
87 . " The public version is deprecated and will be removed in a future release.\n");
89 return shift->_attributes;
94 $self->{'attributes'} ||= [ $self->associated_metaclass->get_all_attributes ]
100 Carp::cluck('The initialize_body method has been made private.'
101 . " The public version is deprecated and will be removed in a future release.\n");
102 shift->_initialize_body;
105 sub _initialize_body {
107 my $method_name = '_generate_constructor_method';
109 $method_name .= '_inline' if $self->is_inline;
111 $self->{'body'} = $self->$method_name;
114 sub generate_constructor_method {
115 Carp::cluck('The generate_constructor_method method has been made private.'
116 . " The public version is deprecated and will be removed in a future release.\n");
117 shift->_generate_constructor_method;
120 sub _generate_constructor_method {
121 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
124 sub generate_constructor_method_inline {
125 Carp::cluck('The generate_constructor_method_inline method has been made private.'
126 . " The public version is deprecated and will be removed in a future release.\n");
127 shift->_generate_constructor_method_inline;
130 sub _generate_constructor_method_inline {
135 my $source = 'sub {';
136 $source .= "\n" . 'my $class = shift;';
138 $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(@_)';
139 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
141 $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};';
143 $source .= "\n" . 'my $instance = ' . $self->_meta_instance->inline_create_instance('$class');
144 $source .= ";\n" . (join ";\n" => map {
145 $self->_generate_slot_initializer($_, $close_over)
146 } @{ $self->_attributes });
147 $source .= ";\n" . 'return $instance';
148 $source .= ";\n" . '}';
149 warn $source if $self->options->{debug};
151 my ( $code, $e ) = $self->_eval_closure(
155 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e" if $e;
160 sub _generate_slot_initializer {
166 if ($attr->has_default) {
168 # default values can either be CODE refs
169 # in which case we need to call them. Or
170 # they can be scalars (strings/numbers)
171 # in which case we can just deal with them
172 # in the code we eval.
173 if ($attr->is_default_a_coderef) {
174 my $idx = @{$close->{'@defaults'}||=[]};
175 push(@{$close->{'@defaults'}}, $attr->default);
176 $default = '$defaults[' . $idx . ']->($instance)';
179 $default = $attr->default;
180 # make sure to quote strings ...
181 unless (looks_like_number($default)) {
182 $default = "'$default'";
185 } elsif( $attr->has_builder ) {
186 $default = '$instance->'.$attr->builder;
189 if ( defined(my $init_arg = $attr->init_arg) ) {
191 'if(exists $params->{\'' . $init_arg . '\'}){' . "\n" .
192 $self->_meta_instance->inline_set_slot_value(
195 '$params->{\'' . $init_arg . '\'}' ) . "\n" .
196 '} ' . (!defined $default ? '' : 'else {' . "\n" .
197 $self->_meta_instance->inline_set_slot_value(
203 } elsif ( defined $default ) {
205 $self->_meta_instance->inline_set_slot_value(
221 Class::MOP::Method::Constructor - Method Meta Object for constructors
225 use Class::MOP::Method::Constructor;
227 my $constructor = Class::MOP::Method::Constructor->new(
228 metaclass => $metaclass,
230 debug => 1, # this is all for now
234 # calling the constructor ...
235 $constructor->body->execute($metaclass->name, %params);
239 This is a subclass of C<Class::MOP::Method> which generates
246 =item B<< Class::MOP::Method::Constructor->new(%options) >>
248 This creates a new constructor object. It accepts a hash reference of
255 This should be a L<Class::MOP::Class> object. It is required.
259 The method name (without a package name). This is required.
263 The package name for the method. This is required.
267 This indicates whether or not the constructor should be inlined. This
272 =item B<< $metamethod->is_inline >>
274 Returns a boolean indicating whether or not the constructor is
277 =item B<< $metamethod->associated_metaclass >>
279 This returns the L<Class::MOP::Class> object for the method.
285 Stevan Little E<lt>stevan@iinteractive.comE<gt>
287 =head1 COPYRIGHT AND LICENSE
289 Copyright 2006-2009 by Infinity Interactive, Inc.
291 L<http://www.iinteractive.com>
293 This library is free software; you can redistribute it and/or modify
294 it under the same terms as Perl itself.