Skip to content

Commit b8376e8

Browse files
committed
Merge branch 'michal-josef-spacek-process_csv_bom' into dev
2 parents 9f4fe15 + 4938ca0 commit b8376e8

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

lib/Catmandu/Importer/CSV.pm

+3-2
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ sub generator {
5858
state $csv = do {
5959
if ($self->header) {
6060
if ($self->fields) {
61-
$self->csv->getline($fh);
61+
$self->csv->header($fh, { 'sep_set' => [$self->sep_char] });
6262
$line++;
6363
}
6464
else {
65-
$self->_set_fields($self->csv->getline($fh));
65+
my @hdr = $self->csv->header($fh, { 'munge_column_names' => 'none', 'sep_set' => [$self->sep_char] });
66+
$self->_set_fields(\@hdr);
6667
$line++;
6768
}
6869
}

t/Catmandu-Importer-CSV.t

+24-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,19 @@ my $importer = $pkg->new(file => \$csv);
2626

2727
isa_ok $importer, $pkg;
2828

29-
is_deeply $importer->to_array, $data;
29+
is_deeply $importer->to_array, $data, 'CSV with header.';
30+
31+
$csv = <<EOF;
32+
\xEF\xBB\xBF"name","age"
33+
"Patrick","39"
34+
"Nicolas","34"
35+
EOF
36+
37+
$importer = $pkg->new(file => \$csv);
38+
39+
isa_ok $importer, $pkg;
40+
41+
is_deeply $importer->to_array, $data, 'CSV with header, BOM.';
3042

3143
$data = [{0 => 'Patrick', 1 => '39'}, {0 => 'Nicolas', 1 => '34'},];
3244

@@ -37,7 +49,7 @@ EOF
3749

3850
$importer = $pkg->new(file => \$csv, header => 0);
3951

40-
is_deeply $importer->to_array, $data;
52+
is_deeply $importer->to_array, $data, 'CSV without header.';
4153

4254
$data = [{name => 'Nicolas', age => '34'},];
4355

@@ -48,7 +60,16 @@ EOF
4860

4961
$importer = $pkg->new(file => \$csv, sep_char => '\t');
5062

51-
is_deeply $importer->to_array, $data;
63+
is_deeply $importer->to_array, $data, 'CSV with header, separator is tab.';
64+
65+
$csv = <<EOF;
66+
\xEF\xBB\xBF"name" "age"
67+
"Nicolas" "34"
68+
EOF
69+
70+
$importer = $pkg->new(file => \$csv, sep_char => '\t');
71+
72+
is_deeply $importer->to_array, $data, 'CSV with header, separator is tab, BOM.';
5273

5374
done_testing;
5475

0 commit comments

Comments
 (0)