File Coverage

blib/lib/WordListRole/FirstNextResetFromEach.pm
Criterion Covered Total %
statement 18 18 100.0
branch 5 6 83.3
condition n/a
subroutine 5 5 100.0
pod 3 3 100.0
total 31 32 96.8


line stmt bran cond sub pod time code
1             package WordListRole::FirstNextResetFromEach;
2              
3             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4             our $DATE = '2021-02-23'; # DATE
5             our $DIST = 'WordList'; # DIST
6             our $VERSION = '0.7.8'; # VERSION
7              
8 1     1   570 use Role::Tiny;
  1         2  
  1         6  
9              
10             requires 'each_word';
11              
12             sub first_word {
13 2     2 1 5145 my $self = shift;
14              
15 2         13 $self->reset_iterator;
16 2         6 $self->next_word;
17             }
18              
19             sub next_word {
20 10     10 1 19 my $self = shift;
21              
22 10 100       31 unless ($self->{_all_words}) {
23 2         4 my @wordlist;
24 2     4   11 $self->each_word(sub { push @wordlist, $_[0] });
  4         11  
25 2         10 $self->{_all_words} = \@wordlist;
26             }
27 10 50       23 $self->{_iterator_idx} = 0 unless defined $self->{_iterator_idx};
28              
29 10 100       17 return undef if $self->{_iterator_idx} > $#{ $self->{_all_words} };
  10         30  
30 8         36 $self->{_all_words}[ $self->{_iterator_idx}++ ];
31             }
32              
33             sub reset_iterator {
34 4     4 1 8 my $self = shift;
35              
36 4         10 $self->{_iterator_idx} = 0;
37             }
38              
39             1;
40             # ABSTRACT: Provide first_word(), next_word(), reset_iterator(); relies on each_word()
41              
42             __END__