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