2 package Moose::Meta::Class;
8 use Scalar::Util 'weaken';
10 our $VERSION = '0.02';
12 use base 'Class::MOP::Class';
14 sub construct_instance {
15 my ($class, %params) = @_;
16 my $instance = $params{'__INSTANCE__'} || {};
17 foreach my $attr ($class->compute_all_applicable_attributes()) {
18 my $init_arg = $attr->init_arg();
19 # try to fetch the init arg from the %params ...
21 $val = $params{$init_arg} if exists $params{$init_arg};
22 # if nothing was in the %params, we can use the
23 # attribute's default value (if it has one)
24 $val ||= $attr->default($instance) if $attr->has_default;
26 if ($attr->has_type_constraint) {
27 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
28 $val = $attr->type_constraint->coercion->coerce($val);
30 (defined($attr->type_constraint->check($val)))
31 || confess "Attribute (" . $attr->name . ") does not pass the type contraint with '$val'";
34 $instance->{$attr->name} = $val;
35 if (defined $val && $attr->is_weak_ref) {
36 weaken($instance->{$attr->name});
50 Moose::Meta::Class - The Moose metaclass
54 This is a subclass of L<Class::MOP::Class> with Moose specific
57 For the most part, the only time you will ever encounter an
58 instance of this class is if you are doing some serious deep
59 introspection. To really understand this class, you need to refer
60 to the L<Class::MOP::Class> documentation.
66 =item B<construct_instance>
68 This provides some Moose specific extensions to this method, you
69 almost never call this method directly unless you really know what
72 This method makes sure to handle the moose weak-ref, type-constraint
73 and type coercion features.
79 All complex software has bugs lurking in it, and this module is no
80 exception. If you find a bug please either email me, or add the bug
85 Stevan Little E<lt>stevan@iinteractive.comE<gt>
87 =head1 COPYRIGHT AND LICENSE
89 Copyright 2006 by Infinity Interactive, Inc.
91 L<http://www.iinteractive.com>
93 This library is free software; you can redistribute it and/or modify
94 it under the same terms as Perl itself.