Perl Text::CSV_XS在注解是最后一行时将其视为数据

fdbelqdn  于 5个月前  发布在  Perl
关注(0)|答案(1)|浏览(41)

我想知道这是Text::CSV_XS的一个bug还是特性。当我的CSV中的最后一行是注解时,它会被解析为包含数据的行(产生undef或“”)。如何repodruce:

$ cat test.csv
id | name
#
42 | foo
#

字符串
这是我的Perl脚本:

#!/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);


当运行时,它会转储以下内容:

$VAR1 = [
          {
            'id' => '42',
            'name' => 'foo'
          },
          {
            'id' => '',             # WHY IS THIS ENTRY HERE?
            'name' => undef
          }
        ];


我本以为最后一行会被当作注解而忽略。当我从test.csv中删除最后一行的注解时,我得到了我所期望的,只有一行:

$VAR1 = [
          {
            'id' => '42',
            'name' => 'foo'
          }
        ];


我错过了什么?我在Ubuntu Jammy上使用Text::CSV_XS版本1.47。

qcuzuvrc

qcuzuvrc1#

报告后,此问题已在1.53版更新日志条目中得到修复

1.53    - 2023-11-22, H.Merijn Brand
    * Two casts for -Wformat (issue 50)
    * Add --skip-empty to csv2xlsx
    * Add --font and --font-size to csv2xlsx
    * Fix skip_empty_rows ("skip") and trailing newlines (Corey Hickey, PR#52)
    * Fix comment in last line (RT#150501)

字符串

相关问题