SPUG:use Text::CSV?

Adam Monsen adamm at wazamatta.com
Thu Feb 27 03:13:53 CST 2003

Michael R. Wolf wrote:
> Text::CSV->combine() seems to be broken.  I've always seen the
> module, but just had a chance to use it.  Boy was I disappointed.

May be related to something in the CAVEATS section in the perldoc:

   "1. Allowable characters within a CSV field include 0x09
   (tab) and the inclusive range of 0x20 (space) through
   0x7E (tilde)."

usually this is the culprit. Everything but these ASCII characters must 
be stripped.

   #!/usr/bin/perl -w
   use Text::CSV_XS;
   use strict;

   my @fields = ('foo', "bar\nblah", "ba^Lz");
   $_ =~ tr/\x09\x20-\x7E/ /c for @fields;

   my $csv = Text::CSV_XS->new({always_quote => 1});
   if ($csv->combine(@fields)) {
     my $string = $csv->string;
     print $string, "\n";
   } else {
     my $err = $csv->error_input;
     print "combine() failed on argument: ", $err, "\n";

To really see the test work, ^L needs to be re-entered as a literal 
CTRL-L, not just a carat followed by an L. It's not uncommon to see 
strange characters if the input for the combine() call was cut and 
pasted from MS Word on a Mac or PC into an HTML form.

> Should I even be using Text::CSV, or is there something that's beyond
> version 0.01, and therefore better, to read/write CSV files?

If you don't mind using C, I'd use Text::CSV_XS, it's a helluva lot
faster than Text::CSV.

DBD::CSV looks cool too, but I haven't tried it.

More information about the spug-list mailing list