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