2 package Class::MOP::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken';
11 our $VERSION = '1.11';
12 $VERSION = eval $VERSION;
13 our $AUTHORITY = 'cpan:STEVAN';
15 use base 'Class::MOP::Method::Inlined';
21 (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
22 || confess "You must pass a metaclass instance if you want to inline"
23 if $options{is_inline};
25 ($options{package_name} && $options{name})
26 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
28 my $self = $class->_new(\%options);
30 # we don't want this creating
31 # a cycle in the code, if not
33 weaken($self->{'associated_metaclass'});
35 $self->_initialize_body;
43 return Class::MOP::Class->initialize($class)->new_object(@_)
44 if $class ne __PACKAGE__;
46 my $params = @_ == 1 ? $_[0] : {@_};
49 # inherited from Class::MOP::Method
50 body => $params->{body},
51 # associated_metaclass => $params->{associated_metaclass}, # overriden
52 package_name => $params->{package_name},
53 name => $params->{name},
54 original_method => $params->{original_method},
56 # inherited from Class::MOP::Generated
57 is_inline => $params->{is_inline} || 0,
58 definition_context => $params->{definition_context},
60 # inherited from Class::MOP::Inlined
61 _expected_method_class => $params->{_expected_method_class},
63 # defined in this subclass
64 options => $params->{options} || {},
65 associated_metaclass => $params->{metaclass},
71 sub options { (shift)->{'options'} }
72 sub associated_metaclass { (shift)->{'associated_metaclass'} }
78 $self->{'attributes'} ||= [ $self->associated_metaclass->get_all_attributes ]
83 sub _initialize_body {
85 my $method_name = '_generate_constructor_method';
87 $method_name .= '_inline' if $self->is_inline;
89 $self->{'body'} = $self->$method_name;
92 sub _eval_environment {
94 my $defaults = [map { $_->default } @{ $self->_attributes }];
96 '$defaults' => \$defaults,
100 sub _generate_constructor_method {
101 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
104 sub _generate_constructor_method_inline {
107 my $meta = $self->associated_metaclass;
111 $meta->_inline_new_object,
115 warn join("\n", @source) if $self->options->{debug};
118 $self->_compile_code(\@source);
121 my $source = join("\n", @source);
122 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$_";
136 Class::MOP::Method::Constructor - Method Meta Object for constructors
140 use Class::MOP::Method::Constructor;
142 my $constructor = Class::MOP::Method::Constructor->new(
143 metaclass => $metaclass,
145 debug => 1, # this is all for now
149 # calling the constructor ...
150 $constructor->body->execute($metaclass->name, %params);
154 This is a subclass of C<Class::MOP::Method> which generates
161 =item B<< Class::MOP::Method::Constructor->new(%options) >>
163 This creates a new constructor object. It accepts a hash reference of
170 This should be a L<Class::MOP::Class> object. It is required.
174 The method name (without a package name). This is required.
178 The package name for the method. This is required.
182 This indicates whether or not the constructor should be inlined. This
187 =item B<< $metamethod->is_inline >>
189 Returns a boolean indicating whether or not the constructor is
192 =item B<< $metamethod->associated_metaclass >>
194 This returns the L<Class::MOP::Class> object for the method.
200 Stevan Little E<lt>stevan@iinteractive.comE<gt>
202 =head1 COPYRIGHT AND LICENSE
204 Copyright 2006-2010 by Infinity Interactive, Inc.
206 L<http://www.iinteractive.com>
208 This library is free software; you can redistribute it and/or modify
209 it under the same terms as Perl itself.