| 1 | use Test::More qw(no_plan); |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | |
| 5 | use autobox::Core; |
| 6 | |
| 7 | my %hash = ( foo => 1, bar => 2, baz => 3 ); |
| 8 | |
| 9 | my @glued; |
| 10 | %hash->each( sub { push @glued, $_[0] . $_[1] } ); |
| 11 | |
| 12 | is_deeply [ sort @glued ], [ qw(bar2 baz3 foo1) ]; |
| 13 | |
| 14 | my @array = values %hash; |
| 15 | |
| 16 | my @added; |
| 17 | @array->each( sub { push @added, $_[0] + 1 } ); |
| 18 | |
| 19 | is_deeply [ sort @added ], [ qw(2 3 4) ]; |
| 20 | |
| 21 | # Ensure each() always iterates through the whole hash |
| 22 | { |
| 23 | my %want = (foo => 23, bar => 42, baz => 99, biff => 66); |
| 24 | |
| 25 | # Call each once on %want to start the iterator attached to %want |
| 26 | my($k,$v) = each %want; |
| 27 | |
| 28 | my %have; |
| 29 | %want->each( sub { $have{$_[0]} = $_[1] } ); |
| 30 | |
| 31 | is_deeply \%have, \%want; |
| 32 | } |