Your code has this:
print "," if $c < $#columns;
That attempts to construct a CSV record by merely putting commas between fields. If there is anything in the field (for example the newlines the OP requested), then you will not end up with a valid CSV file. Text::CSV_XS
has the combine() method that will properly create records by not only inserting commas, but also, when called for quoting and escaping the field data. Other CSV modules like Text::xSV
have similar methods to not only parse CSV correctly, but to produce it correctly.