line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package TablesRole::Source::CSVDATA; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
4
|
|
|
|
|
|
|
our $DATE = '2020-11-10'; # DATE |
5
|
|
|
|
|
|
|
our $DIST = 'TablesRoles-Standard'; # DIST |
6
|
|
|
|
|
|
|
our $VERSION = '0.006'; # VERSION |
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
1513
|
use Role::Tiny; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
17
|
|
9
|
3
|
|
|
3
|
|
472
|
use Role::Tiny::With; |
|
3
|
|
|
|
|
14
|
|
|
3
|
|
|
|
|
162
|
|
10
|
|
|
|
|
|
|
with 'TablesRole::Spec::Basic'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
3
|
|
|
3
|
|
23
|
no strict 'refs'; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
1604
|
|
14
|
3
|
|
|
3
|
0
|
2896
|
require Text::CSV_XS; |
15
|
|
|
|
|
|
|
|
16
|
3
|
|
|
|
|
56173
|
my $class = shift; |
17
|
|
|
|
|
|
|
|
18
|
3
|
|
|
|
|
10
|
my $fh = \*{"$class\::DATA"}; |
|
3
|
|
|
|
|
22
|
|
19
|
3
|
|
|
|
|
12
|
my $fhpos_data_begin = tell $fh; |
20
|
|
|
|
|
|
|
|
21
|
3
|
|
|
|
|
20
|
my $csv_parser = Text::CSV_XS->new({binary=>1}); |
22
|
|
|
|
|
|
|
|
23
|
3
|
50
|
|
|
|
570
|
my $columns = $csv_parser->getline($fh) |
24
|
|
|
|
|
|
|
or die "Can't read columns from first row of CSV"; |
25
|
3
|
|
|
|
|
119
|
my $fhpos_datarow_begin = tell $fh; |
26
|
|
|
|
|
|
|
|
27
|
3
|
|
|
|
|
33
|
bless { |
28
|
|
|
|
|
|
|
fh => $fh, |
29
|
|
|
|
|
|
|
fhpos_data_begin => $fhpos_data_begin, |
30
|
|
|
|
|
|
|
fhpos_datarow_begin => $fhpos_datarow_begin, |
31
|
|
|
|
|
|
|
csv_parser => $csv_parser, |
32
|
|
|
|
|
|
|
columns => $columns, |
33
|
|
|
|
|
|
|
i => 0, # iterator |
34
|
|
|
|
|
|
|
}, $class; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub as_csv { |
38
|
1
|
|
|
1
|
0
|
8
|
my $self = shift; |
39
|
|
|
|
|
|
|
|
40
|
1
|
|
|
|
|
5
|
my $fh = $self->{fh}; |
41
|
1
|
|
|
|
|
3
|
my $oldpos = tell $fh; |
42
|
1
|
|
|
|
|
13
|
seek $fh, $self->{fhpos_data_begin}, 0; |
43
|
1
|
|
|
|
|
24
|
$self->{i} = -1; |
44
|
1
|
|
|
|
|
5
|
local $/; |
45
|
1
|
|
|
|
|
31
|
scalar <$fh>; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub get_column_count { |
49
|
1
|
|
|
1
|
0
|
3
|
my $self = shift; |
50
|
|
|
|
|
|
|
|
51
|
1
|
|
|
|
|
1
|
scalar @{ $self->{columns} }; |
|
1
|
|
|
|
|
6
|
|
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub get_column_names { |
55
|
1
|
|
|
1
|
0
|
3
|
my $self = shift; |
56
|
1
|
50
|
|
|
|
5
|
wantarray ? @{ $self->{columns} } : $self->{columns}; |
|
1
|
|
|
|
|
9
|
|
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub get_row_arrayref { |
60
|
45
|
|
|
45
|
0
|
74
|
my $self = shift; |
61
|
45
|
|
|
|
|
79
|
my $fh = $self->{fh}; |
62
|
45
|
|
|
|
|
986
|
my $row = $self->{csv_parser}->getline($fh); |
63
|
45
|
100
|
|
|
|
1414
|
return unless $row; |
64
|
38
|
|
|
|
|
66
|
$self->{i}++; |
65
|
38
|
|
|
|
|
99
|
$row; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub get_row_count { |
69
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
70
|
|
|
|
|
|
|
|
71
|
1
|
|
|
|
|
4
|
1 while my $row = $self->get_row_arrayref; |
72
|
1
|
|
|
|
|
5
|
$self->{i}; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub get_row_hashref { |
76
|
20
|
|
|
20
|
0
|
36
|
my $self = shift; |
77
|
20
|
|
|
|
|
41
|
my $row_arrayref = $self->get_row_arrayref; |
78
|
20
|
100
|
|
|
|
50
|
return unless $row_arrayref; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# convert to hashref |
81
|
17
|
|
|
|
|
31
|
my $row_hashref = {}; |
82
|
17
|
|
|
|
|
28
|
my $columns = $self->{columns}; |
83
|
17
|
|
|
|
|
37
|
for my $i (0 .. $#{$columns}) { |
|
17
|
|
|
|
|
46
|
|
84
|
51
|
|
|
|
|
128
|
$row_hashref->{ $columns->[$i] } = $row_arrayref->[$i]; |
85
|
|
|
|
|
|
|
} |
86
|
17
|
|
|
|
|
62
|
$row_hashref; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub reset_iterator { |
90
|
8
|
|
|
8
|
0
|
18
|
my $self = shift; |
91
|
8
|
|
|
|
|
21
|
my $fh = $self->{fh}; |
92
|
8
|
|
|
|
|
83
|
seek $fh, $self->{fhpos_datarow_begin}, 0; |
93
|
8
|
|
|
|
|
34
|
$self->{i} = 0; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
1; |
97
|
|
|
|
|
|
|
# ABSTRACT: Role to access table data from CSV in DATA section |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
__END__ |