line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Range::Iter; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
4
|
|
|
|
|
|
|
our $DATE = '2021-07-17'; # DATE |
5
|
|
|
|
|
|
|
our $DIST = 'Range-Iter'; # DIST |
6
|
|
|
|
|
|
|
our $VERSION = '0.003'; # VERSION |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
70093
|
use strict; |
|
1
|
|
|
|
|
13
|
|
|
1
|
|
|
|
|
32
|
|
9
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
5
|
use Scalar::Util qw(looks_like_number); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
68
|
|
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
6
|
use Exporter qw(import); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
228
|
|
14
|
|
|
|
|
|
|
our @EXPORT_OK = qw(range_iter); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub range_iter($$;$) { |
17
|
7
|
|
|
7
|
1
|
3399
|
my ($start, $end, $step) = @_; |
18
|
7
|
|
100
|
|
|
65
|
$step //= 1; |
19
|
|
|
|
|
|
|
|
20
|
7
|
|
|
|
|
11
|
my $value = $start; |
21
|
7
|
|
|
|
|
13
|
my $ended; |
22
|
|
|
|
|
|
|
|
23
|
7
|
100
|
66
|
|
|
36
|
if (looks_like_number($start) && looks_like_number($end)) { |
24
|
|
|
|
|
|
|
# numeric version |
25
|
4
|
100
|
|
|
|
10
|
$ended++ if $value > $end; |
26
|
|
|
|
|
|
|
sub { |
27
|
13
|
100
|
|
13
|
|
46
|
$ended++ if $value > $end; |
28
|
13
|
100
|
|
|
|
24
|
return undef if $ended; |
29
|
9
|
|
|
|
|
13
|
my $old = $value; |
30
|
9
|
|
|
|
|
11
|
$value+=$step; |
31
|
9
|
|
|
|
|
15
|
return $old; |
32
|
4
|
|
|
|
|
30
|
}; |
33
|
|
|
|
|
|
|
} else { |
34
|
3
|
50
|
|
|
|
9
|
die "Cannot specify step != 1 for non-numeric range" if $step != 1; |
35
|
3
|
100
|
|
|
|
9
|
$ended++ if $value gt $end; |
36
|
|
|
|
|
|
|
sub { |
37
|
30
|
100
|
|
30
|
|
99
|
return undef if $ended; |
38
|
27
|
100
|
|
|
|
42
|
$ended++ if $value ge $end; |
39
|
27
|
|
|
|
|
51
|
$value++; |
40
|
3
|
|
|
|
|
20
|
}; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
1; |
45
|
|
|
|
|
|
|
# ABSTRACT: Generate a coderef iterator for range |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
__END__ |