line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Range::Iterator; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $DATE = '2019-04-23'; # DATE |
4
|
|
|
|
|
|
|
our $VERSION = '0.002'; # VERSION |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
171616
|
use strict; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
45
|
|
7
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
5
|
use Scalar::Util qw(looks_like_number); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
315
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
12
|
7
|
|
|
7
|
1
|
3828
|
my $class = shift; |
13
|
7
|
|
|
|
|
16
|
my ($start, $end, $step) = @_; |
14
|
7
|
|
100
|
|
|
35
|
$step //= 1; |
15
|
|
|
|
|
|
|
|
16
|
7
|
|
|
|
|
28
|
my $self = { |
17
|
|
|
|
|
|
|
start => $start, |
18
|
|
|
|
|
|
|
end => $end, |
19
|
|
|
|
|
|
|
step => $step, |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
_ended => 0, |
22
|
|
|
|
|
|
|
_cur => $start, |
23
|
|
|
|
|
|
|
}; |
24
|
|
|
|
|
|
|
|
25
|
7
|
100
|
66
|
|
|
39
|
if (looks_like_number($start) && looks_like_number($end)) { |
26
|
4
|
|
|
|
|
9
|
$self->{_num} = 1; |
27
|
4
|
100
|
|
|
|
10
|
$self->{_ended}++ if $start > $end; |
28
|
|
|
|
|
|
|
} else { |
29
|
3
|
50
|
|
|
|
11
|
die "Cannot specify step != 1 for non-numeric range" if $step != 1; |
30
|
3
|
100
|
|
|
|
11
|
$self->{_ended}++ if $start gt $end; |
31
|
|
|
|
|
|
|
} |
32
|
7
|
|
|
|
|
29
|
bless $self, $class; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub next { |
36
|
43
|
|
|
43
|
1
|
147
|
my $self = shift; |
37
|
|
|
|
|
|
|
|
38
|
43
|
100
|
|
|
|
73
|
if ($self->{_num}) { |
39
|
13
|
100
|
|
|
|
27
|
$self->{_ended}++ if $self->{_cur} > $self->{end}; |
40
|
13
|
100
|
|
|
|
27
|
return undef if $self->{_ended}; |
41
|
9
|
|
|
|
|
13
|
my $old = $self->{_cur}; |
42
|
9
|
|
|
|
|
13
|
$self->{_cur} += $self->{step}; |
43
|
9
|
|
|
|
|
20
|
return $old; |
44
|
|
|
|
|
|
|
} else { |
45
|
30
|
100
|
|
|
|
53
|
return undef if $self->{_ended}; |
46
|
27
|
100
|
|
|
|
46
|
$self->{_ended}++ if $self->{_cur} ge $self->{end}; |
47
|
27
|
|
|
|
|
58
|
$self->{_cur}++; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
1; |
52
|
|
|
|
|
|
|
# ABSTRACT: Generate an iterator object for range |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
__END__ |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=pod |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=encoding UTF-8 |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head1 NAME |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Range::Iterator - Generate an iterator object for range |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 VERSION |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
This document describes version 0.002 of Range::Iterator (from Perl distribution Range-Iterator), released on 2019-04-23. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 SYNOPSIS |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
use Range::Iterator; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
my $iter = Range::Iterator->new(1, 10); |
73
|
|
|
|
|
|
|
while (defined(my $val = $iter->next)) { ... } # 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
You can add step: |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
my $iter = Range::Iterator->new(1, 10, 2); # 1, 3, 5, 7, 9 |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
You can use alphanumeric strings too since C<++> has some extra builtin magic |
80
|
|
|
|
|
|
|
(see L<perlop>): |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
$iter = Range::Iterator->new("zx", "aab"); # zx, zy, zz, aaa, aab |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Infinite list: |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
$iter = Range::Iterator->new(1, Inf); # 1, 2, 3, ... |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 DESCRIPTION |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
This module offers an object-based iterator for range. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=for Pod::Coverage .+ |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 METHODS |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head2 new |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Usage: |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
$obj = Range::Iterator->new($start, $end [ , $step ]) |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head2 next |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Usage: |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my $val = $obj->next |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Return the next value, or undef if there is no more values. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 HOMEPAGE |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Please visit the project's homepage at L<https://metacpan.org/release/Range-Iterator>. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 SOURCE |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Source repository is at L<https://github.com/perlancar/perl-Range-Iterator>. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 BUGS |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Range-Iterator> |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
When submitting a bug or request, please include a test-file or a |
123
|
|
|
|
|
|
|
patch to an existing test-file that illustrates the bug or desired |
124
|
|
|
|
|
|
|
feature. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 SEE ALSO |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
L<Range::Iter> |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
L<Range::ArrayIter> |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
L<Array::Iterator> & L<Array::Iter> offer iterators for list/array. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 AUTHOR |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
perlancar <perlancar@cpan.org> |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
This software is copyright (c) 2019 by perlancar@cpan.org. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
143
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=cut |