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