in reply to Text::CSV_XS and MySQL newline handling

You don't have to do as much as you think by way of doing reading and writing yourself, since Text:CSV_XS's getline returns an arrayref that you can manipulate.

# parse the row and handle any errors my $in_values = $parser->getline($in_fh); croak("Unable to parse line $line_num of file $file: " . $parser->error_input()) if !$in_values; #just add... for (@$in_values) { s:\\\\n:\n:gso }

With the addition of one line, you've unescaped your newlines. And, of course, to put them back, you'd reverse the substitution right before your $csv->print. Ideal? No. But it works, and the performance hit is negligible.

<-radiant.matrix->
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
"In any sufficiently large group of people, most are idiots" - Kaa's Law

Replies are listed 'Best First'.
Re^2: Text::CSV_XS and MySQL newline handling
by perrin (Chancellor) on Nov 17, 2005 at 20:52 UTC
    It's not that simple. Text::CSV_XS will not parse the output the way MySQL generates it. It can't read the line with the escaped newline in it. That means I have to read the lines myself, dealing with possible embedded newlines, and feed them to $parser->parse().

    The output part is also tricky because I would have to escape the newlines AFTER Text::CSV_XS generates the line. If I do it before, then it will escape my escape character when I call print() or combine().