line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package ArrayDataRole::Source::LinesInFile; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
479
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
5
|
use Role::Tiny; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
5
|
1
|
|
|
1
|
|
146
|
use Role::Tiny::With; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
551
|
|
6
|
|
|
|
|
|
|
with 'ArrayDataRole::Spec::Basic'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
9
|
|
|
|
|
|
|
our $DATE = '2021-12-01'; # DATE |
10
|
|
|
|
|
|
|
our $DIST = 'ArrayDataRoles-Standard'; # DIST |
11
|
|
|
|
|
|
|
our $VERSION = '0.007'; # VERSION |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
2
|
|
|
2
|
1
|
2513
|
my ($class, %args) = @_; |
15
|
|
|
|
|
|
|
|
16
|
2
|
|
|
|
|
5
|
my $fh; |
17
|
2
|
100
|
|
|
|
8
|
unless (defined($fh = delete $args{fh})) { |
18
|
1
|
|
|
|
|
3
|
my $filename = delete $args{filename}; |
19
|
1
|
50
|
|
|
|
5
|
defined $filename or die "Please specify fh or filename"; |
20
|
1
|
|
|
|
|
39
|
open $fh, "<", $filename; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
bless { |
24
|
2
|
|
|
|
|
16
|
fh => $fh, |
25
|
|
|
|
|
|
|
pos => 0, # iterator |
26
|
|
|
|
|
|
|
}, $class; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub get_next_item { |
30
|
22
|
|
|
22
|
0
|
112
|
my $self = shift; |
31
|
22
|
100
|
|
|
|
157
|
die "StopIteration" if eof($self->{fh}); |
32
|
20
|
|
|
|
|
65
|
chomp(my $elem = readline($self->{fh})); |
33
|
20
|
|
|
|
|
28
|
$self->{pos}++; |
34
|
20
|
|
|
|
|
59
|
$elem; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub has_next_item { |
38
|
16
|
|
|
16
|
0
|
26
|
my $self = shift; |
39
|
16
|
|
|
|
|
179
|
!eof($self->{fh}); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub get_iterator_pos { |
43
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
44
|
0
|
|
|
|
|
0
|
$self->{pos}; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub reset_iterator { |
48
|
8
|
|
|
8
|
0
|
1814
|
my $self = shift; |
49
|
8
|
|
|
|
|
84
|
seek $self->{fh}, 0, 0; |
50
|
8
|
|
|
|
|
32
|
$self->{pos} = 0; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub get_item_at_pos { |
54
|
6
|
|
|
6
|
0
|
73
|
my ($self, $pos) = @_; |
55
|
6
|
100
|
|
|
|
25
|
$self->reset_iterator if $self->{pos} > $pos; |
56
|
6
|
|
|
|
|
12
|
while (1) { |
57
|
10
|
100
|
|
|
|
23
|
die "Out of range" unless $self->has_next_item; |
58
|
8
|
|
|
|
|
21
|
my $item = $self->get_next_item; |
59
|
8
|
100
|
|
|
|
32
|
return $item if $self->{pos} > $pos; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub has_item_at_pos { |
64
|
6
|
|
|
6
|
0
|
16
|
my ($self, $pos) = @_; |
65
|
6
|
100
|
|
|
|
23
|
return 1 if $self->{pos} > $pos; |
66
|
4
|
|
|
|
|
7
|
while (1) { |
67
|
6
|
100
|
|
|
|
13
|
return 0 unless $self->has_next_item; |
68
|
4
|
|
|
|
|
12
|
$self->get_next_item; |
69
|
4
|
100
|
|
|
|
13
|
return 1 if $self->{pos} > $pos; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub fh { |
74
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
75
|
0
|
|
|
|
|
|
$self->{fh}; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
1; |
79
|
|
|
|
|
|
|
# ABSTRACT: Role to access array data from a file/filehandle, one line per element |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
__END__ |