From: Jesse Luehrs Date: Thu, 20 Aug 2009 05:04:49 +0000 (-0500) Subject: redo the currying syntax to get rid of one of the arrayrefs X-Git-Tag: 0.89_02~53 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3c573ca421d4a4e281e8ceb484bb99062e1ebcde;p=gitmo%2FMoose.git redo the currying syntax to get rid of one of the arrayrefs this will make it more difficult to add things like the subref form of currying in the future, but it is simpler and cleaner for the vastly more common case, and the subref form could be added back in via something like handles => { foo => { grep => sub { ... } } } (rather than using [] for the middle pair of braces, which would continue to mean normal currying) if we decide it's really necessary. --- diff --git a/lib/Moose.pm b/lib/Moose.pm index 3a35baf..2f2ad0c 100644 --- a/lib/Moose.pm +++ b/lib/Moose.pm @@ -546,13 +546,13 @@ You may also use an array reference to curry arguments to the original method. has 'thing' => ( ... - handles => { set_foo => [ set => [ 'foo' ] ] }, + handles => { set_foo => [ set => 'foo' ] }, ); # $self->set_foo(...) calls $self->thing->set('foo', ...) The first element of the array reference is the original method name, and the -second is an array reference of curried arguments. +rest is a list of curried arguments. =item C diff --git a/lib/Moose/Meta/Attribute.pm b/lib/Moose/Meta/Attribute.pm index 821fe4a..69d3c1e 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -736,9 +736,9 @@ sub _make_delegation_method { $method_body = $method_to_call if 'CODE' eq ref($method_to_call); - my $curried_arguments = []; + my @curried_arguments; - ($method_to_call, $curried_arguments) = @$method_to_call + ($method_to_call, @curried_arguments) = @$method_to_call if 'ARRAY' eq ref($method_to_call); return $self->delegation_metaclass->new( @@ -746,7 +746,7 @@ sub _make_delegation_method { package_name => $self->associated_class->name, attribute => $self, delegate_to_method => $method_to_call, - curried_arguments => $curried_arguments || [], + curried_arguments => \@curried_arguments, ); } diff --git a/lib/Moose/Meta/Attribute/Native/Trait.pm b/lib/Moose/Meta/Attribute/Native/Trait.pm index 5058ba7..0ea1d08 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait.pm @@ -109,9 +109,7 @@ around '_make_delegation_method' => sub { my $next = shift; my ( $self, $handle_name, $method_to_call ) = @_; - my ( $name, $curried_args ) = @$method_to_call; - - $curried_args ||= []; + my ( $name, @curried_args ) = @$method_to_call; my $method_constructors = $self->method_constructors; @@ -126,7 +124,7 @@ around '_make_delegation_method' => sub { $handle_name, sub { my $instance = shift; - return $code->( $instance, @$curried_args, @_ ); + return $code->( $instance, @curried_args, @_ ); }, ); }; diff --git a/t/020_attributes/010_attribute_delegation.t b/t/020_attributes/010_attribute_delegation.t index e3a5f0a..95bd73c 100644 --- a/t/020_attributes/010_attribute_delegation.t +++ b/t/020_attributes/010_attribute_delegation.t @@ -32,7 +32,7 @@ use Test::Exception; handles => { 'foo_bar' => 'bar', foo_baz => 'baz', - 'foo_bar_to_20' => [ bar => [ 20 ] ], + 'foo_bar_to_20' => [ bar => 20 ], }, ); } diff --git a/t/070_native_traits/202_trait_array.t b/t/070_native_traits/202_trait_array.t index 4c745ec..25afb92 100644 --- a/t/070_native_traits/202_trait_array.t +++ b/t/070_native_traits/202_trait_array.t @@ -33,11 +33,11 @@ my $sort; 'sort_options_in_place' => 'sort_in_place', 'option_accessor' => 'accessor', 'add_options_with_speed' => - [ 'push' => [ 'funrolls', 'funbuns' ] ], + [ 'push' => 'funrolls', 'funbuns' ], 'prepend_prerequisites_along_with' => - [ 'unshift' => [ 'first', 'second' ] ], + [ 'unshift' => 'first', 'second' ], 'descending_options' => - [ 'sort_in_place' => [ $sort = sub { $_[1] <=> $_[0] } ] ], + [ 'sort_in_place' => ($sort = sub { $_[1] <=> $_[0] }) ], } ); } @@ -260,12 +260,10 @@ is_deeply( 'splice_options' => 'splice', 'sort_options_in_place' => 'sort_in_place', 'option_accessor' => 'accessor', - 'add_options_with_speed' => - [ 'push' => [ 'funrolls', 'funbuns' ] ], + 'add_options_with_speed' => [ 'push' => 'funrolls', 'funbuns' ], 'prepend_prerequisites_along_with' => - [ 'unshift' => [ 'first', 'second' ] ], - 'descending_options' => - [ 'sort_in_place' => [$sort] ], + [ 'unshift' => 'first', 'second' ], + 'descending_options' => [ 'sort_in_place' => $sort ], }, '... got the right handles mapping' ); diff --git a/t/070_native_traits/203_trait_hash.t b/t/070_native_traits/203_trait_hash.t index 2ea8001..fe2e649 100644 --- a/t/070_native_traits/203_trait_hash.t +++ b/t/070_native_traits/203_trait_hash.t @@ -28,7 +28,7 @@ use Test::Moose 'does_ok'; 'option_accessor' => 'accessor', 'key_value' => 'kv', 'options_elements' => 'elements', - 'quantity' => [ accessor => ['quantity'] ], + 'quantity' => [ accessor => 'quantity' ], }, ); } @@ -157,7 +157,7 @@ is_deeply( 'option_accessor' => 'accessor', 'key_value' => 'kv', 'options_elements' => 'elements', - 'quantity' => [ accessor => ['quantity'] ], + 'quantity' => [ accessor => 'quantity' ], }, '... got the right handles mapping' ); diff --git a/t/070_native_traits/204_trait_number.t b/t/070_native_traits/204_trait_number.t index c3566cc..48736f8 100644 --- a/t/070_native_traits/204_trait_number.t +++ b/t/070_native_traits/204_trait_number.t @@ -23,10 +23,10 @@ use Test::Moose; div => 'div', mod => 'mod', abs => 'abs', - inc => [ add => [1] ], - dec => [ sub => [1] ], - odd => [ mod => [2] ], - cut_in_half => [ div => [2] ], + inc => [ add => 1 ], + dec => [ sub => 1 ], + odd => [ mod => 2 ], + cut_in_half => [ div => 2 ], }, ); @@ -102,10 +102,10 @@ is_deeply( div => 'div', mod => 'mod', abs => 'abs', - inc => [ add => [1] ], - dec => [ sub => [1] ], - odd => [ mod => [2] ], - cut_in_half => [ div => [2] ], + inc => [ add => 1 ], + dec => [ sub => 1 ], + odd => [ mod => 2 ], + cut_in_half => [ div => 2 ], }, '... got the right handles mapping' ); diff --git a/t/070_native_traits/205_trait_list.t b/t/070_native_traits/205_trait_list.t index f6fe862..7ec29a0 100644 --- a/t/070_native_traits/205_trait_list.t +++ b/t/070_native_traits/205_trait_list.t @@ -30,10 +30,10 @@ my $up; 'join_options' => 'join', 'get_option_at' => 'get', 'sorted_options' => 'sort', - 'less_than_five' => [ grep => [ $less = sub { $_ < 5 } ] ], - 'up_by_one' => [ map => [ $up = sub { $_ + 1 } ] ], - 'dashify' => [ join => ['-'] ], - 'descending' => [ sort => [ $sort = sub { $_[1] <=> $_[0] } ] ], + 'less_than_five' => [ grep => ($less = sub { $_ < 5 }) ], + 'up_by_one' => [ map => ($up = sub { $_ + 1 }) ], + 'dashify' => [ join => '-' ], + 'descending' => [ sort => ($sort = sub { $_[1] <=> $_[0] }) ], }, ); @@ -121,10 +121,10 @@ is_deeply( 'join_options' => 'join', 'get_option_at' => 'get', 'sorted_options' => 'sort', - 'less_than_five' => [ grep => [$less] ], - 'up_by_one' => [ map => [$up] ], - 'dashify' => [ join => ['-'] ], - 'descending' => [ sort => [$sort] ], + 'less_than_five' => [ grep => $less ], + 'up_by_one' => [ map => $up ], + 'dashify' => [ join => '-' ], + 'descending' => [ sort => $sort ], }, '... got the right handles mapping' ); diff --git a/t/070_native_traits/207_trait_string.t b/t/070_native_traits/207_trait_string.t index c14ff01..7c85ae8 100644 --- a/t/070_native_traits/207_trait_string.t +++ b/t/070_native_traits/207_trait_string.t @@ -26,10 +26,9 @@ my $uc; chomp_string => 'chomp', clear_string => 'clear', length_string => 'length', - exclaim => [ append => ['!'] ], - capitalize_last => - [ replace => [ qr/(.)$/, $uc = sub { uc $1 } ] ], - invalid_number => [ match => [qr/\D/] ], + exclaim => [ append => '!' ], + capitalize_last => [ replace => qr/(.)$/, ($uc = sub { uc $1 }) ], + invalid_number => [ match => qr/\D/ ], }, ); } @@ -108,9 +107,9 @@ is_deeply( chomp_string => 'chomp', clear_string => 'clear', length_string => 'length', - exclaim => [ append => ['!'] ], - capitalize_last => [ replace => [ qr/(.)$/, $uc ] ], - invalid_number => [ match => [qr/\D/] ], + exclaim => [ append => '!' ], + capitalize_last => [ replace => qr/(.)$/, $uc ], + invalid_number => [ match => qr/\D/ ], }, '... got the right handles methods' );