From: Dave Rolsky Date: Wed, 13 Oct 2010 20:17:13 +0000 (-0500) Subject: Add explicit return values for (almost) all native delegation mutating methods X-Git-Tag: 1.16~47 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7f5ec80d99caad6df365a018719f875465358f17;p=gitmo%2FMoose.git Add explicit return values for (almost) all native delegation mutating methods --- diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/clear.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/clear.pm index 67d30a4..1b05915 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/clear.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/clear.pm @@ -14,6 +14,7 @@ with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { qw( _maximum_arguments _inline_optimized_set_new_value + _return_value ) ] }; @@ -30,6 +31,8 @@ sub _inline_optimized_set_new_value { return "$slot_access = []"; } +sub _return_value { return q{} } + no Moose::Role; 1; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/delete.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/delete.pm index 8b546b9..6618687 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/delete.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/delete.pm @@ -16,6 +16,7 @@ with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { _maximum_arguments _inline_check_arguments _inline_optimized_set_new_value + _return_value ) ], }; @@ -36,13 +37,19 @@ sub _potential_value { my ( $self, $slot_access ) = @_; return - "( do { my \@potential = \@{ $slot_access }; splice \@potential, \$_[0], 1; \\\@potential } )"; + "( do { my \@potential = \@{ $slot_access }; \@return = splice \@potential, \$_[0], 1; \\\@potential } )"; } sub _inline_optimized_set_new_value { my ( $self, $inv, $new, $slot_access ) = @_; - return "splice \@{ $slot_access }, \$_[0], 1"; + return "\@return = splice \@{ $slot_access }, \$_[0], 1"; +} + +sub _return_value { + my ( $self, $slot_access ) = @_; + + return 'return $return[0];'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/insert.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/insert.pm index 7377efc..588aa64 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/insert.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/insert.pm @@ -16,6 +16,7 @@ with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { _maximum_arguments _new_members _inline_optimized_set_new_value + _return_value ) ] }; @@ -41,6 +42,12 @@ sub _inline_optimized_set_new_value { return "splice \@{ $slot_access }, \$_[0], 0, \$_[1];"; } +sub _return_value { + my ( $self, $slot_access ) = @_; + + return "return ${slot_access}->[ \$_[0] ];"; +} + no Moose::Role; 1; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/push.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/push.pm index 8345587..238c3e1 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/push.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/push.pm @@ -9,8 +9,14 @@ our $AUTHORITY = 'cpan:STEVAN'; use Moose::Role; -with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => - { -excludes => ['_inline_optimized_set_new_value'] }; +with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { + -excludes => [ + qw( + _inline_optimized_set_new_value + _return_value + ) + ] +}; sub _adds_members { 1 } @@ -26,6 +32,12 @@ sub _inline_optimized_set_new_value { return "push \@{ $slot_access }, \@_"; } +sub _return_value { + my ( $self, $slot_access ) = @_; + + return "return scalar \@{ $slot_access }"; +} + no Moose::Role; 1; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/set.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/set.pm index db6c03c..e25c976 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/set.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/set.pm @@ -17,6 +17,7 @@ with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { _inline_check_arguments _new_members _inline_optimized_set_new_value + _return_value ) ] }; @@ -48,6 +49,12 @@ sub _inline_optimized_set_new_value { return "${slot_access}->[ \$_[0] ] = \$_[1]"; } +sub _return_value { + my ( $self, $slot_access ) = @_; + + return "return ${slot_access}->[ \$_[0] ];"; +} + no Moose::Role; 1; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/sort_in_place.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/sort_in_place.pm index 14d7d27..41ab90f 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/sort_in_place.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/sort_in_place.pm @@ -16,6 +16,7 @@ with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { qw( _maximum_arguments _inline_check_arguments + _return_value ) ] }; @@ -39,6 +40,8 @@ sub _potential_value { "[ \$_[0] ? sort { \$_[0]->( \$a, \$b ) } \@{ $slot_access } : sort \@{ $slot_access} ]"; } +sub _return_value { return q{} } + no Moose::Role; 1; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/splice.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/splice.pm index dbca15c..7099aa2 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/splice.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/splice.pm @@ -16,6 +16,7 @@ with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { _inline_process_arguments _inline_check_arguments _inline_optimized_set_new_value + _return_value ) ] }; @@ -41,13 +42,19 @@ sub _potential_value { my ( $self, $slot_access ) = @_; return "( do { my \@potential = \@{ $slot_access };" - . 'defined $len ? ( splice @potential, $idx, $len, @_ ) : ( splice @potential, $idx ); \\@potential } )'; + . '@return = defined $len ? ( splice @potential, $idx, $len, @_ ) : ( splice @potential, $idx ); \\@potential } )'; } sub _inline_optimized_set_new_value { my ( $self, $inv, $new, $slot_access ) = @_; - return "defined \$len ? ( splice \@{ $slot_access }, \$idx, \$len, \@_ ) : ( splice \@{ $slot_access }, \$idx )"; + return "\@return = defined \$len ? ( splice \@{ $slot_access }, \$idx, \$len, \@_ ) : ( splice \@{ $slot_access }, \$idx )"; +} + +sub _return_value { + my ($self, $slot_access) = @_; + + return 'return wantarray ? @return : $return[-1]'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/unshift.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/unshift.pm index 34185d9..2600b15 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/unshift.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/unshift.pm @@ -9,8 +9,14 @@ our $AUTHORITY = 'cpan:STEVAN'; use Moose::Role; -with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => - { -excludes => ['_inline_optimized_set_new_value'] }; +with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { + -excludes => [ + qw( + _inline_optimized_set_new_value + _return_value + ) + ] +}; sub _adds_members { 1 } @@ -26,6 +32,12 @@ sub _inline_optimized_set_new_value { return "unshift \@{ $slot_access }, \@_"; } +sub _return_value { + my ( $self, $slot_access ) = @_; + + return "return scalar \@{ $slot_access }"; +} + no Moose::Role; 1; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Bool/set.pm b/lib/Moose/Meta/Method/Accessor/Native/Bool/set.pm index a260941..e8cf889 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Bool/set.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Bool/set.pm @@ -16,7 +16,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { _inline_optimized_set_new_value ) ] -}; + }; sub _maximum_arguments { 0 } diff --git a/lib/Moose/Meta/Method/Accessor/Native/Counter/dec.pm b/lib/Moose/Meta/Method/Accessor/Native/Counter/dec.pm index 1a701cb..bc146ca 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Counter/dec.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Counter/dec.pm @@ -15,6 +15,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { _minimum_arguments _maximum_arguments _inline_optimized_set_new_value + _return_value ) ] }; @@ -34,6 +35,12 @@ sub _inline_optimized_set_new_value { return "$slot_access -= defined \$_[0] ? \$_[0] : 1"; } +sub _return_value { + my ( $self, $slot_access ) = @_; + + return "return $slot_access;"; +} + no Moose::Role; 1; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Counter/inc.pm b/lib/Moose/Meta/Method/Accessor/Native/Counter/inc.pm index 2f5a9b4..ada0a97 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Counter/inc.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Counter/inc.pm @@ -15,6 +15,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { _minimum_arguments _maximum_arguments _inline_optimized_set_new_value + _return_value ) ] }; @@ -34,6 +35,12 @@ sub _inline_optimized_set_new_value { return "$slot_access += defined \$_[0] ? \$_[0] : 1"; } +sub _return_value { + my ( $self, $slot_access ) = @_; + + return "return $slot_access;"; +} + no Moose::Role; 1; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Counter/reset.pm b/lib/Moose/Meta/Method/Accessor/Native/Counter/reset.pm index 72e788e..843e65a 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Counter/reset.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Counter/reset.pm @@ -14,6 +14,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { qw( _maximum_arguments _inline_optimized_set_new_value + _return_value ) ] }; @@ -32,6 +33,12 @@ sub _inline_optimized_set_new_value { return "$slot_access = \$attr->default(\$self)"; } +sub _return_value { + my ( $self, $slot_access ) = @_; + + return "return $slot_access;"; +} + no Moose::Role; 1; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Counter/set.pm b/lib/Moose/Meta/Method/Accessor/Native/Counter/set.pm index e6c00bd..4c3628b 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Counter/set.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Counter/set.pm @@ -15,6 +15,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { _minimum_arguments _maximum_arguments _inline_optimized_set_new_value + _return_value ) ] }; @@ -30,6 +31,12 @@ sub _inline_optimized_set_new_value { return "$slot_access = \$_[0];"; } +sub _return_value { + my ( $self, $slot_access ) = @_; + + return "return $slot_access;"; +} + no Moose::Role; 1; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/clear.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/clear.pm index c0fbd1f..b8dcc1d 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash/clear.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/clear.pm @@ -14,6 +14,7 @@ with 'Moose::Meta::Method::Accessor::Native::Hash::Writer' => { qw( _maximum_arguments _inline_optimized_set_new_value + _return_value ) ] }; @@ -30,6 +31,8 @@ sub _inline_optimized_set_new_value { return "$slot_access = {}"; } +sub _return_value { return q{} } + no Moose::Role; 1; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/delete.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/delete.pm index 2197700..7338f56 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash/delete.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/delete.pm @@ -9,21 +9,33 @@ our $AUTHORITY = 'cpan:STEVAN'; use Moose::Role; -with 'Moose::Meta::Method::Accessor::Native::Hash::Writer' => - { -excludes => ['_inline_optimized_set_new_value'] }; +with 'Moose::Meta::Method::Accessor::Native::Hash::Writer' => { + -excludes => [ + qw( + _inline_optimized_set_new_value + _return_value + ) + ], +}; sub _adds_members { 0 } sub _potential_value { my ( $self, $slot_access ) = @_; - return "( do { my \%potential = %{ $slot_access }; delete \@potential{\@_}; \\\%potential; } )"; + return "( do { my \%potential = %{ $slot_access }; \@return = delete \@potential{\@_}; \\\%potential; } )"; } sub _inline_optimized_set_new_value { my ( $self, $inv, $new, $slot_access ) = @_; - return "delete \@{ $slot_access }{\@_}"; + return "\@return = delete \@{ $slot_access }{\@_}"; +} + +sub _return_value { + my ( $self, $slot_access ) = @_; + + return 'return wantarray ? @return : $return[-1];'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/set.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/set.pm index 6512d6c..8f37313 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash/set.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/set.pm @@ -19,6 +19,7 @@ with 'Moose::Meta::Method::Accessor::Native::Hash::Writer' => { _inline_process_arguments _inline_check_arguments _inline_optimized_set_new_value + _return_value ) ], }; @@ -71,6 +72,12 @@ sub _inline_optimized_set_new_value { return "\@{ $slot_access }{ \@_[ \@keys_idx] } = \@_[ \@values_idx ]"; } +sub _return_value { + my ( $self, $slot_access ) = @_; + + return "return wantarray ? \@{ $slot_access }{ \@_[ \@keys_idx ] } : ${slot_access}->{ \$_[ \$keys_idx[0] ] };"; +} + no Moose::Role; 1; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/abs.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/abs.pm index 8a2173b..fe43d74 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Number/abs.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Number/abs.pm @@ -16,7 +16,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { _inline_optimized_set_new_value ) ] -}; + }; sub _maximum_arguments {0} diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/add.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/add.pm index 828d4d4..070e284 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Number/add.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Number/add.pm @@ -17,7 +17,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { _inline_optimized_set_new_value ) ] -}; + }; sub _minimum_arguments {1} diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/div.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/div.pm index 3e516a4..6848699 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Number/div.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Number/div.pm @@ -17,7 +17,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { _inline_optimized_set_new_value ) ] -}; + }; sub _minimum_arguments {1} diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/mod.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/mod.pm index 5419c68..b095723 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Number/mod.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Number/mod.pm @@ -17,7 +17,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { _inline_optimized_set_new_value ) ] -}; + }; sub _minimum_arguments {1} diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/mul.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/mul.pm index 055b28e..c910e8f 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Number/mul.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Number/mul.pm @@ -17,7 +17,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { _inline_optimized_set_new_value ) ] -}; + }; sub _minimum_arguments {1} diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/set.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/set.pm index ca08a01..51a0fdf 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Number/set.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Number/set.pm @@ -17,7 +17,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { _inline_optimized_set_new_value ) ] -}; + }; sub _minimum_arguments {1} sub _maximum_arguments {1} diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/sub.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/sub.pm index 1c581be..9e08576 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Number/sub.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Number/sub.pm @@ -17,7 +17,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { _inline_optimized_set_new_value ) ] -}; + }; sub _minimum_arguments {1} diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/append.pm b/lib/Moose/Meta/Method/Accessor/Native/String/append.pm index cfe52c8..5d0310c 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/append.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/append.pm @@ -17,7 +17,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { _inline_optimized_set_new_value ) ] -}; + }; sub _minimum_arguments { 1 } diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/chomp.pm b/lib/Moose/Meta/Method/Accessor/Native/String/chomp.pm index 14b5b48..5068958 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/chomp.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/chomp.pm @@ -14,22 +14,29 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { qw( _maximum_arguments _inline_optimized_set_new_value + _return_value ) ] }; -sub _maximum_arguments { 0 } +sub _maximum_arguments {0} sub _potential_value { my ( $self, $slot_access ) = @_; - return "( do { my \$val = $slot_access; chomp \$val; \$val } )"; + return "( do { my \$val = $slot_access; \@return = chomp \$val; \$val } )"; } sub _inline_optimized_set_new_value { my ( $self, $inv, $new, $slot_access ) = @_; - return "chomp $slot_access"; + return "\@return = chomp $slot_access"; +} + +sub _return_value { + my ( $self, $slot_access ) = @_; + + return '$return[0]'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/chop.pm b/lib/Moose/Meta/Method/Accessor/Native/String/chop.pm index 71411be..2e55c43 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/chop.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/chop.pm @@ -14,6 +14,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { qw( _maximum_arguments _inline_optimized_set_new_value + _return_value ) ] }; @@ -23,13 +24,19 @@ sub _maximum_arguments { 0 } sub _potential_value { my ( $self, $slot_access ) = @_; - return "( do { my \$val = $slot_access; chop \$val; \$val } )"; + return "( do { my \$val = $slot_access; \@return = chop \$val; \$val } )"; } sub _inline_optimized_set_new_value { my ( $self, $inv, $new, $slot_access ) = @_; - return "chop $slot_access"; + return "\@return = chop $slot_access"; +} + +sub _return_value { + my ( $self, $slot_access ) = @_; + + return '$return[0]'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/inc.pm b/lib/Moose/Meta/Method/Accessor/Native/String/inc.pm index 8f92363..84eb21e 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/inc.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/inc.pm @@ -16,7 +16,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { _inline_optimized_set_new_value ) ] -}; + }; sub _maximum_arguments { 0 } diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/prepend.pm b/lib/Moose/Meta/Method/Accessor/Native/String/prepend.pm index 50d530d..07a77d6 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/prepend.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/prepend.pm @@ -17,7 +17,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { _inline_optimized_set_new_value ) ] -}; + }; sub _minimum_arguments { 1 } diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/replace.pm b/lib/Moose/Meta/Method/Accessor/Native/String/replace.pm index bdfd68a..653ed82 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/replace.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/replace.pm @@ -21,7 +21,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { _inline_optimized_set_new_value ) ] -}; + }; sub _minimum_arguments { 1 } diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/substr.pm b/lib/Moose/Meta/Method/Accessor/Native/String/substr.pm index ed203ec..9e3b24c 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/substr.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/substr.pm @@ -108,19 +108,19 @@ sub _potential_value { my ( $self, $slot_access ) = @_; return - "( do { my \$potential = $slot_access; substr \$potential, \$offset, \$length, \$replacement; \$potential; } )"; + "( do { my \$potential = $slot_access; \@return = substr \$potential, \$offset, \$length, \$replacement; \$potential; } )"; } sub _inline_optimized_set_new_value { my ( $self, $inv, $new, $slot_access ) = @_; - return "substr $slot_access, \$offset, \$length, \$replacement"; + return "\@return = substr $slot_access, \$offset, \$length, \$replacement"; } sub _return_value { my ( $self, $slot_access, $for_writer ) = @_; - return q{} if $for_writer; + return '$return[0]' if $for_writer; return "substr $slot_access, \$offset, \$length"; } diff --git a/lib/Moose/Meta/Method/Accessor/Native/Writer.pm b/lib/Moose/Meta/Method/Accessor/Native/Writer.pm index a73e666..ffa460b 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Writer.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Writer.pm @@ -52,6 +52,12 @@ sub _writer_core { my $potential_value = $self->_potential_value($slot_access); + if ( $self->_return_value($slot_access) ) { + # some writers will save the return value in this variable when they + # generate the potential value. + $code .= "\n" . 'my @return;'; + } + $code .= "\n" . $self->_inline_copy_native_value( \$potential_value ); $code .= "\n" . $self->_inline_tc_code( @@ -167,7 +173,11 @@ sub _inline_optimized_set_new_value { return $self->_inline_store(@_); } -sub _return_value { return q{} } +sub _return_value { + my ( $self, $slot_access ) = @_; + + return $slot_access; +} no Moose::Role; diff --git a/t/070_native_traits/010_trait_array.t b/t/070_native_traits/010_trait_array.t index ad61bd1..c88fd6e 100644 --- a/t/070_native_traits/010_trait_array.t +++ b/t/070_native_traits/010_trait_array.t @@ -147,7 +147,10 @@ sub run_tests { lives_ok { $obj->push() } 'call to push without arguments lives'; - lives_ok { $obj->unshift( 101, 22 ) } + lives_and { + is( $obj->unshift( 101, 22 ), 8, + 'unshift returns size of the new array' ); + } 'unshifted two values and lived'; is_deeply( @@ -216,10 +219,14 @@ sub run_tests { qr/Cannot call get with more than 1 argument/, 'throws an error when get_curried is called with an argument'; - lives_ok { $obj->set( 1, 100 ) } 'set value at index 1 lives'; + lives_and { + is( $obj->set( 1, 100 ), 100, 'set returns new value' ); + } + 'set value at index 1 lives'; is( $obj->get(1), 100, 'get value at index 1 returns new value' ); + throws_ok { $obj->set( 1, 99, 42 ) } qr/Cannot call set with more than 2 arguments/, 'throws an error when set is called with three arguments'; @@ -245,7 +252,10 @@ sub run_tests { 'accessor with one argument returns value at index 1' ); - lives_ok { $obj->accessor( 1 => 97 ) } 'accessor as writer lives'; + lives_and { + is( $obj->accessor( 1 => 97 ), 97, 'accessor returns new value' ); + } + 'accessor as writer lives'; is( $obj->get(1), 97, @@ -305,9 +315,15 @@ sub run_tests { 'throws an error when is_empty is called with an argument'; $obj->clear; - $obj->push( 1, 5, 10, 42 ); + is( + $obj->push( 1, 5, 10, 42 ), 4, + 'pushed 4 elements, got number of elements in the array back' + ); - lives_ok { $obj->delete(2) } 'delete lives'; + lives_and { + is( $obj->delete(2), 10, 'delete returns deleted value' ); + } + 'delete lives'; is_deeply( $obj->_values, [ 1, 5, 42 ], @@ -340,17 +356,31 @@ sub run_tests { qr/Cannot call insert with more than 2 arguments/, 'throws an error when insert is called with three arguments'; - lives_ok { $obj->splice( 1, 0, 2, 3 ) } 'splice lives'; + lives_and { + is_deeply( + [ $obj->splice( 1, 0, 2, 3 ) ], + [], + 'return value of splice is empty list when not removing elements' + ); + } + 'splice lives'; is_deeply( $obj->_values, [ 1, 2, 3, 21, 42 ], 'splice added the specified elements' ); - lives_ok { $obj->splice( 1, 1, 99 ) } 'splice lives'; + lives_and { + is_deeply( + [ $obj->splice( 1, 2, 99 ) ], + [ 2, 3 ], + 'splice returns list of removed values' + ); + } + 'splice lives'; is_deeply( - $obj->_values, [ 1, 99, 3, 21, 42 ], + $obj->_values, [ 1, 99, 21, 42 ], 'splice added the specified elements' ); @@ -366,14 +396,14 @@ sub run_tests { 'splice_curried_1 lives'; is_deeply( - $obj->_values, [ 1, 101, 21, 42 ], + $obj->_values, [ 1, 101, 42 ], 'splice added the specified elements' ); lives_ok { $obj->splice_curried_2(102) } 'splice_curried_2 lives'; is_deeply( - $obj->_values, [ 1, 102, 42 ], + $obj->_values, [ 1, 102 ], 'splice added the specified elements' ); @@ -384,6 +414,18 @@ sub run_tests { 'splice added the specified elements' ); + is_deeply( + scalar $obj->splice( 1, 2 ), + 4, + 'splice in scalar context returns last element removed' + ); + + is_deeply( + scalar $obj->splice( 1, 0, 42 ), + undef, + 'splice in scalar context returns undef when no elements are removed' + ); + $obj->_values( [ 3, 9, 5, 22, 11 ] ); is_deeply( diff --git a/t/070_native_traits/020_trait_bool.t b/t/070_native_traits/020_trait_bool.t index b749895..5443020 100644 --- a/t/070_native_traits/020_trait_bool.t +++ b/t/070_native_traits/020_trait_bool.t @@ -74,7 +74,7 @@ sub run_tests { with_immutable { my $obj = $class->new; - $obj->illuminate; + ok( $obj->illuminate, 'set returns true' ); ok( $obj->is_lit, 'set is_lit to 1 using ->illuminate' ); ok( !$obj->is_dark, 'check if is_dark does the right thing' ); @@ -82,7 +82,7 @@ sub run_tests { qr/Cannot call set with any arguments/, 'set throws an error when an argument is passed'; - $obj->darken; + ok( !$obj->darken, 'unset returns false' ); ok( !$obj->is_lit, 'set is_lit to 0 using ->darken' ); ok( $obj->is_dark, 'check if is_dark does the right thing' ); @@ -90,7 +90,7 @@ sub run_tests { qr/Cannot call unset with any arguments/, 'unset throws an error when an argument is passed'; - $obj->flip_switch; + ok( $obj->flip_switch, 'toggle returns new value' ); ok( $obj->is_lit, 'toggle is_lit back to 1 using ->flip_switch' ); ok( !$obj->is_dark, 'check if is_dark does the right thing' ); diff --git a/t/070_native_traits/040_trait_counter.t b/t/070_native_traits/040_trait_counter.t index a20e5c2..ae7e816 100644 --- a/t/070_native_traits/040_trait_counter.t +++ b/t/070_native_traits/040_trait_counter.t @@ -79,31 +79,31 @@ sub run_tests { is( $obj->counter, 0, '... got the default value' ); - $obj->inc_counter; + is( $obj->inc_counter, 1, 'inc returns new value' ); is( $obj->counter, 1, '... got the incremented value' ); - $obj->inc_counter; + is( $obj->inc_counter, 2, 'inc returns new value' ); is( $obj->counter, 2, '... got the incremented value (again)' ); throws_ok { $obj->inc_counter( 1, 2 ) } qr/Cannot call inc with more than 1 argument/, 'inc throws an error when two arguments are passed'; - $obj->dec_counter; + is( $obj->dec_counter, 1, 'dec returns new value' ); is( $obj->counter, 1, '... got the decremented value' ); throws_ok { $obj->dec_counter( 1, 2 ) } qr/Cannot call dec with more than 1 argument/, 'dec throws an error when two arguments are passed'; - $obj->reset_counter; + is( $obj->reset_counter, 0, 'reset returns new value' ); is( $obj->counter, 0, '... got the original value' ); throws_ok { $obj->reset_counter(2) } qr/Cannot call reset with any arguments/, 'reset throws an error when an argument is passed'; - $obj->set_counter(5); + is( $obj->set_counter(5), 5, 'set returns new value' ); is( $obj->counter, 5, '... set the value' ); throws_ok { $obj->set_counter( 1, 2 ) } diff --git a/t/070_native_traits/050_trait_hash.t b/t/070_native_traits/050_trait_hash.t index 5b7cd19..e9ea77c 100644 --- a/t/070_native_traits/050_trait_hash.t +++ b/t/070_native_traits/050_trait_hash.t @@ -88,8 +88,12 @@ sub run_tests { is_deeply( $obj->options, {}, '... no options yet' ); ok( !$obj->has_option('foo'), '... we have no foo option' ); - lives_ok { - $obj->set_option( foo => 'bar' ); + lives_and { + is( + $obj->set_option( foo => 'bar' ), + 'bar', + 'set return single new value in scalar context' + ); } '... set the option okay'; @@ -134,13 +138,18 @@ sub run_tests { [qw(bar baz blah flop)], "get multiple options at once" ); - lives_ok { - $obj->delete_option('bar'); + lives_and { + is( scalar $obj->delete_option('bar'), 'baz', + 'delete returns deleted value' ); } '... deleted the option okay'; lives_ok { - $obj->delete_option( 'oink', 'xxy' ); + is_deeply( + [ $obj->delete_option( 'oink', 'xxy' ) ], + [ 'blah', 'flop' ], + 'delete returns all deleted values in list context' + ); } '... deleted multiple option okay'; @@ -181,7 +190,12 @@ sub run_tests { } '... bad constructor params'; - $obj->set_option( oink => "blah", xxy => "flop" ); + is_deeply( + [ $obj->set_option( oink => "blah", xxy => "flop" ) ], + [ 'blah', 'flop' ], + 'set returns newly set values in order of keys provided' + ); + my @key_value = sort { $a->[0] cmp $b->[0] } $obj->key_value; is_deeply( \@key_value, diff --git a/t/070_native_traits/060_trait_number.t b/t/070_native_traits/060_trait_number.t index 7eef720..6b24631 100644 --- a/t/070_native_traits/060_trait_number.t +++ b/t/070_native_traits/060_trait_number.t @@ -83,7 +83,7 @@ sub run_tests { is( $obj->integer, 5, 'Default to five' ); - $obj->add(10); + is( $obj->add(10), 15, 'add returns new value' ); is( $obj->integer, 15, 'Add ten for fithteen' ); @@ -91,7 +91,7 @@ sub run_tests { qr/Cannot call add with more than 1 argument/, 'add throws an error when 2 arguments are passed'; - $obj->sub(3); + is( $obj->sub(3), 12, 'sub returns new value' ); is( $obj->integer, 12, 'Subtract three for 12' ); @@ -99,7 +99,7 @@ sub run_tests { qr/Cannot call sub with more than 1 argument/, 'sub throws an error when 2 arguments are passed'; - $obj->set(10); + is( $obj->set(10), 10, 'set returns new value' ); is( $obj->integer, 10, 'Set to ten' ); @@ -107,7 +107,7 @@ sub run_tests { qr/Cannot call set with more than 1 argument/, 'set throws an error when 2 arguments are passed'; - $obj->div(2); + is( $obj->div(2), 5, 'div returns new value' ); is( $obj->integer, 5, 'divide by 2' ); @@ -115,7 +115,7 @@ sub run_tests { qr/Cannot call div with more than 1 argument/, 'div throws an error when 2 arguments are passed'; - $obj->mul(2); + is( $obj->mul(2), 10, 'mul returns new value' ); is( $obj->integer, 10, 'multiplied by 2' ); @@ -123,7 +123,7 @@ sub run_tests { qr/Cannot call mul with more than 1 argument/, 'mul throws an error when 2 arguments are passed'; - $obj->mod(2); + is( $obj->mod(2), 0, 'mod returns new value' ); is( $obj->integer, 0, 'Mod by 2' ); @@ -139,7 +139,7 @@ sub run_tests { $obj->set(-1); - $obj->abs; + is( $obj->abs, 1, 'abs returns new value' ); throws_ok { $obj->abs(10) } qr/Cannot call abs with any arguments/, diff --git a/t/070_native_traits/070_trait_string.t b/t/070_native_traits/070_trait_string.t index 5f6c028..c6b9c63 100644 --- a/t/070_native_traits/070_trait_string.t +++ b/t/070_native_traits/070_trait_string.t @@ -96,14 +96,14 @@ sub run_tests { qr/Cannot call length with any arguments/, 'length throws an error when an argument is passed'; - $obj->inc; + is( $obj->inc, 'b', 'inc returns new value' ); is( $obj->_string, 'b', 'a becomes b after inc' ); throws_ok { $obj->inc(42) } qr/Cannot call inc with any arguments/, 'inc throws an error when an argument is passed'; - $obj->append('foo'); + is( $obj->append('foo'), 'bfoo', 'append returns new value' ); is( $obj->_string, 'bfoo', 'appended to the string' ); throws_ok { $obj->append( 'foo', 2 ) } @@ -118,10 +118,10 @@ sub run_tests { 'append_curried throws an error when two arguments are passed'; $obj->_string("has nl$/"); - $obj->chomp; + is( $obj->chomp, 1, 'chomp returns number of characters removed' ); is( $obj->_string, 'has nl', 'chomped string' ); - $obj->chomp; + is( $obj->chomp, 0, 'chomp returns number of characters removed' ); is( $obj->_string, 'has nl', 'chomp is a no-op when string has no line ending' @@ -131,7 +131,7 @@ sub run_tests { qr/Cannot call chomp with any arguments/, 'chomp throws an error when an argument is passed'; - $obj->chop; + is( $obj->chop, 'l', 'chop returns character removed' ); is( $obj->_string, 'has n', 'chopped string' ); throws_ok { $obj->chop(42) } @@ -139,13 +139,18 @@ sub run_tests { 'chop throws an error when an argument is passed'; $obj->_string('x'); - $obj->prepend('bar'); + is( $obj->prepend('bar'), 'barx', 'prepend returns new value' ); is( $obj->_string, 'barx', 'prepended to string' ); $obj->prepend_curried; is( $obj->_string, '-barx', 'prepend_curried prepended to string' ); - $obj->replace( qr/([ao])/, sub { uc($1) } ); + is( + $obj->replace( qr/([ao])/, sub { uc($1) } ), + '-bArx', + 'replace returns new value' + ); + is( $obj->_string, '-bArx', 'substitution using coderef for replacement' @@ -235,7 +240,11 @@ sub run_tests { 'substr as getter with two arguments' ); - $obj->substr( 1, 3, 'ong' ); + is( + $obj->substr( 1, 3, 'ong' ), + 'ome', + 'substr as setter returns replaced string' + ); is( $obj->_string, 'song long string',