From: Mike Whitaker Date: Wed, 15 Oct 2008 20:24:22 +0000 (+0000) Subject: Add test for modify-in-place usage of ->map, as well as an example in the pod docs. X-Git-Tag: 0_09~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=09a0196c32a63189d212a905146e0271861255b7;p=gitmo%2FMoose-Autobox.git Add test for modify-in-place usage of ->map, as well as an example in the pod docs. --- diff --git a/lib/Moose/Autobox/Array.pm b/lib/Moose/Autobox/Array.pm index 848fbfd..6234435 100644 --- a/lib/Moose/Autobox/Array.pm +++ b/lib/Moose/Autobox/Array.pm @@ -232,6 +232,24 @@ This is a role to describe operations on the Array type. =item B +Note that, in both the above, $_ is in scope within the code block, as well as +being passed as $_[0]. As per CORE::map and CORE::grep, $_ is an alias to +the list value, so can be used to to modify the list, viz: + + use Moose::Autobox; + + my $foo = [1, 2, 3]; + $foo->map( sub {$_++} ); + print $foo->dump; + +yields + + $VAR1 = [ + 2, + 3, + 4 + ]; + =item B =item B diff --git a/t/001_basic.t b/t/001_basic.t index 7ee4b93..4c18e6f 100644 --- a/t/001_basic.t +++ b/t/001_basic.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 68; +use Test::More tests => 69; BEGIN { use_ok('Moose::Autobox'); @@ -75,6 +75,10 @@ $a->map(sub { $_ + 2 }), is_deeply($a, [ 4, 2, 6, 78, 101, 2, 3 ], '... original value is unchanged'); +my $b = [1, 2, 3]; +$b->map(sub { $_++ } ); +is_deeply($b, [ 2, 3, 4 ], '... original value is changed'); + is_deeply( $a->reverse(), [ 3, 2, 101, 78, 6, 2, 4 ],