| 1 | package TableDataRole::Source::CSVInFile; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 13 | ▶ | use 5.010001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 13 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 13 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 13 | ▶ | use Role::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 13 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||
| 10 | 13 | our $DATE = '2024-05-14'; # DATE | |||||||||||||
| 11 | 13 | our $DIST = 'TableDataRoles-Standard'; # DIST | |||||||||||||
| 12 | 13 | our $VERSION = '0.025'; # VERSION | |||||||||||||
| 13 | |||||||||||||||
| 14 | 13 | with 'TableDataRole::Spec::Basic'; | |||||||||||||
| 15 | |||||||||||||||
| 16 | 10 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.6
Pod
documented
| |||||||||||||||
| 17 | 10 | require Text::CSV_XS; | |||||||||||||
| 18 | |||||||||||||||
| 19 | 10 | my ($class, %args) = @_; | |||||||||||||
| 20 | |||||||||||||||
| 21 | 10 | my $fh; | |||||||||||||
| 22 | 10 | ▶ | if (defined(my $filename = delete $args{filename})) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 23 | 3 | ▶ | open $fh, "<", $filename | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 24 | or die "Can't open file '$filename': $!"; | ||||||||||||||
| 25 | } elsif (defined($fh = delete $args{filehandle})) { | ||||||||||||||
| 26 | } else { | ||||||||||||||
| 27 | 0 | die "Please specify 'filename' or 'filehandle'"; | |||||||||||||
| 28 | } | ||||||||||||||
| 29 | 10 | ▶ | die "Unknown argument(s): ". join(", ", sort keys %args) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | if keys %args; | ||||||||||||||
| 31 | |||||||||||||||
| 32 | 10 | my $csv_parser = Text::CSV_XS->new({binary=>1, auto_diag=>9, diag_verbose=>1}); | |||||||||||||
| 33 | |||||||||||||||
| 34 | 10 | my $fhpos_data_begin = tell $fh; | |||||||||||||
| 35 | 10 | ▶ | my $columns = $csv_parser->getline($fh) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | or die "Can't read columns from first row of CSV file"; | ||||||||||||||
| 37 | 10 | my $fhpos_datarow_begin = tell $fh; | |||||||||||||
| 38 | |||||||||||||||
| 39 | 10 | bless { | |||||||||||||
| 40 | fh => $fh, | ||||||||||||||
| 41 | fhpos_data_begin => $fhpos_data_begin, | ||||||||||||||
| 42 | fhpos_datarow_begin => $fhpos_datarow_begin, | ||||||||||||||
| 43 | csv_parser => $csv_parser, | ||||||||||||||
| 44 | columns => $columns, | ||||||||||||||
| 45 | pos => 0, # iterator | ||||||||||||||
| 46 | }, $class; | ||||||||||||||
| 47 | } | ||||||||||||||
| 48 | |||||||||||||||
| 49 | 3 | ▶ | sub as_csv { | ||||||||||||
|
Subroutine
as_csv: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 50 | 3 | my $self = shift; | |||||||||||||
| 51 | |||||||||||||||
| 52 | 3 | my $fh = $self->{fh}; | |||||||||||||
| 53 | 3 | my $oldpos = tell $fh; | |||||||||||||
| 54 | 3 | seek $fh, $self->{fhpos_data_begin}, 0; | |||||||||||||
| 55 | 3 | $self->{pos} = 0; | |||||||||||||
| 56 | 3 | local $/; | |||||||||||||
| 57 | 3 | scalar <$fh>; | |||||||||||||
| 58 | } | ||||||||||||||
| 59 | |||||||||||||||
| 60 | 3 | ▶ | sub get_column_count { | ||||||||||||
|
Subroutine
get_column_count: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 61 | 3 | my $self = shift; | |||||||||||||
| 62 | |||||||||||||||
| 63 | 3 | scalar @{ $self->{columns} }; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 109 | ▶ | sub get_column_names { | ||||||||||||
|
Subroutine
get_column_names: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 67 | 109 | my $self = shift; | |||||||||||||
| 68 | 109 | ▶ | wantarray ? @{ $self->{columns} } : $self->{columns}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | 463 | ▶ | sub has_next_item { | ||||||||||||
|
Subroutine
has_next_item: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 72 | 463 | my $self = shift; | |||||||||||||
| 73 | 463 | my $fh = $self->{fh}; | |||||||||||||
| 74 | 463 | !eof($fh); | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | |||||||||||||||
| 77 | 447 | ▶ | sub get_next_item { | ||||||||||||
|
Subroutine
get_next_item: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||
| 78 | 447 | my $self = shift; | |||||||||||||
| 79 | 447 | my $fh = $self->{fh}; | |||||||||||||
| 80 | 447 | ▶ | die "StopIteration" if eof($fh); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | 447 | my $row = $self->{csv_parser}->getline($fh); | |||||||||||||
| 82 | 447 | $self->{pos}++; | |||||||||||||
| 83 | 447 | $row; | |||||||||||||
| 84 | } | ||||||||||||||
| 85 | |||||||||||||||
| 86 | 4 | ▶ | sub get_next_row_hashref { | ||||||||||||
|
Subroutine
get_next_row_hashref: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||
| 87 | 4 | my $self = shift; | |||||||||||||
| 88 | 4 | my $fh = $self->{fh}; | |||||||||||||
| 89 | 4 | ▶ | die "StopIteration" if eof($fh); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 90 | 4 | my $row = $self->{csv_parser}->getline($fh); | |||||||||||||
| 91 | 4 | $self->{pos}++; | |||||||||||||
| 92 | 4 | +{ map {($self->{columns}[$_] => $row->[$_])} 0..$#{$self->{columns}} }; | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | |||||||||||||||
| 95 | 0 | ▶ | sub get_iterator_pos { | ||||||||||||
|
Subroutine
get_iterator_pos: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 96 | 0 | my $self = shift; | |||||||||||||
| 97 | 0 | $self->{pos}; | |||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | 20 | ▶ | sub reset_iterator { | ||||||||||||
|
Subroutine
reset_iterator: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 101 | 20 | my $self = shift; | |||||||||||||
| 102 | 20 | my $fh = $self->{fh}; | |||||||||||||
| 103 | 20 | seek $fh, $self->{fhpos_datarow_begin}, 0; | |||||||||||||
| 104 | 20 | $self->{pos} = 0; | |||||||||||||
| 105 | } | ||||||||||||||
| 106 | |||||||||||||||
| 107 | 10 | ▶ | sub DESTROY { | ||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||
| 108 | 10 | my $self = shift; | |||||||||||||
| 109 | 10 | my $fh = $self->{fh}; | |||||||||||||
| 110 | 10 | seek $fh, $self->{fhpos_data_begin}, 0; | |||||||||||||
| 111 | } | ||||||||||||||
| 112 | |||||||||||||||
| 113 | 13 | 1; | |||||||||||||
| 114 | # ABSTRACT: Role to access table data from CSV in a file/filehandle | ||||||||||||||
| 115 | |||||||||||||||
| 116 | __END__ | ||||||||||||||