Perl Language Split a string on unquoted separators Text::CSV or Text::CSV_XS

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

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";

Output:

a quoted, comma
 word1
 word2

NOTES

  • By default, Text::CSV does not strip whitespace around separator character, the way Text::ParseWords does. However, adding allow_whitespace=>1 to constructor attributes achieves that effect.

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

    Output:

    a quoted, comma
    word1
    word2
    
  • The library supports escaping special characters (quotes, separators)

  • The library supports configurable separator character, quote character, and escape character

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



Got any Perl Language Question?