in reply to Text::CSV_XS and line-endings

friedo--

This works for me:
#!/usr/bin/perl -w use strict; use Text::CSV_XS; # Slurp up the whole file open(INF,"<test.mac") || die "Can't open test.mac!"; my $file = <INF>; close(INF); # Convert CRs to LFs $file =~ s/\015/\012/g; # Parse CSV file line-by-line my $csv = Text::CSV_XS->new(); for my $i (split /\012+/, $file) { my $status = $csv->parse($i); print "ST:", $status; for my $j ($csv->fields) { print " [", $j, "]"; } print "\n"; }
--roboticus

Replies are listed 'Best First'.
Re^2: Text::CSV_XS and line-endings
by jdalbec (Deacon) on Mar 17, 2006 at 03:45 UTC
    Since we don't know what OS the client was running, perhaps
    # Convert CRs and CRLFs to LFs $file =~ s/\015\012?/\012/g;
    is best. Are LFCRs a possible concern?
      Use Text::FixEOL to fix messed up line endings. It does the sane thing for even really messed up line endings in most cases. That is what it was written for.
      use Text::FixEOL; # Convert EOLs in the $file string to unix conventions my $fixer = Text::FixEOL->new; $file = $fixer->to_unix($file);
Re^2: Text::CSV_XS and line-endings
by samtregar (Abbot) on Mar 17, 2006 at 16:19 UTC
    That won't work for rows that contain new-lines, which is common in CSVs since they don't have a way to escape them. Text::CSV_XS handles that with its binary option, but only if you let it read the lines for obvious reasons.

    -sam