File Coverage

blib/lib/Range/Iterator.pm
Criterion Covered Total %
statement 26 26 100.0
branch 17 18 94.4
condition 4 5 80.0
subroutine 4 4 100.0
pod 2 2 100.0
total 53 55 96.3


line stmt bran cond sub pod time code
1             package Range::Iterator;
2              
3             our $DATE = '2019-04-17'; # DATE
4             our $VERSION = '0.001'; # VERSION
5              
6 1     1   55421 use strict;
  1         11  
  1         26  
7 1     1   5 use warnings;
  1         1  
  1         261  
8              
9             my $re_num = qr/\A[+-]?[0-9]+(\.[0-9]+)?\z/;
10              
11             sub new {
12 7     7 1 2941 my $class = shift;
13 7         16 my ($start, $end, $step) = @_;
14 7   100     27 $step //= 1;
15              
16 7         25 my $self = {
17             start => $start,
18             end => $end,
19             step => $step,
20              
21             _ended => 0,
22             _cur => $start,
23             };
24              
25 7 100 66     76 if ($start =~ $re_num && $end =~ $re_num) {
26 4         9 $self->{_num} = 1;
27 4 100       9 $self->{_ended}++ if $start > $end;
28             } else {
29 3 50       9 die "Cannot specify step != 1 for non-numeric range" if $step != 1;
30 3 100       7 $self->{_ended}++ if $start gt $end;
31             }
32 7         26 bless $self, $class;
33             }
34              
35             sub next {
36 43     43 1 127 my $self = shift;
37              
38 43 100       63 if ($self->{_num}) {
39 13 100       22 $self->{_ended}++ if $self->{_cur} > $self->{end};
40 13 100       24 return undef if $self->{_ended};
41 9         12 my $old = $self->{_cur};
42 9         11 $self->{_cur} += $self->{step};
43 9         13 return $old;
44             } else {
45 30 100       41 return undef if $self->{_ended};
46 27 100       41 $self->{_ended}++ if $self->{_cur} ge $self->{end};
47 27         50 $self->{_cur}++;
48             }
49             }
50              
51             1;
52             # ABSTRACT: Generate an iterator object for range
53              
54             __END__