1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
use Test::More tests=>5;
use strict;
use warnings;
use Iterator::Simple qw(:all);
my $itr;
{
$itr = [1,2,'STR',4, 52, 'foo', 12, 3];
eval {
no warnings 'numeric';
$itr = ifilter $itr, sub{
if($_ eq 'STR') {
return iter([ split '', $_]);
}
elsif($_ >= 1 and $_ < 11) {
return $_ + 10;
}
elsif($_ >= 11 and $_ <= 21) {
return; #skip
}
elsif($_ > 21) {
return $_ % 10;
}
else {
return $_
}
};
};
is($@, '', 'ifilter creation');
ok is_iterator($itr), 'ifilter confirm';
is_deeply list($itr) => [11,12,'S','T','R',14, 2, 'foo', 13], 'ifilter result';
}
{
$itr = [ 1, 2, 3 ];
eval {
$itr = ifilter $itr, bless({ code => sub { $_ + 1 } } => 'ifiltertest');
};
is($@, '', 'ifilter creation');
is_deeply list($itr) => [ 2, 3, 4 ], 'ifilter result';
}
package ifiltertest;
use overload (
'&{}' => '_call',
fallback => 1,
);
sub _call { shift->{code} }
|