From: Jesse Luehrs Date: Sun, 31 Oct 2010 03:27:43 +0000 (-0500) Subject: make native trait inlining work X-Git-Tag: 1.9900~40 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=53a4677c8ac29f814924cd2eb21a760b10bf7f5d;p=gitmo%2FMoose.git make native trait inlining work --- diff --git a/lib/Moose/Meta/Method/Accessor.pm b/lib/Moose/Meta/Method/Accessor.pm index 0f0c220..a369d87 100644 --- a/lib/Moose/Meta/Method/Accessor.pm +++ b/lib/Moose/Meta/Method/Accessor.pm @@ -61,7 +61,6 @@ sub _generate_accessor_method_inline { my $slot_access = $self->_inline_get($inv); my $value = $self->_value_needs_copy ? '$val' : '$_[1]'; my $old = '@old'; - my $default = '$default'; $self->_compile_code([ 'sub {', @@ -69,13 +68,12 @@ sub _generate_accessor_method_inline { 'if (scalar(@_) >= 2) {', $self->_inline_copy_value($value), $self->_inline_check_required, - $self->_inline_check_coercion($value), - $self->_inline_check_constraint($value), + $self->_inline_tc_code($value), $self->_inline_get_old_value_for_trigger($inv, $old), $self->_inline_store($inv, $value), $self->_inline_trigger($inv, $value, $old), '}', - $self->_inline_check_lazy($inv, $default), + $self->_inline_check_lazy($inv), $self->_inline_post_body(@_), 'return ' . $self->_inline_auto_deref($slot_access) . ';', '}', @@ -94,8 +92,7 @@ sub _generate_writer_method_inline { $self->_inline_pre_body(@_), $self->_inline_copy_value($value), $self->_inline_check_required, - $self->_inline_check_coercion($value), - $self->_inline_check_constraint($value), + $self->_inline_tc_code($value), $self->_inline_get_old_value_for_trigger($inv, $old), $self->_inline_store($inv, $value), $self->_inline_post_body(@_), @@ -109,7 +106,6 @@ sub _generate_reader_method_inline { my $inv = '$_[0]'; my $slot_access = $self->_inline_get($inv); - my $default = '$default'; $self->_compile_code([ 'sub {', @@ -120,7 +116,7 @@ sub _generate_reader_method_inline { 'data => \@_' ) . ';', '}', - $self->_inline_check_lazy($inv, $default), + $self->_inline_check_lazy($inv), $self->_inline_post_body(@_), 'return ' . $self->_inline_auto_deref($slot_access) . ';', '}', @@ -175,8 +171,8 @@ sub _generate_clearer_method { : $self->SUPER::_generate_clearer_method(@_); } -sub _inline_pre_body { '' } -sub _inline_post_body { '' } +sub _inline_pre_body { return } +sub _inline_post_body { return } sub _inline_check_constraint { my $self = shift; @@ -197,6 +193,14 @@ sub _inline_check_constraint { '}'; } +sub _inline_tc_code { + my $self = shift; + return ( + $self->_inline_check_coercion(@_), + $self->_inline_check_constraint(@_), + ); +} + sub _inline_check_coercion { my $self = shift; my ($value) = @_; @@ -229,18 +233,18 @@ sub _inline_check_lazy { my ($instance, $default) = @_; my $attr = $self->associated_attribute; - return '' unless $attr->is_lazy; + return unless $attr->is_lazy; my $slot_exists = $self->_inline_has($instance); return 'if (!' . $slot_exists . ') {', - $self->_inline_init_from_default($instance, $default), + $self->_inline_init_from_default($instance, '$default', 'lazy'), '}'; } sub _inline_init_from_default { my $self = shift; - my ($instance, $default) = @_; + my ($instance, $default, $for_lazy) = @_; my $attr = $self->associated_attribute; # XXX: should this throw an error instead? @@ -249,8 +253,12 @@ sub _inline_init_from_default { return $self->_inline_generate_default($instance, $default), $attr->has_type_constraint - ? ($self->_inline_check_coercion($default), - $self->_inline_check_constraint($default)) + # intentionally not using _inline_tc_code, since that can be + # overridden to do things like possibly only do member tc + # checks, which isn't appropriate for checking the result + # of a default + ? ($self->_inline_check_coercion($default, $for_lazy), + $self->_inline_check_constraint($default, $for_lazy)) : (), $self->_inline_init_slot($attr, $instance, $default); } diff --git a/lib/Moose/Meta/Method/Accessor/Native.pm b/lib/Moose/Meta/Method/Accessor/Native.pm index 5c8e22b..49d214b 100644 --- a/lib/Moose/Meta/Method/Accessor/Native.pm +++ b/lib/Moose/Meta/Method/Accessor/Native.pm @@ -32,20 +32,19 @@ around new => sub { return $class->$orig(%options); }; -around _new => sub { - shift; +sub _new { my $class = shift; my $options = @_ == 1 ? $_[0] : {@_}; return bless $options, $class; -}; +} sub root_types { (shift)->{'root_types'} } sub _initialize_body { my $self = shift; - $self->{'body'} = $self->_compile_code( $self->_generate_method ); + $self->{'body'} = $self->_compile_code( [$self->_generate_method] ); return; } @@ -53,64 +52,77 @@ sub _initialize_body { sub _inline_curried_arguments { my $self = shift; - return q{} unless @{ $self->curried_arguments }; + return unless @{ $self->curried_arguments }; - return 'unshift @_, @curried;' + return ('unshift @_, @curried;'); } sub _inline_check_argument_count { my $self = shift; - my $code = q{}; - - if ( my $min = $self->_minimum_arguments ) { - my $err_msg = sprintf( - q{"Cannot call %s without at least %s argument%s"}, - $self->delegate_to_method, - $min, - ( $min == 1 ? q{} : 's' ) + my @code; + + if (my $min = $self->_minimum_arguments) { + push @code, ( + 'if (@_ < ' . $min . ') {', + $self->_inline_throw_error( + sprintf( + '"Cannot call %s without at least %s argument%s"', + $self->delegate_to_method, + $min, + ($min == 1 ? '' : 's'), + ) + ) . ';', + '}', ); - - $code - .= "\n" - . $self->_inline_throw_error($err_msg) - . " unless \@_ >= $min;"; } - if ( defined( my $max = $self->_maximum_arguments ) ) { - my $err_msg = sprintf( - q{"Cannot call %s with %s argument%s"}, - $self->delegate_to_method, - ( $max ? "more than $max" : 'any' ), - ( $max == 1 ? q{} : 's' ) + if (defined(my $max = $self->_maximum_arguments)) { + push @code, ( + 'if (@_ > ' . $max . ') {', + $self->_inline_throw_error( + sprintf( + '"Cannot call %s with %s argument%s"', + $self->delegate_to_method, + $max ? "more than $max" : 'any', + ($max == 1 ? '' : 's'), + ) + ) . ';', + '}', ); - - $code - .= "\n" - . $self->_inline_throw_error($err_msg) - . " if \@_ > $max;"; } - return $code; + return @code; +} + +sub _inline_return_value { + my $self = shift; + my ($slot_access, $for_writer) = @_; + + return ( + 'return ' . $self->_return_value($slot_access, $for_writer) . ';', + ); } sub _minimum_arguments { 0 } sub _maximum_arguments { undef } override _inline_get => sub { - my ( $self, $instance ) = @_; + my $self = shift; + my ($instance) = @_; return $self->_slot_access_can_be_inlined ? super() - : "${instance}->\$reader"; + : $instance . '->$reader'; }; override _inline_store => sub { - my ( $self, $instance, $value ) = @_; + my $self = shift; + my ($instance, $value) = @_; return $self->_slot_access_can_be_inlined ? super() - : "${instance}->\$writer($value)"; + : $instance . '->$writer(' . $value . ')'; }; override _eval_environment => sub { diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array.pm b/lib/Moose/Meta/Method/Accessor/Native/Array.pm index 224e907..0eb17a3 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array.pm @@ -12,12 +12,18 @@ $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; sub _inline_check_var_is_valid_index { - my ( $self, $var ) = @_; - - return $self->_inline_throw_error( q{'The index passed to } - . $self->delegate_to_method - . q{ must be an integer'} ) - . qq{ unless defined $var && $var =~ /^-?\\d+\$/;}; + my $self = shift; + my ($var) = @_; + + return ( + 'if (!defined(' . $var . ') || ' . $var . ' !~ /^-?\d+$/) {', + $self->_inline_throw_error( + '"The index passed to ' + . $self->delegate_to_method + . ' must be an integer"', + ) . ';', + '}', + ); } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/Writer.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/Writer.pm index bf86f5a..31bd29f 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/Writer.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/Writer.pm @@ -9,14 +9,17 @@ our $AUTHORITY = 'cpan:STEVAN'; use Moose::Role; -with 'Moose::Meta::Method::Accessor::Native::Writer', +with 'Moose::Meta::Method::Accessor::Native::Writer' => { + -excludes => ['_inline_coerce_new_values'], + }, 'Moose::Meta::Method::Accessor::Native::Array', 'Moose::Meta::Method::Accessor::Native::Collection'; sub _new_members {'@_'} sub _inline_copy_old_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; return '[ @{(' . $slot_access . ')} ]'; } diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/accessor.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/accessor.pm index ee08424..393fc1f 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/accessor.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/accessor.pm @@ -32,43 +32,27 @@ with 'Moose::Meta::Method::Accessor::Native::Array::set' => { sub _generate_method { my $self = shift; - my $inv = '$self'; - - my $code = 'sub {'; - $code .= "\n" . $self->_inline_pre_body(@_); - - $code .= "\n" . 'my $self = shift;'; - - $code .= "\n" . $self->_inline_curried_arguments; - - $code .= "\n" . $self->_inline_check_lazy($inv); - + my $inv = '$self'; my $slot_access = $self->_inline_get($inv); - # get - $code .= "\n" . 'if ( @_ == 1 ) {'; - - $code .= "\n" . $self->_inline_check_var_is_valid_index('$_[0]'); - - $code - .= "\n" - . 'return ' - . $self - ->Moose::Meta::Method::Accessor::Native::Array::get::_return_value( - $slot_access) - . ';'; - - # set - $code .= "\n" . '} else {'; - - $code .= "\n" . $self->_writer_core( $inv, $slot_access ); - - $code .= "\n" . $self->_inline_post_body(@_); - - $code .= "\n}"; - $code .= "\n}"; - - return $code; + return ( + 'sub {', + $self->_inline_pre_body(@_), + 'my ' . $inv . ' = shift;', + $self->_inline_curried_arguments, + $self->_inline_check_lazy($inv), + # get + 'if (@_ == 1) {', + $self->_inline_check_var_is_valid_index('$_[0]'), + $self->Moose::Meta::Method::Accessor::Native::Array::get::_inline_return_value($slot_access), + '}', + # set + 'else {', + $self->_writer_core($inv, $slot_access), + $self->_inline_post_body(@_), + '}', + '}', + ); } sub _minimum_arguments {1} diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/clear.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/clear.pm index 27d60cd..e73384c 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/clear.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/clear.pm @@ -13,7 +13,7 @@ with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { -excludes => [ qw( _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value _return_value ) ] @@ -25,13 +25,14 @@ sub _adds_members { 0 } sub _potential_value { return '[]' } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access = []"; + return $slot_access . ' = []'; } -sub _return_value { return q{} } +sub _return_value { return '' } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/count.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/count.pm index 945a22b..8b4de0f 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/count.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/count.pm @@ -15,9 +15,10 @@ with 'Moose::Meta::Method::Accessor::Native::Reader' => sub _maximum_arguments { 0 } sub _return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "scalar \@{ ($slot_access) }"; + return 'scalar @{ (' . $slot_access . ') }'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/delete.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/delete.pm index 06baa74..58189c3 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/delete.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/delete.pm @@ -15,7 +15,7 @@ with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { _minimum_arguments _maximum_arguments _inline_check_arguments - _inline_optimized_set_new_value + _optimized_set_new_value _return_value ) ], @@ -34,22 +34,28 @@ sub _inline_check_arguments { sub _adds_members { 0 } sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return - "( do { my \@potential = \@{ ($slot_access) }; \@return = splice \@potential, \$_[0], 1; \\\@potential } )"; + return '(do { ' + . 'my @potential = @{ (' . $slot_access . ') }; ' + . '@return = splice @potential, $_[0], 1; ' + . '\@potential; ' + . '})'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "\@return = splice \@{ ($slot_access) }, \$_[0], 1"; + return '@return = splice @{ (' . $slot_access . ') }, $_[0], 1'; } sub _return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return 'return $return[0];'; + return '$return[0]'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/elements.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/elements.pm index 512e444..556655b 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/elements.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/elements.pm @@ -15,10 +15,10 @@ with 'Moose::Meta::Method::Accessor::Native::Reader' => sub _maximum_arguments { 0 } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return "\@{ ($slot_access) }"; + return '@{ (' . $slot_access . ') }'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/first.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/first.pm index fc35c9e..bb3b687 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/first.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/first.pm @@ -29,16 +29,20 @@ sub _maximum_arguments { 1 } sub _inline_check_arguments { my $self = shift; - return $self->_inline_throw_error( - q{'The argument passed to first must be a code reference'}) - . q{ unless Params::Util::_CODELIKE( $_[0] );}; + return ( + 'if (!Params::Util::_CODELIKE($_[0])) {', + $self->_inline_throw_error( + '"The argument passed to first must be a code reference"', + ) . ';', + '}', + ); } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return "&List::Util::first( \$_[0], \@{ ($slot_access) } )"; + return '&List::Util::first( $_[0], @{ (' . $slot_access . ') } )'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/get.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/get.pm index b2a79f5..c440270 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/get.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/get.pm @@ -33,10 +33,10 @@ sub _inline_check_arguments { } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return "${slot_access}->[ \$_[0] ]"; + return $slot_access . '->[ $_[0] ]'; } 1; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/grep.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/grep.pm index 2c82f69..389dc65 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/grep.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/grep.pm @@ -28,16 +28,20 @@ sub _maximum_arguments { 1 } sub _inline_check_arguments { my $self = shift; - return $self->_inline_throw_error( - q{'The argument passed to grep must be a code reference'}) - . q{ unless Params::Util::_CODELIKE( $_[0] );}; + return ( + 'if (!Params::Util::_CODELIKE($_[0])) {', + $self->_inline_throw_error( + '"The argument passed to grep must be a code reference"', + ) . ';', + '}', + ); } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return "grep { \$_[0]->() } \@{ ($slot_access) }"; + return 'grep { $_[0]->() } @{ (' . $slot_access . ') }'; } 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 ff12580..886344c 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/insert.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/insert.pm @@ -14,8 +14,9 @@ with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { qw( _minimum_arguments _maximum_arguments + _inline_coerce_new_values _new_members - _inline_optimized_set_new_value + _optimized_set_new_value _return_value ) ] @@ -28,37 +29,42 @@ sub _maximum_arguments { 2 } sub _adds_members { 1 } sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return - "( do { my \@potential = \@{ ($slot_access) }; splice \@potential, \$_[0], 0, \$_[1]; \\\@potential } )"; + return '(do { ' + . 'my @potential = @{ (' . $slot_access . ') }; ' + . 'splice @potential, $_[0], 0, $_[1]; ' + . '\@potential; ' + . '})'; } # We need to override this because while @_ can be written to, we cannot write # directly to $_[1]. -around _inline_coerce_new_values => sub { - shift; +sub _inline_coerce_new_values { my $self = shift; - return q{} unless $self->associated_attribute->should_coerce; + return unless $self->associated_attribute->should_coerce; - return q{} unless $self->_tc_member_type_can_coerce; + return unless $self->_tc_member_type_can_coerce; - return '@_ = ( $_[0], $member_tc_obj->coerce( $_[1] ) );'; + return '@_ = ($_[0], $member_tc_obj->coerce($_[1]));'; }; sub _new_members { '$_[1]' } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "splice \@{ ($slot_access) }, \$_[0], 0, \$_[1];"; + return 'splice @{ (' . $slot_access . ') }, $_[0], 0, $_[1]'; } sub _return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "return ${slot_access}->[ \$_[0] ];"; + return $slot_access . '->[ $_[0] ]'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/is_empty.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/is_empty.pm index ebcc150..c64c0a3 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/is_empty.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/is_empty.pm @@ -15,10 +15,10 @@ with 'Moose::Meta::Method::Accessor::Native::Reader' => sub _maximum_arguments { 0 } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return "\@{ ($slot_access) } ? 0 : 1"; + return '@{ (' . $slot_access . ') } ? 0 : 1'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/join.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/join.pm index 6215e07..40f488e 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/join.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/join.pm @@ -28,16 +28,20 @@ sub _maximum_arguments { 1 } sub _inline_check_arguments { my $self = shift; - return $self->_inline_throw_error( - q{'The argument passed to join must be a string'}) - . ' unless Moose::Util::_STRINGLIKE0( $_[0] );'; + return ( + 'if (!Moose::Util::_STRINGLIKE0($_[0])) {', + $self->_inline_throw_error( + '"The argument passed to join must be a string"', + ) . ';', + '}', + ); } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return "join \$_[0], \@{ ($slot_access) }"; + return 'join $_[0], @{ (' . $slot_access . ') }'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/map.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/map.pm index f6837b0..07c65f3 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/map.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/map.pm @@ -28,16 +28,20 @@ sub _maximum_arguments { 1 } sub _inline_check_arguments { my $self = shift; - return $self->_inline_throw_error( - q{'The argument passed to map must be a code reference'}) - . q{ unless Params::Util::_CODELIKE( $_[0] );}; + return ( + 'if (!Params::Util::_CODELIKE($_[0])) {', + $self->_inline_throw_error( + '"The argument passed to map must be a code reference"', + ) . ';', + '}', + ); } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return "map { \$_[0]->() } \@{ ($slot_access) }"; + return 'map { $_[0]->() } @{ (' . $slot_access . ') }'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/natatime.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/natatime.pm index 971f3ef..37f73ba 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/natatime.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/natatime.pm @@ -30,25 +30,36 @@ sub _maximum_arguments {2} sub _inline_check_arguments { my $self = shift; - return $self->_inline_throw_error( - q{'The n value passed to natatime must be an integer'}) - . ' unless defined $_[0] && $_[0] =~ /^\\d+$/;' . "\n" - . $self->_inline_throw_error( - q{'The second argument passed to natatime must be a code reference'}) - . q{ if @_ == 2 && ! Params::Util::_CODELIKE( $_[1] );}; + return ( + 'if (!defined($_[0]) || $_[0] !~ /^\d+$/) {', + $self->_inline_throw_error( + '"The n value passed to natatime must be an integer"', + ) . ';', + '}', + 'if (@_ == 2 && !Params::Util::_CODELIKE($_[1])) {', + $self->_inline_throw_error( + '"The second argument passed to natatime must be a code ' + . 'reference"', + ) . ';', + '}', + ); } sub _inline_return_value { - my ( $self, $slot_access ) = @_; - - return - "my \$iter = List::MoreUtils::natatime( \$_[0], \@{ ($slot_access) } );" - . "\n" - . 'if ( $_[1] ) {' . "\n" - . 'while (my @vals = $iter->()) {' . "\n" - . '$_[1]->(@vals);' . "\n" . '}' . "\n" - . '} else {' . "\n" - . 'return $iter;' . "\n" . '}'; + my $self = shift; + my ($slot_access) = @_; + + return ( + 'my $iter = List::MoreUtils::natatime($_[0], @{ (' . $slot_access . ') });', + 'if ($_[1]) {', + 'while (my @vals = $iter->()) {', + '$_[1]->(@vals);', + '}', + '}', + 'else {', + 'return $iter;', + '}', + ); } # Not called, but needed to satisfy the Reader role diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/pop.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/pop.pm index 500a448..6a91814 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/pop.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/pop.pm @@ -13,7 +13,7 @@ with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { -excludes => [ qw( _maximum_arguments _inline_capture_return_value - _inline_optimized_set_new_value + _optimized_set_new_value _return_value ) ] }; @@ -23,27 +23,33 @@ sub _maximum_arguments { 0 } sub _adds_members { 0 } sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "[ \@{ ($slot_access) } > 1 ? \@{ ($slot_access) }[ 0 .. \$#{ ($slot_access) } - 1 ] : () ]"; + return '[ @{ (' . $slot_access . ') } > 1 ' + . '? @{ (' . $slot_access . ') }[0..$#{ (' . $slot_access . ') } - 1] ' + . ': () ]'; } sub _inline_capture_return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "my \$old = ${slot_access}->[-1];"; + return 'my $old = ' . $slot_access . '->[-1];'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "pop \@{ ($slot_access) }"; + return 'pop @{ (' . $slot_access . ') }'; } sub _return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return 'return $old;'; + return '$old'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/push.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/push.pm index a98e544..3d122df 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/push.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/push.pm @@ -12,7 +12,7 @@ use Moose::Role; with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { -excludes => [ qw( - _inline_optimized_set_new_value + _optimized_set_new_value _return_value ) ] @@ -21,21 +21,24 @@ with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { sub _adds_members { 1 } sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "[ \@{ ($slot_access) }, \@_ ]"; + return '[ @{ (' . $slot_access . ') }, @_ ]'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "push \@{ ($slot_access) }, \@_"; + return 'push @{ (' . $slot_access . ') }, @_'; } sub _return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "return scalar \@{ ($slot_access) }"; + return 'scalar @{ (' . $slot_access . ') }'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/reduce.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/reduce.pm index f00d3a9..d9cb20c 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/reduce.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/reduce.pm @@ -29,16 +29,20 @@ sub _maximum_arguments { 1 } sub _inline_check_arguments { my $self = shift; - return $self->_inline_throw_error( - q{'The argument passed to reduce must be a code reference'}) - . q{ unless Params::Util::_CODELIKE( $_[0] );}; + return ( + 'if (!Params::Util::_CODELIKE($_[0])) {', + $self->_inline_throw_error( + '"The argument passed to reduce must be a code reference"', + ) . ';', + '}', + ); } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return "List::Util::reduce { \$_[0]->( \$a, \$b ) } \@{ ($slot_access) }"; + return 'List::Util::reduce { $_[0]->($a, $b) } @{ (' . $slot_access . ') }'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/set.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/set.pm index 81cc7d8..4b02f4c 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/set.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/set.pm @@ -15,8 +15,9 @@ with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { _minimum_arguments _maximum_arguments _inline_check_arguments + _inline_coerce_new_values _new_members - _inline_optimized_set_new_value + _optimized_set_new_value _return_value ) ] @@ -35,37 +36,42 @@ sub _inline_check_arguments { sub _adds_members { 1 } sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return - "( do { my \@potential = \@{ ($slot_access) }; \$potential[ \$_[0] ] = \$_[1]; \\\@potential } )"; + return '(do { ' + . 'my @potential = @{ (' . $slot_access . ') }; ' + . '$potential[$_[0]] = $_[1]; ' + . '\@potential; ' + . '})'; } # We need to override this because while @_ can be written to, we cannot write # directly to $_[1]. -around _inline_coerce_new_values => sub { - shift; +sub _inline_coerce_new_values { my $self = shift; - return q{} unless $self->associated_attribute->should_coerce; + return unless $self->associated_attribute->should_coerce; - return q{} unless $self->_tc_member_type_can_coerce; + return unless $self->_tc_member_type_can_coerce; - return '@_ = ( $_[0], $member_tc_obj->coerce( $_[1] ) );'; + return '@_ = ($_[0], $member_tc_obj->coerce($_[1]));'; }; sub _new_members { '$_[1]' } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "${slot_access}->[ \$_[0] ] = \$_[1]"; + return $slot_access . '->[$_[0]] = $_[1]'; } sub _return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "return ${slot_access}->[ \$_[0] ];"; + return $slot_access . '->[$_[0]]'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/shift.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/shift.pm index 37670b4..f1a8fcf 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/shift.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/shift.pm @@ -14,7 +14,7 @@ with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { qw( _maximum_arguments _inline_capture_return_value - _inline_optimized_set_new_value + _optimized_set_new_value _return_value ) ] @@ -25,27 +25,33 @@ sub _maximum_arguments { 0 } sub _adds_members { 0 } sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "[ \@{ ($slot_access) } > 1 ? \@{ ($slot_access) }[ 1 .. \$#{ ($slot_access) } ] : () ]"; + return '[ @{ (' . $slot_access . ') } > 1 ' + . '? @{ (' . $slot_access . ') }[1..$#{ (' . $slot_access . ') }] ' + . ': () ]'; } sub _inline_capture_return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "my \$old = ${slot_access}->[0];"; + return 'my $old = ' . $slot_access . '->[0];'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "shift \@{ ($slot_access) };"; + return 'shift @{ (' . $slot_access . ') }'; } sub _return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return 'return $old'; + return '$old'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/shuffle.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/shuffle.pm index 70b3096..cdacd6e 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/shuffle.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/shuffle.pm @@ -17,10 +17,10 @@ with 'Moose::Meta::Method::Accessor::Native::Reader' => sub _maximum_arguments { 0 } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return "List::Util::shuffle \@{ ($slot_access) }"; + return 'List::Util::shuffle @{ (' . $slot_access . ') }'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/sort.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/sort.pm index 60e6520..db109a7 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/sort.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/sort.pm @@ -25,17 +25,22 @@ sub _maximum_arguments { 1 } sub _inline_check_arguments { my $self = shift; - return $self->_inline_throw_error( - q{'The argument passed to sort must be a code reference'}) - . q{ if @_ && ! Params::Util::_CODELIKE( $_[0] );}; + return ( + 'if (@_ && !Params::Util::_CODELIKE($_[0])) {', + $self->_inline_throw_error( + '"The argument passed to sort must be a code reference"', + ) . ';', + '}', + ); } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return - "\$_[0] ? sort { \$_[0]->( \$a, \$b ) } \@{ ($slot_access) } : sort \@{ ($slot_access) }"; + return '$_[0] ' + . '? sort { $_[0]->($a, $b) } @{ (' . $slot_access . ') } ' + . ': sort @{ (' . $slot_access . ') }'; } no Moose::Role; 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 dd5f393..a11c082 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 @@ -26,21 +26,28 @@ sub _maximum_arguments { 1 } sub _inline_check_arguments { my $self = shift; - return $self->_inline_throw_error( - q{'The argument passed to sort_in_place must be a code reference'}) - . q{ if @_ && ! Params::Util::_CODELIKE( $_[0] );}; + return ( + 'if (@_ && !Params::Util::_CODELIKE($_[0])) {', + $self->_inline_throw_error( + '"The argument passed to sort_in_place must be a code ' + . 'reference"', + ) . ';', + '}', + ); } sub _adds_members { 0 } sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return - "[ \$_[0] ? sort { \$_[0]->( \$a, \$b ) } \@{ ($slot_access) } : sort \@{ ($slot_access) } ]"; + return '[ $_[0] ' + . '? sort { $_[0]->($a, $b) } @{ (' . $slot_access . ') } ' + . ': sort @{ (' . $slot_access . ') } ]'; } -sub _return_value { return q{} } +sub _return_value { return '' } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/splice.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/splice.pm index f68db7a..caccb7f 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/splice.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/splice.pm @@ -15,7 +15,7 @@ with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { _minimum_arguments _inline_process_arguments _inline_check_arguments - _inline_optimized_set_new_value + _optimized_set_new_value _return_value ) ] @@ -26,35 +26,52 @@ sub _minimum_arguments { 1 } sub _adds_members { 1 } sub _inline_process_arguments { - return 'my $idx = shift;' . "\n" . 'my $len = @_ ? shift : undef;'; + return ( + 'my $idx = shift;', + 'my $len = @_ ? shift : undef;', + ); } sub _inline_check_arguments { my $self = shift; - return - $self->_inline_check_var_is_valid_index('$idx') . "\n" - . $self->_inline_throw_error(q{'The length argument passed to splice must be an integer'}) - . ' if defined $len && $len !~ /^-?\\d+$/;'; + return ( + $self->_inline_check_var_is_valid_index('$idx'), + 'if (defined($len) && $len !~ /^-?\d+$/) {', + $self->_inline_throw_error( + '"The length argument passed to splice must be an integer"', + ) . ';', + '}', + ); } sub _potential_value { - my ( $self, $slot_access ) = @_; - - return "( do { my \@potential = \@{ ($slot_access) };" - . '@return = defined $len ? ( splice @potential, $idx, $len, @_ ) : ( splice @potential, $idx ); \\@potential } )'; + my $self = shift; + my ($slot_access) = @_; + + return '(do { ' + . 'my @potential = @{ (' . $slot_access . ') }; ' + . '@return = defined $len ' + . '? (splice @potential, $idx, $len, @_) ' + . ': (splice @potential, $idx); ' + . '\@potential;' + . '})'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "\@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) = @_; + my $self = shift; + my ($slot_access) = @_; - return 'return wantarray ? @return : $return[-1]'; + return 'wantarray ? @return : $return[-1]'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/uniq.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/uniq.pm index 9203f52..b49faf8 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/uniq.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/uniq.pm @@ -17,10 +17,10 @@ with 'Moose::Meta::Method::Accessor::Native::Reader' => sub _maximum_arguments { 0 } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return "List::MoreUtils::uniq \@{ ($slot_access) }"; + return 'List::MoreUtils::uniq @{ (' . $slot_access . ') }'; } 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 698025d..ce60a44 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/unshift.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/unshift.pm @@ -12,7 +12,7 @@ use Moose::Role; with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { -excludes => [ qw( - _inline_optimized_set_new_value + _optimized_set_new_value _return_value ) ] @@ -21,21 +21,24 @@ with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => { sub _adds_members { 1 } sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "[ \@_, \@{ ($slot_access) } ]"; + return '[ @_, @{ (' . $slot_access . ') } ]'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "unshift \@{ ($slot_access) }, \@_"; + return 'unshift @{ (' . $slot_access . ') }, @_'; } sub _return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "return scalar \@{ ($slot_access) }"; + return 'scalar @{ (' . $slot_access . ') }'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Bool/not.pm b/lib/Moose/Meta/Method/Accessor/Native/Bool/not.pm index 80198bf..f79439b 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Bool/not.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Bool/not.pm @@ -15,9 +15,10 @@ with 'Moose::Meta::Method::Accessor::Native::Reader' => sub _maximum_arguments { 0 } sub _return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "! $slot_access"; + return '!' . $slot_access; } 1; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Bool/set.pm b/lib/Moose/Meta/Method/Accessor/Native/Bool/set.pm index 6196288..03d41a6 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Bool/set.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Bool/set.pm @@ -13,7 +13,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { -excludes => [ qw( _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -22,10 +22,11 @@ sub _maximum_arguments { 0 } sub _potential_value { 1 } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access = 1"; + return $slot_access . ' = 1'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Bool/toggle.pm b/lib/Moose/Meta/Method/Accessor/Native/Bool/toggle.pm index 6c26131..dcef5cc 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Bool/toggle.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Bool/toggle.pm @@ -13,7 +13,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { -excludes => [ qw( _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -21,15 +21,17 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { sub _maximum_arguments { 0 } sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "$slot_access ? 0 : 1"; + return $slot_access . ' ? 0 : 1'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access = $slot_access ? 0 : 1"; + return $slot_access . ' = ' . $slot_access . ' ? 0 : 1'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Bool/unset.pm b/lib/Moose/Meta/Method/Accessor/Native/Bool/unset.pm index a9d64fb..8b5d0ef 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Bool/unset.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Bool/unset.pm @@ -13,7 +13,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { -excludes => [ qw( _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -22,10 +22,11 @@ sub _maximum_arguments { 0 } sub _potential_value { 0 } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access = 0"; + return $slot_access . ' = 0'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Code/execute.pm b/lib/Moose/Meta/Method/Accessor/Native/Code/execute.pm index 390c1ce..38044d3 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Code/execute.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Code/execute.pm @@ -12,9 +12,10 @@ use Moose::Role; with 'Moose::Meta::Method::Accessor::Native::Reader'; sub _return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "${slot_access}->(\@_)"; + return $slot_access . '->(@_)'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Code/execute_method.pm b/lib/Moose/Meta/Method/Accessor/Native/Code/execute_method.pm index f17bcc7..c13156b 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Code/execute_method.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Code/execute_method.pm @@ -12,9 +12,10 @@ use Moose::Role; with 'Moose::Meta::Method::Accessor::Native::Reader'; sub _return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "${slot_access}->(\$self, \@_)"; + return $slot_access . '->($self, @_)'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Collection.pm b/lib/Moose/Meta/Method/Accessor/Native/Collection.pm index 25d7997..517ff54 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Collection.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Collection.pm @@ -11,20 +11,18 @@ use Moose::Role; requires qw( _adds_members ); -around _inline_coerce_new_values => sub { - shift; +sub _inline_coerce_new_values { my $self = shift; - return q{} unless $self->associated_attribute->should_coerce; + return unless $self->associated_attribute->should_coerce; - return q{} unless $self->_tc_member_type_can_coerce; + return unless $self->_tc_member_type_can_coerce; - return - '(' - . $self->_new_members - . ') = map { $member_tc_obj->coerce($_) } ' - . $self->_new_members . ';'; -}; + return ( + '(' . $self->_new_members . ') = map { $member_tc_obj->coerce($_) }', + $self->_new_members . ';', + ); +} sub _tc_member_type_can_coerce { my $self = shift; @@ -37,43 +35,41 @@ sub _tc_member_type_can_coerce { sub _tc_member_type { my $self = shift; - for ( - my $tc = $self->associated_attribute->type_constraint; - $tc; - $tc = $tc->parent - ) { - + my $tc = $self->associated_attribute->type_constraint; + while ($tc) { return $tc->type_parameter if $tc->can('type_parameter'); + $tc = $tc->parent; } return; } -around _value_needs_copy => sub { - shift; +sub _value_needs_copy { my $self = shift; return $self->_constraint_must_be_checked && !$self->_check_new_members_only; -}; +} -around _inline_tc_code => sub { - shift; - my ( $self, $potential_value ) = @_; +sub _inline_tc_code { + my $self = shift; + my ($potential_value) = @_; - return q{} unless $self->_constraint_must_be_checked; + return unless $self->_constraint_must_be_checked; - if ( $self->_check_new_members_only ) { - return q{} unless $self->_adds_members; + if ($self->_check_new_members_only) { + return unless $self->_adds_members; - return $self->_inline_check_member_constraint( $self->_new_members ); + return $self->_inline_check_member_constraint($self->_new_members); } else { - return $self->_inline_check_coercion($potential_value) . "\n" - . $self->_inline_check_constraint($potential_value); + return ( + $self->_inline_check_coercion($potential_value), + $self->_inline_check_constraint($potential_value), + ); } -}; +} sub _check_new_members_only { my $self = shift; @@ -101,31 +97,38 @@ sub _check_new_members_only { } sub _inline_check_member_constraint { - my ( $self, $new_value ) = @_; + my $self = shift; + my ($new_value) = @_; my $attr_name = $self->associated_attribute->name; - return '$member_tc->($_) || ' - . $self->_inline_throw_error( - qq{"A new member value for '$attr_name' does not pass its type constraint because: "} - . ' . $member_tc->get_message($_)', - "data => \$_" - ) . " for $new_value;"; + return ( + 'for (' . $new_value . ') {', + 'if (!$member_tc->($_)) {', + $self->_inline_throw_error( + '"A new member value for ' . $attr_name + . ' does not pass its type constraint because: "' + . ' . $member_tc->get_message($_)', + 'data => $_', + ) . ';', + '}', + '}', + ); } -around _inline_get_old_value_for_trigger => sub { - shift; - my ( $self, $instance ) = @_; +sub _inline_get_old_value_for_trigger { + my $self = shift; + my ($instance, $old) = @_; my $attr = $self->associated_attribute; - return '' unless $attr->has_trigger; + return unless $attr->has_trigger; - return - 'my @old = ' - . $self->_inline_has($instance) . q{ ? } - . $self->_inline_copy_old_value( $self->_inline_get($instance) ) - . ": ();\n"; -}; + return ( + 'my ' . $old . ' = ' . $self->_inline_has($instance), + '? ' . $self->_inline_copy_old_value($self->_inline_get($instance)), + ': ();', + ); +} around _eval_environment => sub { my $orig = shift; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Counter/dec.pm b/lib/Moose/Meta/Method/Accessor/Native/Counter/dec.pm index 2870175..119c174 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Counter/dec.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Counter/dec.pm @@ -14,7 +14,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { qw( _minimum_arguments _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -23,15 +23,17 @@ sub _minimum_arguments {0} sub _maximum_arguments {1} sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "$slot_access - ( defined \$_[0] ? \$_[0] : 1 )"; + return $slot_access . ' - ( defined $_[0] ? $_[0] : 1 )'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access -= defined \$_[0] ? \$_[0] : 1"; + return $slot_access . ' -= defined $_[0] ? $_[0] : 1'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Counter/inc.pm b/lib/Moose/Meta/Method/Accessor/Native/Counter/inc.pm index 4db79d4..2a0b44e 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Counter/inc.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Counter/inc.pm @@ -14,7 +14,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { qw( _minimum_arguments _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -23,15 +23,17 @@ sub _minimum_arguments { 0 } sub _maximum_arguments { 1 } sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "$slot_access + ( defined \$_[0] ? \$_[0] : 1 )"; + return $slot_access . ' + ( defined $_[0] ? $_[0] : 1 )'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access += defined \$_[0] ? \$_[0] : 1"; + return $slot_access . ' += defined $_[0] ? $_[0] : 1'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Counter/reset.pm b/lib/Moose/Meta/Method/Accessor/Native/Counter/reset.pm index bf4abd5..39e9954 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Counter/reset.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Counter/reset.pm @@ -13,7 +13,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { -excludes => [ qw( _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -21,15 +21,17 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { sub _maximum_arguments { 0 } sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "\$attr->default(\$self)" + return '$attr->default($self)'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access = \$attr->default(\$self)"; + return $slot_access . ' = $attr->default($self)'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Counter/set.pm b/lib/Moose/Meta/Method/Accessor/Native/Counter/set.pm index 38f84e6..4ca9d86 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Counter/set.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Counter/set.pm @@ -14,7 +14,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { qw( _minimum_arguments _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -24,10 +24,11 @@ sub _maximum_arguments {1} sub _potential_value {'$_[0]'} -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access = \$_[0];"; + return $slot_access . ' = $_[0]'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash.pm index 814faa1..5ed7110 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash.pm @@ -10,12 +10,18 @@ our $AUTHORITY = 'cpan:STEVAN'; use Moose::Role; sub _inline_check_var_is_valid_key { - my ( $self, $var ) = @_; + my $self = shift; + my ($var) = @_; - return $self->_inline_throw_error( q{'The key passed to } - . $self->delegate_to_method - . q{ must be a defined value'} ) - . qq{ unless defined $var;}; + return ( + 'if (!defined(' . $var . ')) {', + $self->_inline_throw_error( + '"The key passed to ' + . $self->delegate_to_method + . ' must be a defined value"', + ) . ';', + '}', + ); } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/Writer.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/Writer.pm index 2602d9a..99bf3d2 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash/Writer.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/Writer.pm @@ -11,14 +11,17 @@ our $AUTHORITY = 'cpan:STEVAN'; use Moose::Role; -with 'Moose::Meta::Method::Accessor::Native::Writer', +with 'Moose::Meta::Method::Accessor::Native::Writer' => { + -excludes => ['_inline_coerce_new_values'], + }, 'Moose::Meta::Method::Accessor::Native::Hash', 'Moose::Meta::Method::Accessor::Native::Collection'; sub _new_values {'@values'} sub _inline_copy_old_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; return '{ %{(' . $slot_access . ')} }'; } diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/accessor.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/accessor.pm index 3d9b21e..b3db6d6 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash/accessor.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/accessor.pm @@ -35,43 +35,27 @@ with 'Moose::Meta::Method::Accessor::Native::Hash::set' => { sub _generate_method { my $self = shift; - my $inv = '$self'; - - my $code = 'sub {'; - $code .= "\n" . $self->_inline_pre_body(@_); - - $code .= "\n" . 'my $self = shift;'; - - $code .= "\n" . $self->_inline_curried_arguments; - - $code .= "\n" . $self->_inline_check_lazy($inv); - + my $inv = '$self'; my $slot_access = $self->_inline_get($inv); - # get - $code .= "\n" . 'if ( @_ == 1 ) {'; - - $code .= "\n" . $self->_inline_check_var_is_valid_key('$_[0]'); - - $code - .= "\n" - . 'return ' - . $self - ->Moose::Meta::Method::Accessor::Native::Hash::get::_return_value( - $slot_access) - . ';'; - - # set - $code .= "\n" . '} else {'; - - $code .= "\n" . $self->_writer_core( $inv, $slot_access ); - - $code .= "\n" . $self->_inline_post_body(@_); - - $code .= "\n}"; - $code .= "\n}"; - - return $code; + return ( + 'sub {', + $self->_inline_pre_body(@_), + 'my ' . $inv . ' = shift;', + $self->_inline_curried_arguments, + $self->_inline_check_lazy($inv), + # get + 'if (@_ == 1) {', + $self->_inline_check_var_is_valid_key('$_[0]'), + $self->Moose::Meta::Method::Accessor::Native::Hash::get::_inline_return_value($slot_access), + '}', + # set + 'else {', + $self->_writer_core($inv, $slot_access), + $self->_inline_post_body(@_), + '}', + '}', + ); } sub _minimum_arguments {1} diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/clear.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/clear.pm index 52df18f..08dc8a3 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash/clear.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/clear.pm @@ -13,7 +13,7 @@ with 'Moose::Meta::Method::Accessor::Native::Hash::Writer' => { -excludes => [ qw( _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value _return_value ) ] @@ -25,13 +25,14 @@ sub _adds_members { 0 } sub _potential_value { return '{}' } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access = {}"; + return $slot_access . ' = {}'; } -sub _return_value { return q{} } +sub _return_value { return '' } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/count.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/count.pm index 7e7d539..8b56c1d 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash/count.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/count.pm @@ -17,10 +17,10 @@ with 'Moose::Meta::Method::Accessor::Native::Reader' => sub _maximum_arguments { 0 } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return "scalar keys \%{ ($slot_access) }"; + return 'scalar keys %{ (' . $slot_access . ') }'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/defined.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/defined.pm index 877f19c..667dc21 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash/defined.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/defined.pm @@ -33,10 +33,10 @@ sub _inline_check_arguments { } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return "defined ${slot_access}->{ \$_[0] }"; + return 'defined ' . $slot_access . '->{ $_[0] }'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/delete.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/delete.pm index 5c7ae78..f530b41 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash/delete.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/delete.pm @@ -12,7 +12,7 @@ use Moose::Role; with 'Moose::Meta::Method::Accessor::Native::Hash::Writer' => { -excludes => [ qw( - _inline_optimized_set_new_value + _optimized_set_new_value _return_value ) ], @@ -21,21 +21,28 @@ with 'Moose::Meta::Method::Accessor::Native::Hash::Writer' => { sub _adds_members { 0 } sub _potential_value { - my ( $self, $slot_access ) = @_; - - return "( do { my \%potential = %{ ($slot_access) }; \@return = delete \@potential{\@_}; \\\%potential; } )"; + my $self = shift; + my ($slot_access) = @_; + + return '(do { ' + . 'my %potential = %{ (' . $slot_access . ') }; ' + . '@return = delete @potential{@_}; ' + . '\%potential; ' + . '})'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "\@return = delete \@{ ($slot_access) }{\@_}"; + return '@return = delete @{ (' . $slot_access . ') }{@_}'; } sub _return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return 'return wantarray ? @return : $return[-1];'; + return 'wantarray ? @return : $return[-1]'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/elements.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/elements.pm index 2913f65..a7beb3a 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash/elements.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/elements.pm @@ -17,10 +17,11 @@ with 'Moose::Meta::Method::Accessor::Native::Reader' => sub _maximum_arguments { 0 } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return "map { \$_, ${slot_access}->{\$_} } keys \%{ ($slot_access) }"; + return 'map { $_, ' . $slot_access . '->{$_} } ' + . 'keys %{ (' . $slot_access . ') }'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/exists.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/exists.pm index 099786a..64a1662 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash/exists.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/exists.pm @@ -33,10 +33,10 @@ sub _inline_check_arguments { } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = shift; - return "exists ${slot_access}->{ \$_[0] }"; + return 'exists ' . $slot_access . '->{ $_[0] }'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/get.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/get.pm index 03d39e7..dabb524 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash/get.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/get.pm @@ -26,16 +26,20 @@ sub _minimum_arguments { 1 } sub _inline_check_arguments { my $self = shift; - return - 'for (@_) {' . "\n" - . $self->_inline_check_var_is_valid_key('$_') . "\n" . '}'; + return ( + 'for (@_) {', + $self->_inline_check_var_is_valid_key('$_'), + '}', + ); } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return "\@_ > 1 ? \@{ ($slot_access) }{\@_} : ${slot_access}->{ \$_[0] }"; + return '@_ > 1 ' + . '? @{ (' . $slot_access . ') }{@_} ' + . ': ' . $slot_access . '->{$_[0]}'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/is_empty.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/is_empty.pm index c0cfd08..298c74d 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash/is_empty.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/is_empty.pm @@ -17,10 +17,10 @@ with 'Moose::Meta::Method::Accessor::Native::Reader' => sub _maximum_arguments { 0 } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return "scalar keys \%{ ($slot_access) } ? 0 : 1"; + return 'scalar keys %{ (' . $slot_access . ') } ? 0 : 1'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/keys.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/keys.pm index 6c16a09..206875e 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash/keys.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/keys.pm @@ -17,10 +17,10 @@ with 'Moose::Meta::Method::Accessor::Native::Reader' => sub _maximum_arguments { 0 } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return "keys \%{ ($slot_access) }"; + return 'keys %{ (' . $slot_access . ') }'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/kv.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/kv.pm index b26132d..f4381c1 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash/kv.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/kv.pm @@ -17,10 +17,11 @@ with 'Moose::Meta::Method::Accessor::Native::Reader' => sub _maximum_arguments { 0 } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return "map { [ \$_, ${slot_access}->{\$_} ] } keys \%{ ($slot_access) }"; + return 'map { [ $_, ' . $slot_access . '->{$_} ] } ' + . 'keys %{ (' . $slot_access . ') }'; } 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 e0cdbc8..720f350 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash/set.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/set.pm @@ -19,7 +19,8 @@ with 'Moose::Meta::Method::Accessor::Native::Hash::Writer' => { _maximum_arguments _inline_process_arguments _inline_check_arguments - _inline_optimized_set_new_value + _inline_coerce_new_values + _optimized_set_new_value _return_value ) ], @@ -33,68 +34,83 @@ around _inline_check_argument_count => sub { my $orig = shift; my $self = shift; - return - $self->$orig(@_) . "\n" - . $self->_inline_throw_error( - q{'You must pass an even number of arguments to set'}) - . ' if @_ % 2;'; + return ( + $self->$orig(@_), + 'if (@_ % 2) {', + $self->_inline_throw_error( + '"You must pass an even number of arguments to set"', + ) . ';', + '}', + ); }; sub _inline_process_arguments { my $self = shift; - return 'my @keys_idx = grep { ! ($_ % 2) } 0..$#_;' . "\n" - . 'my @values_idx = grep { $_ % 2 } 0..$#_;'; + return ( + 'my @keys_idx = grep { ! ($_ % 2) } 0..$#_;', + 'my @values_idx = grep { $_ % 2 } 0..$#_;', + ); } sub _inline_check_arguments { my $self = shift; - return - 'for (@keys_idx) {' . "\n" - . $self->_inline_throw_error( - q{'Hash keys passed to set must be defined'}) - . ' unless defined $_[$_];' . "\n" . '}'; + return ( + 'for (@keys_idx) {', + 'if (!defined($_[$_])) {', + $self->_inline_throw_error( + '"Hash keys passed to set must be defined"', + ) . ';', + '}', + '}', + ); } sub _adds_members { 1 } # We need to override this because while @_ can be written to, we cannot write # directly to $_[1]. -around _inline_coerce_new_values => sub { - shift; +sub _inline_coerce_new_values { my $self = shift; - return q{} unless $self->associated_attribute->should_coerce; + return unless $self->associated_attribute->should_coerce; - return q{} unless $self->_tc_member_type_can_coerce; + return unless $self->_tc_member_type_can_coerce; # Is there a simpler way to do this? - return 'my $iter = List::MoreUtils::natatime 2, @_;' - . '@_ = ();' - . 'while ( my ( $key, $val ) = $iter->() ) {' - . 'push @_, $key, $member_tc_obj->coerce($val);' - . '}'; + return ( + 'my $iter = List::MoreUtils::natatime(2, @_);', + '@_ = ();', + 'while (my ($key, $val) = $iter->()) {', + 'push @_, $key, $member_tc_obj->coerce($val);', + '}', + ); }; sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "{ %{ ($slot_access) }, \@_ }"; + return '{ %{ (' . $slot_access . ') }, @_ }'; } sub _new_members { '@_[ @values_idx ]' } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "\@{ ($slot_access) }{ \@_[ \@keys_idx] } = \@_[ \@values_idx ]"; + return '@{ (' . $slot_access . ') }{ @_[@keys_idx] } = @_[@values_idx]'; } sub _return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "return wantarray ? \@{ ($slot_access) }{ \@_[ \@keys_idx ] } : ${slot_access}->{ \$_[ \$keys_idx[0] ] };"; + return 'wantarray ' + . '? @{ (' . $slot_access . ') }{ @_[@keys_idx] } ' + . ': ' . $slot_access . '->{ $_[$keys_idx[0]] }'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/values.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/values.pm index ab234e5..7ac72e2 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash/values.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/values.pm @@ -17,10 +17,10 @@ with 'Moose::Meta::Method::Accessor::Native::Reader' => sub _maximum_arguments { 0 } sub _return_value { - my $self = shift; - my $slot_access = shift; + my $self = shift; + my ($slot_access) = @_; - return "values \%{ ($slot_access) }"; + return 'values %{ (' . $slot_access . ') }'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/abs.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/abs.pm index 0784948..aa99138 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Number/abs.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Number/abs.pm @@ -13,7 +13,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { -excludes => [ qw( _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -21,15 +21,17 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { sub _maximum_arguments {0} sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "abs($slot_access)"; + return 'abs(' . $slot_access . ')'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access = abs($slot_access)"; + return $slot_access . ' = abs(' . $slot_access . ')'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/add.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/add.pm index 6ce3613..e581af4 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Number/add.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Number/add.pm @@ -14,7 +14,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { qw( _minimum_arguments _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -24,15 +24,17 @@ sub _minimum_arguments {1} sub _maximum_arguments {1} sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "$slot_access + \$_[0]"; + return $slot_access . ' + $_[0]'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access += \$_[0]"; + return $slot_access . ' += $_[0]'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/div.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/div.pm index a42125f..005ff6f 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Number/div.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Number/div.pm @@ -14,7 +14,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { qw( _minimum_arguments _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -24,15 +24,17 @@ sub _minimum_arguments {1} sub _maximum_arguments {1} sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "$slot_access / \$_[0]"; + return $slot_access . ' / $_[0]'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access /= \$_[0]"; + return $slot_access . ' /= $_[0]'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/mod.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/mod.pm index fbbb434..affa5c6 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Number/mod.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Number/mod.pm @@ -14,7 +14,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { qw( _minimum_arguments _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -24,15 +24,17 @@ sub _minimum_arguments {1} sub _maximum_arguments {1} sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "$slot_access % \$_[0]"; + return $slot_access . ' % $_[0]'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access %= \$_[0]"; + return $slot_access . ' %= $_[0]'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/mul.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/mul.pm index b6d59a5..b06e8d9 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Number/mul.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Number/mul.pm @@ -14,7 +14,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { qw( _minimum_arguments _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -24,15 +24,17 @@ sub _minimum_arguments {1} sub _maximum_arguments {1} sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "$slot_access * \$_[0]"; + return $slot_access . ' * $_[0]'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access *= \$_[0]"; + return $slot_access . ' *= $_[0]'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/set.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/set.pm index 263e1e6..31990b6 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Number/set.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Number/set.pm @@ -14,7 +14,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { qw( _minimum_arguments _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -24,10 +24,11 @@ sub _maximum_arguments {1} sub _potential_value {'$_[0]'} -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access = \$_[0]"; + return $slot_access . ' = $_[0]'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/sub.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/sub.pm index e75ca69..57d0dff 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Number/sub.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Number/sub.pm @@ -14,7 +14,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { qw( _minimum_arguments _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -24,15 +24,17 @@ sub _minimum_arguments {1} sub _maximum_arguments {1} sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "$slot_access - \$_[0]"; + return $slot_access . ' - $_[0]'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access -= \$_[0]"; + return $slot_access . ' -= $_[0]'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Reader.pm b/lib/Moose/Meta/Method/Accessor/Native/Reader.pm index 957207c..5909153 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Reader.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Reader.pm @@ -16,49 +16,36 @@ requires '_return_value'; sub _generate_method { my $self = shift; - my $inv = '$self'; - - my $code = 'sub {'; - $code .= "\n" . $self->_inline_pre_body(@_); - - $code .= "\n" . 'my $self = shift;'; - - $code .= "\n" . $self->_inline_curried_arguments; - + my $inv = '$self'; my $slot_access = $self->_inline_get($inv); - $code .= "\n" . $self->_reader_core( $inv, $slot_access, @_ ); - - $code .= "\n}"; - - return $code; + return ( + 'sub {', + $self->_inline_pre_body(@_), + 'my ' . $inv . ' = shift;', + $self->_inline_curried_arguments, + $self->_reader_core($inv, $slot_access, @_), + '}', + ); } sub _reader_core { - my ( $self, $inv, $slot_access, @extra ) = @_; - - my $code = q{}; - - $code .= "\n" . $self->_inline_check_argument_count; - $code .= "\n" . $self->_inline_process_arguments( $inv, $slot_access ); - $code .= "\n" . $self->_inline_check_arguments; - - $code .= "\n" . $self->_inline_check_lazy($inv); - $code .= "\n" . $self->_inline_post_body(@extra); - $code .= "\n" . $self->_inline_return_value($slot_access); - - return $code; + my $self = shift; + my ($inv, $slot_access, @extra) = @_; + + return ( + $self->_inline_check_argument_count, + $self->_inline_process_arguments($inv, $slot_access), + $self->_inline_check_arguments, + $self->_inline_check_lazy($inv), + $self->_inline_post_body(@extra), + $self->_inline_return_value($slot_access), + ); } -sub _inline_process_arguments {q{}} +sub _inline_process_arguments { return } -sub _inline_check_arguments {q{}} - -sub _inline_return_value { - my ( $self, $slot_access ) = @_; - - 'return ' . $self->_return_value($slot_access) . ';'; -} +sub _inline_check_arguments { return } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/append.pm b/lib/Moose/Meta/Method/Accessor/Native/String/append.pm index 51df675..37ee88b 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/append.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/append.pm @@ -14,7 +14,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { qw( _minimum_arguments _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -24,15 +24,17 @@ sub _minimum_arguments { 1 } sub _maximum_arguments { 1 } sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "( $slot_access . \$_[0] )"; + return '( ' . $slot_access . ' . $_[0] )'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access .= \$_[0]"; + return $slot_access . ' .= $_[0]'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/chomp.pm b/lib/Moose/Meta/Method/Accessor/Native/String/chomp.pm index 1dc33cd..3c56056 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/chomp.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/chomp.pm @@ -13,7 +13,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { -excludes => [ qw( _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value _return_value ) ] @@ -22,19 +22,26 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { sub _maximum_arguments {0} sub _potential_value { - my ( $self, $slot_access ) = @_; - - return "( do { my \$val = $slot_access; \@return = chomp \$val; \$val } )"; + my $self = shift; + my ($slot_access) = @_; + + return '(do { ' + . 'my $val = ' . $slot_access . '; ' + . '@return = chomp $val; ' + . '$val ' + . '})'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "\@return = chomp $slot_access"; + return '@return = chomp ' . $slot_access; } sub _return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; return '$return[0]'; } diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/chop.pm b/lib/Moose/Meta/Method/Accessor/Native/String/chop.pm index f8784d0..2f610d5 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/chop.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/chop.pm @@ -13,7 +13,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { -excludes => [ qw( _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value _return_value ) ] @@ -22,19 +22,26 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { sub _maximum_arguments { 0 } sub _potential_value { - my ( $self, $slot_access ) = @_; - - return "( do { my \$val = $slot_access; \@return = chop \$val; \$val } )"; + my $self = shift; + my ($slot_access) = @_; + + return '(do { ' + . 'my $val = ' . $slot_access . '; ' + . '@return = chop $val; ' + . '$val; ' + . '})'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "\@return = chop $slot_access"; + return '@return = chop ' . $slot_access; } sub _return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; return '$return[0]'; } diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/clear.pm b/lib/Moose/Meta/Method/Accessor/Native/String/clear.pm index f47c788..c2cb974 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/clear.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/clear.pm @@ -13,7 +13,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { -excludes => [ qw( _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -21,15 +21,17 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { sub _maximum_arguments { 0 } sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "q{}"; + return '""'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access = q{}"; + return $slot_access . ' = ""'; } 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 595ade8..8c4649b 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/inc.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/inc.pm @@ -13,7 +13,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { -excludes => [ qw( _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -21,15 +21,21 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { sub _maximum_arguments { 0 } sub _potential_value { - my ( $self, $slot_access ) = @_; - - return "( do { my \$val = $slot_access; \$val++; \$val } )"; + my $self = shift; + my ($slot_access) = @_; + + return '(do { ' + . 'my $val = ' . $slot_access . '; ' + . '$val++; ' + . '$val; ' + . '})'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "${slot_access}++"; + return $slot_access . '++'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/length.pm b/lib/Moose/Meta/Method/Accessor/Native/String/length.pm index c0749a9..3fc8686 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/length.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/length.pm @@ -15,9 +15,10 @@ with 'Moose::Meta::Method::Accessor::Native::Reader' => sub _maximum_arguments { 0 } sub _return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "length $slot_access"; + return 'length ' . $slot_access; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/match.pm b/lib/Moose/Meta/Method/Accessor/Native/String/match.pm index 989776e..950b7df 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/match.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/match.pm @@ -29,15 +29,21 @@ sub _maximum_arguments { 1 } sub _inline_check_arguments { my $self = shift; - return $self->_inline_throw_error( - q{'The argument passed to match must be a string or regexp reference'} - ) . q{ unless Moose::Util::_STRINGLIKE0( $_[0] ) || Params::Util::_REGEX( $_[0] );}; + return ( + 'if (!Moose::Util::_STRINGLIKE0($_[0]) && !Params::Util::_REGEX($_[0])) {', + $self->_inline_throw_error( + '"The argument passed to match must be a string or regexp ' + . 'reference"', + ) . ';', + '}', + ); } sub _return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "$slot_access =~ \$_[0]"; + return $slot_access . ' =~ $_[0]'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/prepend.pm b/lib/Moose/Meta/Method/Accessor/Native/String/prepend.pm index 5f77cdd..a431f55 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/prepend.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/prepend.pm @@ -14,7 +14,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { qw( _minimum_arguments _maximum_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -24,15 +24,17 @@ sub _minimum_arguments { 1 } sub _maximum_arguments { 1 } sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "( \$_[0] . $slot_access )"; + return '( $_[0] . ' . $slot_access . ' )'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "$slot_access = \$_[0] . $slot_access"; + return $slot_access . ' = $_[0] . ' . $slot_access; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/replace.pm b/lib/Moose/Meta/Method/Accessor/Native/String/replace.pm index 4098509..ce2bae6 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/replace.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/replace.pm @@ -18,7 +18,7 @@ with 'Moose::Meta::Method::Accessor::Native::Writer' => { _minimum_arguments _maximum_arguments _inline_check_arguments - _inline_optimized_set_new_value + _optimized_set_new_value ) ] }; @@ -30,25 +30,44 @@ sub _maximum_arguments { 2 } sub _inline_check_arguments { my $self = shift; - return $self->_inline_throw_error( - q{'The first argument passed to replace must be a string or regexp reference'} - ) - . q{ unless Moose::Util::_STRINGLIKE0( $_[0] ) || Params::Util::_REGEX( $_[0] );} - . $self->_inline_throw_error( - q{'The second argument passed to replace must be a string or code reference'} - ) . q{ unless Moose::Util::_STRINGLIKE0( $_[1] ) || Params::Util::_CODELIKE( $_[1] );}; + return ( + 'if (!Moose::Util::_STRINGLIKE0($_[0]) && !Params::Util::_REGEX($_[0])) {', + $self->_inline_throw_error( + '"The first argument passed to replace must be a string or ' + . 'regexp reference"' + ) . ';', + '}', + 'if (!Moose::Util::_STRINGLIKE0($_[1]) && !Params::Util::_CODELIKE($_[1])) {', + $self->_inline_throw_error( + '"The second argument passed to replace must be a string or ' + . 'code reference"' + ) . ';', + '}', + ); } sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return "( do { my \$val = $slot_access; ref \$_[1] ? \$val =~ s/\$_[0]/\$_[1]->()/e : \$val =~ s/\$_[0]/\$_[1]/; \$val } )"; + return '(do { ' + . 'my $val = ' . $slot_access . '; ' + . 'ref $_[1] ' + . '? $val =~ s/$_[0]/$_[1]->()/e ' + . ': $val =~ s/$_[0]/$_[1]/; ' + . '$val; ' + . '})'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "if ( ref \$_[1] ) { $slot_access =~ s/\$_[0]/\$_[1]->()/e; } else { $slot_access =~ s/\$_[0]/\$_[1]/; }"; + return '(do { ' + . 'ref $_[1] ' + . '? ' . $slot_access . ' =~ s/$_[0]/$_[1]->()/e ' + . ': ' . $slot_access . ' =~ s/$_[0]/$_[1]/; ' + . '})'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/substr.pm b/lib/Moose/Meta/Method/Accessor/Native/String/substr.pm index e4f1c13..b3a0a2f 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/substr.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/substr.pm @@ -30,7 +30,7 @@ with 'Moose::Meta::Method::Accessor::Native::Reader' => { _maximum_arguments _inline_process_arguments _inline_check_arguments - _inline_optimized_set_new_value + _optimized_set_new_value _return_value ) ] @@ -39,90 +39,98 @@ with 'Moose::Meta::Method::Accessor::Native::Reader' => { sub _generate_method { my $self = shift; - my $inv = '$self'; - + my $inv = '$self'; my $slot_access = $self->_inline_get($inv); - my $code = 'sub {'; - - $code .= "\n" . $self->_inline_pre_body(@_); - $code .= "\n" . 'my $self = shift;'; - - $code .= "\n" . $self->_inline_curried_arguments; - - $code .= "\n" . 'if ( @_ == 1 || @_ == 2 ) {'; - - $code .= $self->_reader_core( $inv, $slot_access ); - - $code .= "\n" . '} elsif ( @_ == 3 ) {'; - - $code .= $self->_writer_core( $inv, $slot_access ); - - $code .= "\n" . $self->_inline_post_body(@_); - - $code .= "\n" . '} else {'; - - $code .= "\n" . $self->_inline_check_argument_count; - - $code .= "\n" . '}'; - $code .= "\n" . '}'; - - return $code; + return ( + 'sub {', + $self->_inline_pre_body(@_), + 'my ' . $inv . ' = shift;', + $self->_inline_curried_arguments, + 'if (@_ == 1 || @_ == 2) {', + $self->_reader_core($inv, $slot_access), + '}', + 'elsif (@_ == 3) {', + $self->_writer_core($inv, $slot_access), + $self->_inline_post_body(@_), + '}', + 'else {', + $self->_inline_check_argument_count, + '}', + '}', + ); } sub _minimum_arguments {1} sub _maximum_arguments {3} sub _inline_process_arguments { - my ( $self, $inv, $slot_access ) = @_; + my $self = shift; + my ($inv, $slot_access) = @_; - return - 'my $offset = shift;' . "\n" - . "my \$length = \@_ ? shift : length $slot_access;" . "\n" - . 'my $replacement = shift;'; + return ( + 'my $offset = shift;', + 'my $length = @_ ? shift : length ' . $slot_access . ';', + 'my $replacement = shift;', + ); } sub _inline_check_arguments { - my ( $self, $for_writer ) = @_; - - my $code - = $self->_inline_throw_error( - q{'The first argument passed to substr must be an integer'}) - . q{ unless $offset =~ /^-?\\d+$/;} . "\n" - . $self->_inline_throw_error( - q{'The second argument passed to substr must be an integer'}) - . q{ unless $length =~ /^-?\\d+$/;}; + my $self = shift; + my ($for_writer) = @_; + + my @code = ( + 'if ($offset !~ /^-?\d+$/) {', + $self->_inline_throw_error( + '"The first argument passed to substr must be an integer"' + ) . ';', + '}', + 'if ($length !~ /^-?\d+$/) {', + $self->_inline_throw_error( + '"The second argument passed to substr must be an integer"' + ) . ';', + '}', + ); if ($for_writer) { - $code - .= "\n" - . $self->_inline_throw_error( - q{'The third argument passed to substr must be a string'}) - . q{ unless Moose::Util::_STRINGLIKE0($replacement);}; + push @code, ( + 'if (!Moose::Util::_STRINGLIKE0($replacement)) {', + $self->_inline_throw_error( + '"The third argument passed to substr must be a string"' + ) . ';', + '}', + ); } - return $code; + return @code; } sub _potential_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; - return - "( do { my \$potential = $slot_access; \@return = substr \$potential, \$offset, \$length, \$replacement; \$potential; } )"; + return '(do { ' + . 'my $potential = ' . $slot_access . '; ' + . '@return = substr $potential, $offset, $length, $replacement; ' + . '$potential; ' + . '})'; } -sub _inline_optimized_set_new_value { - my ( $self, $inv, $new, $slot_access ) = @_; +sub _optimized_set_new_value { + my $self = shift; + my ($inv, $new, $slot_access) = @_; - return "\@return = substr $slot_access, \$offset, \$length, \$replacement"; + return '@return = substr ' . $slot_access . ', ' + . '$offset, $length, $replacement'; } sub _return_value { - my ( $self, $slot_access, $for_writer ) = @_; + my $self = shift; + my ($slot_access, $for_writer) = @_; return '$return[0]' if $for_writer; - return "substr $slot_access, \$offset, \$length"; + return 'substr ' . $slot_access . ', $offset, $length'; } no Moose::Role; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Writer.pm b/lib/Moose/Meta/Method/Accessor/Native/Writer.pm index 85af7b2..de127f9 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Writer.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Writer.pm @@ -18,73 +18,60 @@ requires '_potential_value'; sub _generate_method { my $self = shift; - my $inv = '$self'; - + my $inv = '$self'; my $slot_access = $self->_inline_get($inv); - my $code = 'sub {'; - - $code .= "\n" . $self->_inline_pre_body(@_); - - $code .= "\n" . 'my $self = shift;'; - - $code .= "\n" . $self->_inline_curried_arguments; - - $code .= $self->_writer_core( $inv, $slot_access ); - - $code .= "\n" . $self->_inline_post_body(@_); - - $code .= "\n}"; - - return $code; + return ( + 'sub {', + $self->_inline_pre_body(@_), + 'my ' . $inv . ' = shift;', + $self->_inline_curried_arguments, + $self->_writer_core($inv, $slot_access), + $self->_inline_post_body(@_), + '}', + ); } sub _writer_core { - my ( $self, $inv, $slot_access ) = @_; - - my $code = q{}; - - $code .= "\n" . $self->_inline_check_argument_count; - $code .= "\n" . $self->_inline_process_arguments( $inv, $slot_access ); - $code .= "\n" . $self->_inline_check_arguments('for writer'); + my $self = shift; + my ($inv, $slot_access) = @_; - $code .= "\n" . $self->_inline_check_lazy($inv); + my $potential = $self->_potential_value($slot_access); + my $old = '@old'; - my $potential_value = $self->_potential_value($slot_access); + my @code; + push @code, ( + $self->_inline_check_argument_count, + $self->_inline_process_arguments($inv, $slot_access), + $self->_inline_check_arguments('for writer'), + $self->_inline_check_lazy($inv), + ); - if ( $self->_return_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;'; + push @code, 'my @return;' } - # This is only needed by collections. - $code .= "\n" . $self->_inline_coerce_new_values; - $code .= "\n" . $self->_inline_copy_native_value( \$potential_value ); - $code .= "\n" - . $self->_inline_tc_code( - $potential_value - ); - - $code .= "\n" . $self->_inline_get_old_value_for_trigger($inv); - $code .= "\n" . $self->_inline_capture_return_value($slot_access); - $code .= "\n" - . $self->_inline_set_new_value( - $inv, - $potential_value, - $slot_access, - ) . ';'; - $code .= "\n" . $self->_inline_trigger( $inv, $slot_access, '@old' ); - $code .= "\n" . $self->_return_value( $slot_access, 'for writer' ); - - return $code; + push @code, ( + $self->_inline_coerce_new_values, + $self->_inline_copy_native_value(\$potential), + $self->_inline_tc_code($potential), + $self->_inline_get_old_value_for_trigger($inv, $old), + $self->_inline_capture_return_value($slot_access), + $self->_inline_set_new_value($inv, $potential, $slot_access), + $self->_inline_trigger($inv, $slot_access, $old), + $self->_inline_return_value($slot_access, 'for writer'), + ); + + return @code; } -sub _inline_process_arguments {q{}} +sub _inline_process_arguments { return } -sub _inline_check_arguments {q{}} +sub _inline_check_arguments { return } -sub _inline_coerce_new_values {q{}} +sub _inline_coerce_new_values { return } sub _value_needs_copy { my $self = shift; @@ -103,59 +90,62 @@ sub _constraint_must_be_checked { } sub _is_root_type { - my ($self, $type) = @_; + my $self = shift; + my ($type) = @_; - my $name = $type->name(); + my $name = $type->name; return any { $name eq $_ } @{ $self->root_types }; } sub _inline_copy_native_value { - my ( $self, $potential_ref ) = @_; + my $self = shift; + my ($potential_ref) = @_; - return q{} unless $self->_value_needs_copy; + return unless $self->_value_needs_copy; - my $code = "my \$potential = ${$potential_ref};"; + my $code = 'my $potential = ' . ${$potential_ref} . ';'; ${$potential_ref} = '$potential'; - return $code; + return ($code); } -sub _inline_tc_code { - my ( $self, $potential_value ) = @_; +around _inline_tc_code => sub { + my $orig = shift; + my $self = shift; + my ($value, $for_lazy) = @_; - return q{} unless $self->_constraint_must_be_checked; + return unless $for_lazy || $self->_constraint_must_be_checked; - return $self->_inline_check_coercion($potential_value) . "\n" - . $self->_inline_check_constraint($potential_value); -} + return $self->$orig(@_); +}; sub _inline_check_coercion { - my ( $self, $value ) = @_; + my $self = shift; + my ($value) = @_; my $attr = $self->associated_attribute; - - return q{} - unless $attr->should_coerce - && $attr->type_constraint->has_coercion; + return unless $attr->should_coerce && $attr->type_constraint->has_coercion; # We want to break the aliasing in @_ in case the coercion tries to make a # destructive change to an array member. - return "$value = \$type_constraint_obj->coerce($value);"; + return ($value . ' = $type_constraint_obj->coerce(' . $value . ');'); } -override _inline_check_constraint => sub { - my ( $self, $value, $for_lazy ) = @_; +around _inline_check_constraint => sub { + my $orig = shift; + my $self = shift; + my ($value, $for_lazy) = @_; - return q{} unless $for_lazy || $self->_constraint_must_be_checked; + return unless $for_lazy || $self->_constraint_must_be_checked; - return super(); + return $self->$orig(@_); }; -sub _inline_capture_return_value { return q{} } +sub _inline_capture_return_value { return } -sub _inline_set_new_value { +sub _set_new_value { my $self = shift; return $self->_inline_store(@_) @@ -163,7 +153,12 @@ sub _inline_set_new_value { || !$self->_slot_access_can_be_inlined || !$self->_inline_get_is_lvalue; - return $self->_inline_optimized_set_new_value(@_); + return $self->_optimized_set_new_value(@_); +} + +sub _inline_set_new_value { + my $self = shift; + return $self->_set_new_value(@_) . ';'; } sub _inline_get_is_lvalue { @@ -172,14 +167,15 @@ sub _inline_get_is_lvalue { return $self->associated_attribute->associated_class->instance_metaclass->inline_get_is_lvalue; } -sub _inline_optimized_set_new_value { +sub _optimized_set_new_value { my $self = shift; return $self->_inline_store(@_); } sub _return_value { - my ( $self, $slot_access ) = @_; + my $self = shift; + my ($slot_access) = @_; return $slot_access; }