Text :: CSV perl模块中的is_quoted()方法

bar*_*ush 6 csv perl perl-module

我可能会遗漏一些明显的东西,但我有一个非常简单的perl脚本,其中Text :: CSV模块中的is_quoted()方法没有按预期工作.这是代码:

# cat ./testcsv.pl
#!/usr/bin/perl

use strict;
use Text::CSV;

my $csv = Text::CSV->new ( { quote_char => '"' } )
        or die "Cannot use CSV: ".Text::CSV->error_diag ();

print "Text::CSV version = " . $csv->version() . "\n\n";

my $line = '"text field 111",222,"text field 333",444';

my $status  = $csv->parse($line);
if ($status)
  {
  my $column_idx = 0;
  my @fields = $csv->fields ();
  foreach my $field (@fields)
     {
     my $quoted = $csv->is_quoted ($column_idx);
     $column_idx++;
     print "field #$column_idx: '$field'; quoted = " . ($quoted ? "YES\n" : "NO\n");
     }
  }
Run Code Online (Sandbox Code Playgroud)

这是我运行脚本时得到的结果:

    # perl -v  | grep "is perl"
    This is perl, v5.10.1 (*) built for PA-RISC2.0
    # ./testcsv.pl
    Text::CSV version = 1.29

    field #1: 'text field 111'; quoted = NO
    field #2: '222'; quoted = NO
    field #3: 'text field 333'; quoted = NO
    field #4: '444'; quoted = NO
    # 

我们可以看到,parse()方法正确地将原始字符串分隔成字段,因此我知道Text :: CSV已安装且工作正常.通过阅读Text :: CSV文档,我的理解是,如果指示列中的数据包含在quote_char引号中,则is_quoted()方法应该返回true值.因此我希望在字段1和3之后看到'YES',因为它们在$ line变量的初始化中被明确引用.但这不会发生.

我做错了什么,还是Text :: CSV坏了?

ike*_*ami 7

你需要指定keep_meta_info => 1.


顺便说一句,我不喜欢有两个迭代器,所以我会迭代索引.

my @fields = $csv->fields();
for my $column_idx (0..$#fields) {
   my $field = $fields[$column_idx];
   ...
}
Run Code Online (Sandbox Code Playgroud)