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