File Coverage

blib/lib/ArrayDataRole/Source/LinesInDATA.pm
Criterion Covered Total %
statement 39 46 84.7
branch 14 14 100.0
condition n/a
subroutine 10 14 71.4
pod 3 10 30.0
total 66 84 78.5


line stmt bran cond sub pod time code
1             package ArrayDataRole::Source::LinesInDATA;
2              
3 2     2   1396 use strict;
  2         5  
  2         71  
4 2     2   13 use Role::Tiny;
  2         7  
  2         13  
5 2     2   350 use Role::Tiny::With;
  2         5  
  2         235  
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   15 no strict 'refs'; ## no critic: TestingAndDebugging::RequireUseStrict
  2         4  
  2         1045  
15              
16 2     2 0 229 my $class = shift;
17              
18 2         5 my $fh = \*{"$class\::DATA"};
  2         12  
19 2         8 my $fhpos_data_begin = tell $fh;
20              
21 2         16 bless {
22             fh => $fh,
23             fhpos_data_begin => $fhpos_data_begin,
24             pos => 0, # iterator
25             }, $class;
26             }
27              
28             sub get_next_item {
29 22     22 0 116 my $self = shift;
30 22 100       111 die "StopIteration" if eof($self->{fh});
31 21         59 chomp(my $elem = readline($self->{fh}));
32 21         36 $self->{pos}++;
33 21         63 $elem;
34             }
35              
36             sub has_next_item {
37 18     18 0 109 my $self = shift;
38 18         127 !eof($self->{fh});
39             }
40              
41             sub get_iterator_pos {
42 0     0 0 0 my $self = shift;
43 0         0 $self->{pos};
44             }
45              
46             sub reset_iterator {
47 5     5 0 1911 my $self = shift;
48 5         81 seek $self->{fh}, $self->{fhpos_data_begin}, 0;
49 5         59 $self->{pos} = 0;
50             }
51              
52             sub get_item_at_pos {
53 3     3 0 51 my ($self, $pos) = @_;
54 3 100       14 $self->reset_iterator if $self->{pos} > $pos;
55 3         7 while (1) {
56 7 100       17 die "Out of range" unless $self->has_next_item;
57 6         17 my $item = $self->get_next_item;
58 6 100       23 return $item if $self->{pos} > $pos;
59             }
60             }
61              
62             sub has_item_at_pos {
63 3     3 0 8 my ($self, $pos) = @_;
64 3 100       17 return 1 if $self->{pos} > $pos;
65 2         4 while (1) {
66 5 100       12 return 0 unless $self->has_next_item;
67 4         15 $self->get_next_item;
68 4 100       16 return 1 if $self->{pos} > $pos;
69             }
70             }
71              
72             sub fh {
73 0     0 1   my $self = shift;
74 0           $self->{fh};
75             }
76              
77             sub fh_min_offset {
78 0     0 1   my $self = shift;
79 0           $self->{fhpos_data_begin};
80             }
81              
82 0     0 1   sub fh_max_offset { undef }
83              
84             1;
85             # ABSTRACT: Role to access array data from DATA section, one line per element
86              
87             __END__