TableDataRole/Source/CSVInFiles.pm

filter: Statementstmt 90.8% 89 / 98 Branchbran 57.1% 16 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.3% 12 / 13 Podpod 22.2% 2 / 9 totaltotal 80.4% 119 / 148 CC 18 SCAR 16.3
CC
18
Cov
83%
new
10.4
get_next_item
5.3
has_next_item
4.0
CRAP 5.1
« TableDataRole/Munge/GroupRows.pm ↑ Summary TableDataRole/Source/AOH.pm »
1 package TableDataRole::Source::CSVInFiles;
2  
3 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 3 our $DATE = '2024-05-14'; # DATE
11 3 our $DIST = 'TableDataRoles-Standard'; # DIST
12 3 our $VERSION = '0.025'; # VERSION
13  
14 3 with 'TableDataRole::Spec::Basic';
15  
16 1 ▶sub new {
Subroutine new: called CC=9 SCAR=23.4
Pod documented
17 1 require Text::CSV_XS;
18  
19 1 my ($class, %args) = @_;
20  
21 1 my $fhs = [];
22 1 ▶ if (defined(my $filenames = delete $args{filenames})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $filenames = delete $args{'filenames'})) { } 1 0 1
elsif (defined($fhs = delete $args{'filehandles'})) { } 0 0 0
23 1 for my $filename (@$filenames) {
24 3 ▶ open my $fh, "<", $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or die "Can't open file '${filename}': $!" 3 0 3
25 or die "Can't open file '$filename': $!";
26 3 push @$fhs, $fh;
27 }
28 } elsif (defined($fhs = delete $args{filehandles})) {
29 } else {
30 0 die "Please specify 'filenames' or 'filehandles'";
31 }
32 1 ▶ @$fhs or die "Please supply at least one filename/filehandle";
Branch: 1 branch
BranchTrueFalseTotal
@$fhs or die "Please supply at least one filename/filehandle" 1 0 1
33 1 ▶ die "Unknown argument(s): ". join(", ", sort keys %args)
Branch: 1 branch
BranchTrueFalseTotal
if keys %args 0 1 1
34 if keys %args;
35  
36 1 my $csv_parser = Text::CSV_XS->new({binary=>1});
37  
38 1 my $files = [];
39 1 my $columns;
40 1 for my $fh (@$fhs) {
41 3 my $fhpos_data_begin = tell $fh;
42 3 ▶ $columns = $csv_parser->getline($fh)
Branch: 1 branch
BranchTrueFalseTotal
$columns = $csv_parser->getline($fh) or die "Can't read columns from first row of CSV file" 3 0 3
43 or die "Can't read columns from first row of CSV file";
44 3 my $fhpos_datarow_begin = tell $fh;
45 3 push @$files, {
46 fh => $fh,
47 fhpos_data_begin => $fhpos_data_begin,
48 fhpos_datarow_begin => $fhpos_datarow_begin,
49 };
50 }
51 bless {
52 1 files => $files,
53 csv_parser => $csv_parser,
54 columns => $columns,
55 file_pos => 0, # which file are we at
56 pos => 0, # iterator
57 }, $class;
58 }
59  
60 1 ▶sub as_csv {
Subroutine as_csv: called CC=3 SCAR=11.0
Pod documented
61 1 my $self = shift;
62  
63 1 my $res = "";
64 1 for my $i (0 .. $#{$self->{files}}) {
65 3 my $file = $self->{files}[$i];
66 3 my $fh = $file->{fh};
67 3 my $oldpos = tell $fh;
68 3 ▶ seek $fh, ($i ? $file->{fhpos_datarow_begin} : $file->{fhpos_data_begin}), 0;
Branch: 1 branch
BranchTrueFalseTotal
$i ? : 2 1 3
69 3 local $/;
70 3 $res .= scalar <$fh>;
71 }
72 1 $self->{pos} = 0;
73 1 $self->{file_pos} = 0;
74 1 $res;
75 }
76  
77 1 ▶sub get_column_count {
Subroutine get_column_count: called CC=1 SCAR=0.0
Pod undocumented
78 1 my $self = shift;
79  
80 1 scalar @{ $self->{columns} };
81 }
82  
83 1 ▶sub get_column_names {
Subroutine get_column_names: called CC=2 SCAR=7.1
Pod undocumented
84 1 my $self = shift;
85 1 ▶ wantarray ? @{ $self->{columns} } : $self->{columns};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
86 }
87  
88 16 ▶sub has_next_item {
Subroutine has_next_item: called CC=4 SCAR=13.9
Pod undocumented
89 16 my $self = shift;
90 16 my $files = $self->{files};
91 16 my $seek = 0;
92 16 while (1) {
93 18 my $file = $self->{files}[$self->{file_pos}];
94 18 my $fh = $file->{fh};
95 18 ▶ if ($seek) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seek) 2 16 18
96 2 seek $fh, $file->{fhpos_datarow_begin}, 0;
97 2 $seek = 0;
98 }
99 18 ▶ return 1 unless eof($fh);
Branch: 1 branch
BranchTrueFalseTotal
unless eof $fh 15 3 18
100 3 ▶ return 0 if $self->{file_pos} >= $#{$self->{files}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"file_pos"} >= $#{$$self{"files"};} 1 2 3
101 2 $self->{file_pos}++;
102 2 $seek++;
103 }
104 }
105  
106 19 ▶sub get_next_item {
Subroutine get_next_item: called CC=4 SCAR=16.7
Pod undocumented
107 19 my $self = shift;
108 19 my $files = $self->{files};
109 19 my $seek = 0;
110 19 while (1) {
111 19 my $file = $self->{files}[$self->{file_pos}];
112 19 my $fh = $file->{fh};
113 19 ▶ if ($seek) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seek) 0 19 19
114 0 seek $fh, $file->{fhpos_datarow_begin}, 0;
115 0 $seek = 0;
116 }
117 19 ▶ unless (eof($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 19 0 19
118 19 my $row = $self->{csv_parser}->getline($fh);
119 19 $self->{pos}++;
120 19 return $row;
121 }
122 0 ▶ die "StopIteration" if $self->{file_pos} >= $#{$self->{files}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"file_pos"} >= $#{$$self{"files"};} 0 0 0
123 0 $self->{file_pos}++;
124 0 $seek++;
125 }
126 }
127  
128 2 ▶sub get_next_row_hashref {
Subroutine get_next_row_hashref: called CC=1 SCAR=0.0
Pod undocumented
129 2 my $self = shift;
130 2 my $row = $self->get_next_item;
131 2 +{ map {($self->{columns}[$_] => $row->[$_])} 0..$#{$self->{columns}} };
132 }
133  
134 0 ▶sub get_iterator_pos {
Subroutine get_iterator_pos: not called CC=1 SCAR=6.9
Pod undocumented
135 0 my $self = shift;
136 0 $self->{pos};
137 }
138  
139 3 ▶sub reset_iterator {
Subroutine reset_iterator: called CC=1 SCAR=0.0
Pod undocumented
140 3 my $self = shift;
141 3 $self->{file_pos} = 0;
142 3 my $fh = $self->{files}[0]{fh};
143 3 seek $fh, $self->{files}[0]{fhpos_datarow_begin}, 0;
144 3 $self->{pos} = 0;
145 }
146  
147 3 1;
148 # ABSTRACT: Role to access table data from CSV in a set of files/filehandles
149  
150 __END__
« TableDataRole/Munge/GroupRows.pm ↑ Summary TableDataRole/Source/AOH.pm »