From: Dave Rolsky Date: Sun, 16 Sep 2012 17:24:26 +0000 (-0500) Subject: Move attr arg even/odd test to Moose::Meta::Attribute X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=42d1b6c38bfd203de88a93e26cba7015b27fecd0;p=gitmo%2FMoose.git Move attr arg even/odd test to Moose::Meta::Attribute --- diff --git a/Changes b/Changes index 8ec8923..5554cdc 100644 --- a/Changes +++ b/Changes @@ -11,6 +11,10 @@ for, noteworthy changes. * Roles can now override methods from other roles they consume directly, without needing to manually exclude them (just like classes can). (mst) + * The has() sub exported by Moose no longer checks whether it received an + even number of arguments (after the name). Instead, this is checked by + Moose::Meta::Attribute->interpolate_class_and_new. + [BUG FIXES] * Fix false positive when checking for circular references for modules that diff --git a/lib/Moose.pm b/lib/Moose.pm index 14cb7ba..5f1e3a2 100644 --- a/lib/Moose.pm +++ b/lib/Moose.pm @@ -64,15 +64,13 @@ sub has { my $meta = shift; my $name = shift; - Moose->throw_error('Usage: has \'name\' => ( key => value, ... )') - if @_ % 2 == 1; - my %context = Moose::Util::_caller_info; $context{context} = 'has declaration'; $context{type} = 'class'; - my %options = ( definition_context => \%context, @_ ); + + my @options = ( definition_context => \%context, @_ ); my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ]; - $meta->add_attribute( $_, %options ) for @$attrs; + $meta->add_attribute( $_, @options ) for @$attrs; } sub before { diff --git a/lib/Moose/Meta/Attribute.pm b/lib/Moose/Meta/Attribute.pm index 46a8412..ef52231 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -116,8 +116,12 @@ sub new { } sub interpolate_class_and_new { - my ($class, $name, %args) = @_; + my ($class, $name, @args) = @_; + $class->throw_error('Defining an attribute requires a hash of options') + if @args % 2; + + my %args = @args; my ( $new_class, @traits ) = $class->interpolate_class(\%args); $new_class->new($name, %args, ( scalar(@traits) ? ( traits => \@traits ) : () ) ); diff --git a/t/attributes/misc_attribute_tests.t b/t/attributes/misc_attribute_tests.t index c3d691c..01f6c90 100644 --- a/t/attributes/misc_attribute_tests.t +++ b/t/attributes/misc_attribute_tests.t @@ -260,13 +260,14 @@ ok(OutOfClassTest->meta->get_attribute('bar'), 'attr created from can'); { - { - package Foo; - use Moose; - - ::like( ::exception { has 'foo' => ( 'ro', isa => 'Str' ) }, qr/^Usage/, 'has throws error with odd number of attribute options' ); - } + package Foo; + use Moose; + ::like( + ::exception{ has 'foo' => ( 'ro', isa => 'Str' ) }, + qr/^Defining an attribute/, + 'has throws error with odd number of attribute options' + ); } done_testing;