Skip Menu |

This queue is for tickets about the Moose CPAN distribution.

Report information
The Basics
Id: 79376
Status: resolved
Priority: 0/
Queue: Moose

People
Owner: Nobody in particular
Requestors: pshangov [...] yahoo.com
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: (no value)
Fixed in: (no value)



Subject: Move option number check from has()
Currently Moose::has() checks that it is is passed an even number of arguments (i.e. a hash). This check does not belong there as has() doesn't really care about those options. The earliest these matter is in the Moose::Meta::Attribute constructors - new() and interpolate_class_and_new(). The attached patch does two things: 1) Moves the option check to interpolate_class_and_new(). This means there is no check inside new(), but it seems to be always called from interpolate_class_and_new() during normal attribute construction. 2) Changes the error message since it is no-longer 'has'-specific. Also I have always found the existing error message rather confusing, as it does not tell me much about what the actual problem is. The patch is probably fairly basic and probably should not be applied as is, and probably needs more tests, but I would like to know if the approach makes sense. P.
Subject: move_option_check.patch
ÿþFrom d000ba751e994cb12c33efc68f2dec455621d638 Mon Sep 17 00:00:00 2001 From: Peter Shangov <pshangov@yahoo.com> Date: Mon, 3 Sep 2012 00:10:40 +0100 Subject: [PATCH] move option number check --- lib/Moose.pm | 7 ++----- lib/Moose/Meta/Attribute.pm | 9 +++++++-- t/attributes/misc_attribute_tests.t | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/Moose.pm b/lib/Moose.pm index 14cb7ba..c184a67 100644 --- a/lib/Moose.pm +++ b/lib/Moose.pm @@ -64,15 +64,12 @@ 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..8746afa 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -116,10 +116,15 @@ sub new { } sub interpolate_class_and_new { - my ($class, $name, %args) = @_; + my $class = shift; + my $name = shift; - my ( $new_class, @traits ) = $class->interpolate_class(\%args); + $class->throw_error('Attribute options must be an even number') + if @_ % 2 == 1; + + my %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..f5edd07 100644 --- a/t/attributes/misc_attribute_tests.t +++ b/t/attributes/misc_attribute_tests.t @@ -264,7 +264,7 @@ 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' ); + ::like( ::exception { has 'foo' => ( 'ro', isa => 'Str' ) }, qr/^Attribute/, 'has throws error with odd number of attribute options' ); } } -- 1.7.7.1.msysgit.0
Committed (with a better error message), thanks.