in reply to Question about parsing columns when using Text::CSV

In order to start programming defensive and safe, don't use the diamond for CSV

use strict; use warnings; use Text::CSV; my ($file, output) = ("112008.csv", "112008.txt"); my $csv = Text::CSV->new ({ binary => 1 }); open my $ih, "<", $file or die "Can't open in file $file: $!"; open my $oh, ">", $output or die "Can't open out file $output: $!"; while (my $row = $csv->getline ($ih)) { my @columns = map { sprintf "%.3f", $_ } @$row; # do something with @columns print { $oh } join "|", @columns, "\n"; } $csv->eof () or $csv->error_diag (); close $ih or die "$file: $!"; close $oh or die "$output: $!";

Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: Question about parsing columns when using Text::CSV
by jeffa (Bishop) on Jan 06, 2009 at 17:44 UTC

    Greetings. What exactly is wrong with using the diamond operator here? What specifically makes it unsafe, and why is using your method considered "better defense?" Thanks.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      Short answer: nested newlines

      Longer answer: Though in the case of the OP, there are no embedded newlines or other binary characters, it's just waiting for it to happen one day or another when new fields are added to the end. CSV is safe and defined enough to cope, but if the diamond is used to read CSV (in general), all lines that have embedded newlines will cause the CSV parsing to fail from that point on.

      Note that the $csv->getline () will use the diamond (CORE's getline ()) internally too, but Text::CSV (and Text::CSV_XS and Text::CSV_PP) will know internally that the line might not be finished and will correct that on a need-to-be basis.

      Another reason to use getline () instead of diamond, is that the getline () is magnitudes faster than the combination of diamond + parse (), even when using Text::CSV_PP.


      Enjoy, Have FUN! H.Merijn