From: Tomas Doran Date: Thu, 11 Feb 2010 02:17:32 +0000 (+0000) Subject: Add each_value to scalar. Also add tests for the behaviour of setting both $_ and... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fscalar_each_value;p=gitmo%2FMoose-Autobox.git Add each_value to scalar. Also add tests for the behaviour of setting both $_ and $_[0] as this tripped me up initially --- diff --git a/Changes b/Changes index 7dc388a..8a5254a 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,6 @@ Revision history for Perl extension Moose::Autobox + - add each_value to Scalar (t0m) - add first and last to Scalar and Array (t0m) 0.10 diff --git a/lib/Moose/Autobox/Scalar.pm b/lib/Moose/Autobox/Scalar.pm index 6ebb9fe..d7555a9 100644 --- a/lib/Moose/Autobox/Scalar.pm +++ b/lib/Moose/Autobox/Scalar.pm @@ -11,6 +11,8 @@ sub first { $_[0] } sub last { $_[0] } sub print { CORE::print $_[0] } sub say { CORE::print $_[0], "\n" } +sub each_value { $_[1]->($_ = $_[0]) } + 1; __END__ @@ -55,6 +57,10 @@ As per flatten. As per flatten. +=item B + +As per flatten. + =head1 BUGS All complex software has bugs lurking in it, and this module is no diff --git a/t/010_each.t b/t/010_each.t index 5c1f02d..19b4b2a 100644 --- a/t/010_each.t +++ b/t/010_each.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 11; +use Test::More tests => 17; require_ok('Moose::Autobox'); @@ -20,13 +20,17 @@ use Moose::Autobox; is_deeply($href, \%hash, "sanity check to start"); - my @keys; + my (@keys, @keys2); $href->each_key(sub { push @keys, $_ }); - is_deeply([ sort @keys ], [ sort keys %hash ], "keys found via each_key"); + is_deeply([ sort @keys ], [ sort keys %hash ], 'keys found via each_key in $_'); + $href->each_key(sub { push @keys2, $_[0] }); + is_deeply([ sort @keys2 ], [ sort keys %hash ], 'keys found via each_key in $_[0]'); - my @values; + my (@values, @values2); $href->each_value(sub { push @values, $_ }); - is_deeply([ sort @values ], [ sort values %hash ], "values via each_values"); + is_deeply([ sort @values ], [ sort values %hash ], 'values via each_values in $_'); + $href->each_value(sub { push @values2, $_[0] }); + is_deeply([ sort @values2 ], [ sort values %hash ], 'values via each_values in $_[0]'); $href->each_value(sub { $_++ }); is($href->{a}, 2, "we can ++ values directly"); @@ -42,13 +46,17 @@ use Moose::Autobox; is_deeply($aref, \@array, "sanity check to start"); - my @keys; + my (@keys, @keys2); $aref->each_key(sub { push @keys, $_ }); - is_deeply([ @keys ], [ 0, 1, 2 ], "keys found via each_key"); + is_deeply([ @keys ], [ 0, 1, 2 ], 'keys found via each_key in $_'); + $aref->each_key(sub { push @keys2, $_[0] }); + is_deeply([ @keys2 ], [ 0, 1, 2 ], 'keys found via each_key in $_[0]'); - my @values; + my (@values, @values2); $aref->each_value(sub { push @values, $_ }); - is_deeply([ @values ], [ @array ], "values via each_values"); + is_deeply([ @values ], [ @array ], 'values via each_values in $_'); + $aref->each_value(sub { push @values2, $_[0] }); + is_deeply([ @values2 ], [ @array ], 'values via each_values in $_[0]'); $aref->each_value(sub { $_ = uc }); is($aref->[0], 'FOO', "we can alter values directly"); @@ -56,3 +64,16 @@ use Moose::Autobox; $aref->each_key(sub { $_ = $_ + 1 }); ok(! $aref->[3], "we cannot alter keys directly"); } + +{ + my $string = 'foo'; + my @values; + $string->each_value(sub { push @values, $_ }); + is_deeply( [@values], [$string], 'each_values for scalar using $_' ); +} +{ + my $string = 'foo'; + my @values; + $string->each_value(sub { push @values, $_[0] }); + is_deeply( [@values], [$string], 'each_values for scalar using $_[0]' ); +}