################ package MyCSV; ################ use Text::CSV_XS; use IO::Scalar; # parse_csv() # # receives a CSV string # returns a table of rows and column as an AoA # sub parse_csv { my($input_str)=@_; my $aoa = []; my $csv = Text::CSV_XS->new({binary=>1}); my $fh = IO::Scalar->new(\$input_str); while (my $cols = $csv->getline($fh)) { last unless @$cols; push @$aoa,$cols } return $aoa } # produce_csv() # # receives a table of rows and columns as an AoA # returns a CSV string # sub produce_csv { my($aoa)=@_; my $output_str = ''; my $csv = Text::CSV_XS->new({binary=>1}); for my $row( @$aoa ) { @$row = map {defined $_ ? $_ : ''} @$row; my $success = $csv->combine(@$row); die "Coulnd't parse '@$row'\n" unless $success; $output_str .= $csv->string . $/; } return $output_str; } 1;