TextCSV 或 TextCSV XS

use Text::CSV; # Can use Text::CSV which will switch to _XS if installed
$sep_char = ",";
my $csv = Text::CSV->new({sep_char => $sep_char});
my $line = q{"a quoted, comma", word1, word2};
$csv->parse($line);
my @fields = $csv->fields();
print join("\n", @fields)."\n";

输出:

a quoted, comma
 word1
 word2

笔记

  • 默认情况下,Text::CSV 不会剥离分隔符周围的空格,就像 Text::ParseWords 那样。但是,将 allow_whitespace=>1 添加到构造函数属性可以实现该效果。

    my $csv = Text::CSV_XS->new({sep_char => $sep_char, allow_whitespace=>1});  
    

    输出:

    a quoted, comma
    word1
    word2
    
  • 该库支持转义特殊字符(引号,分隔符)

  • 该库支持可配置的分隔符,引号字符和转义字符

Documentatoin: http//search.cpan.org/perldoc/Text :: CSV