I wonder if this is a bug or feature of Text::CSV_XS
. When the last line in my CSV is a comment, it will be parsed as a line containing data (yielding undef or ""). How to repodruce:
$ cat test.csv
id | name
#
42 | foo
#
This is my perl script:
#!/usr/bin/env perl
use warnings;
use diagnostics;
use strict;
use Text::CSV_XS qw(csv);
use Data::Dumper;
# Returns array of hashrefs for argument CSV file.
sub load_csv_file {
my $filename = shift;
return csv (
in => $filename,
sep_char => '|',
headers => 'auto',
allow_whitespace => 1,
comment_str => "#"
);
}
my $table = load_csv_file("test.csv");
print Dumper($table);
When run it dumps this:
$VAR1 = [
{
'id' => '42',
'name' => 'foo'
},
{
'id' => '', # WHY IS THIS ENTRY HERE?
'name' => undef
}
];
I would have expected the last line to be treated as a comment and ignored. When I remove the comment in the last line from test.csv, I get what I expect, just one row:
$VAR1 = [
{
'id' => '42',
'name' => 'foo'
}
];
What am I missing? I'm using Text::CSV_XS version 1.47 on Ubuntu Jammy.
After reporting, this was fixed in version 1.53, Changelog entry