#!/usr/bin/perl use strict; use warnings; use Text::Wrap; use Text::CSV_XS; my $csv = Text::CSV_XS->new ( { binary => 1, eol => "\r\n", auto_diag => 1 }); open my $fh, ">", "wraptest.csv" or die "$!"; my @data = ( ['1a this is more than 20 characters', '', '1c and another one that is more than 20 characters'], ['2a this is more than 20 characters', '', '2c and another one that is more than 20 characters'], ); for my $row_index (0..1){ my @row; for my $col_index (0..2){ my $source_cell = $data[$row_index][$col_index]; my $value; if ($source_cell){ local $Text::Wrap::columns = 20; $value = wrap ("", "", $source_cell); } push @row , $value; } $csv->print ($fh, \@row); } close $fh;