X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FMethod%2FConstructor.pm;h=a96187d4fdff5ccf65eccb4c1a5559baf1ecc11f;hb=d58887328353bfd216406ff7d686b5a4877d1731;hp=1e9079743a5b5b1734846097fa01a99cbf304b63;hpb=065f79e7cd03765f26c6ea276aaf9b3c5897886a;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/Method/Constructor.pm b/lib/Mouse/Meta/Method/Constructor.pm index 1e90797..a96187d 100644 --- a/lib/Mouse/Meta/Method/Constructor.pm +++ b/lib/Mouse/Meta/Method/Constructor.pm @@ -1,5 +1,5 @@ package Mouse::Meta::Method::Constructor; -use Mouse::Util qw(get_code_ref); # enables strict and warnings +use Mouse::Util qw(:meta); # enables strict and warnings sub _inline_slot{ my(undef, $self_var, $attr_name) = @_; @@ -51,6 +51,11 @@ sub _generate_processattrs { my @res; my $has_triggers; + my $strict = $metaclass->strict_constructor; + + if($strict){ + push @res, 'my $used = 0;'; + } for my $index (0 .. @$attrs - 1) { my $code = ''; @@ -60,6 +65,7 @@ sub _generate_processattrs { my $init_arg = $attr->init_arg; my $type_constraint = $attr->type_constraint; + my $is_weak_ref = $attr->is_weak_ref; my $need_coercion; my $instance_slot = $method_class->_inline_slot('$instance', $key); @@ -76,9 +82,9 @@ sub _generate_processattrs { my $post_process = ''; if(defined $type_constraint){ $post_process .= "\$checks[$index]->($instance_slot)"; - $post_process .= " or $attr_var->verify_type_constraint_error(q{$key}, $instance_slot, $constraint_var);\n"; + $post_process .= " or $attr_var->_throw_type_constraint_error($instance_slot, $constraint_var);\n"; } - if($attr->is_weak_ref){ + if($is_weak_ref){ $post_process .= "Scalar::Util::weaken($instance_slot) if ref $instance_slot;\n"; } @@ -99,7 +105,11 @@ sub _generate_processattrs { $code .= "push \@triggers, [$attr_var\->{trigger}, $instance_slot];\n"; } - $code .= "\n} else {\n"; + if ($strict){ + $code .= '++$used;' . "\n"; + } + + $code .= "\n} else {\n"; # $value exists } if ($attr->has_default || $attr->has_builder) { @@ -126,6 +136,9 @@ sub _generate_processattrs { } $code .= "$instance_slot = $value;\n"; + if($is_weak_ref){ + $code .= "Scalar::Util::weaken($instance_slot);\n"; + } } } elsif ($attr->is_required) { @@ -137,13 +150,18 @@ sub _generate_processattrs { push @res, $code; } + if($strict){ + push @res, q{if($used < keys %{$args})} + . q{{ $metaclass->_report_unknown_args(\@attrs, $args) }}; + } + if($metaclass->is_anon_class){ push @res, q{$instance->{__METACLASS__} = $metaclass;}; } if($has_triggers){ unshift @res, q{my @triggers;}; - push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;}; + push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;}; } return join "\n", @res; @@ -177,7 +195,7 @@ sub _generate_BUILDALL { my @code; for my $class ($metaclass->linearized_isa) { - if (get_code_ref($class, 'BUILD')) { + if (Mouse::Util::get_code_ref($class, 'BUILD')) { unshift @code, qq{${class}::BUILD(\$instance, \$args);}; } } @@ -193,7 +211,7 @@ Mouse::Meta::Method::Constructor - A Mouse method generator for constructors =head1 VERSION -This document describes Mouse version 0.40_02 +This document describes Mouse version 0.68 =head1 SEE ALSO