in reply to Converting a delimited file.

Use Text::CSV_XS' csv function with the on_in callback:

$ cat test.pl use 5.20.0; use warnings; use Text::CSV_XS "csv"; csv (in => "test.csv", blank_is_undef => 1, on_in => sub { splice @{$_[1]}, 2, 2, $_[1][2] // $_[1][3]; }); $ cat test.csv 1,2,,,5,6 1,2,3,,5,6 1,2,,4,5,6 1,2,3,4,5,6 $ perl test.pl 1,2,,5,6 1,2,3,5,6 1,2,4,5,6 1,2,3,5,6

update: That also surfaced a small buglet in the code, as I wanted to show that that all fields on output could be forced quoted in the same call, but somewhere that option currently gets lost. The workaround however is easy:

$ cat test.pl use 5.20.0; use warnings; use Text::CSV_XS "csv"; csv (quote_always => 1, in => csv (in => "test.csv", blank_is_undef => + 1, on_in => sub { splice @{$_[1]}, 2, 2, $_[1][2] // $_[1][3]; })); $ perl test.pl "1","2",,"5","6" "1","2","3","5","6" "1","2","4","5","6" "1","2","3","5","6"

Working on a fix for 1.18 ...

update 2: fixed. I'll release after going through the long test procedure.

$ perl -MText::CSV_XS=csv -e'csv (in => "test.csv", blank_is_undef => +1, quote_always => 1, on_in => sub { splice @{$_[1]}, 2, 2, $_[1][2] +// $_[1][3]; });' "1","2",,"5","6" "1","2","3","5","6" "1","2","4","5","6" "1","2","3","5","6"

Enjoy, Have FUN! H.Merijn