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-06-23'; # DATE
5             our $DIST = 'WordList'; # DIST
6             our $VERSION = '0.7.10'; # VERSION
7              
8 1     1   475 use Role::Tiny;
  1         2  
  1         4  
9              
10             requires 'each_word';
11              
12             sub first_word {
13 2     2 1 4338 my $self = shift;
14              
15 2         11 $self->reset_iterator;
16 2         5 $self->next_word;
17             }
18              
19             sub next_word {
20 10     10 1 16 my $self = shift;
21              
22 10 100       24 unless ($self->{_all_words}) {
23 2         3 my @wordlist;
24 2     4   10 $self->each_word(sub { push @wordlist, $_[0] });
  4         10  
25 2         7 $self->{_all_words} = \@wordlist;
26             }
27 10 50       20 $self->{_iterator_idx} = 0 unless defined $self->{_iterator_idx};
28              
29 10 100       13 return undef if $self->{_iterator_idx} > $#{ $self->{_all_words} };
  10         24  
30 8         26 $self->{_all_words}[ $self->{_iterator_idx}++ ];
31             }
32              
33             sub reset_iterator {
34 4     4 1 5 my $self = shift;
35              
36 4         8 $self->{_iterator_idx} = 0;
37             }
38              
39             1;
40             # ABSTRACT: Provide first_word(), next_word(), reset_iterator(); relies on each_word()
41              
42             __END__