in reply to Embedded Newlines or Converting One-Liner to Loop

Text::CSV can handle embedded newlines.
  • Comment on Re: Embedded Newlines or Converting One-Liner to Loop

Replies are listed 'Best First'.
Re^2: Embedded Newlines or Converting One-Liner to Loop
by mwb613 (Beadle) on Dec 15, 2016 at 01:17 UTC

    Right... I'm just seeing the old thread (from 2003) now

    I think I ignored it before because my log processor doesn't just work with CSVs but I think I can probably work around that.

    Thanks!

      Hi mwb613,

      I second Text::CSV as "the" solution for reading CSV files, especially if they've got things like embedded newlines. It would also help in the case that the input format changes, since Text::CSV is quite flexible.

      my log processor doesn't just work with CSVs but I think I can probably work around that

      One solution would be to abstract out the handling of records, so that it doesn't matter where they come from:

      my $csv = Text::CSV->new({ binary => 1, eol => $/ }) or die Text::CSV->error_diag; open my $fh, "<", $file or die "$file: $!"; while ( my $row = $csv->getline($fh) ) { handle_row($row); } $csv->eof or $csv->error_diag(); close $fh; sub handle_row { # ... }

      (Untested, mostly a copy-n-paste from Text::CSV.)

      Hope this helps,
      -- Hauke D