As you are already familiar with Text::CSV, please keep using its readline instead of falling back to perl's <> method. Things turn nasty quite fast when the CSV contains nested quotation, Unicode or newlines. (This line was updated as GrandFather just copied the use of OP's reading method).

Check if you have installed Text::CSV_XS. Text::CSV is just a wrapper module over Text::CSV_PP and/or Text::CSV_XS. The XS version is about 100 times faster than the PP version and with lots of columns and/or rows, the difference adds up quite fast (see this graph).

update: I extended the speed compare tests with a plain split approach (which obviously breaks on nested sep_char, newlines and other problematic content. Even then Text::CSV_XS can outperform plain perl! See here.

Use the getline method. Do not mix perl's readline (<>) with the parse method.

(Already mentioned) Use three-arg open calls and lexical handles.

Use builtin error reporting (the auto_diag attribute). No need for else-branches at all.

#!/usr/bin/perl use strict; use warnings; use Text::CSV_XS; my $csv = Text::CSV_XS->new ({ binary => 1, # Always do so auto_diag => 1, # Makes finding bugs a whole lot easier }); my $file = "File_name.csv"; # Now you can use it in error reporting open my $fh, "<", $file or die "$file: $!"; # Three-arg open $csv->getline ($fh) for 1, 2; # Skip first two lines while (my $row = $csv->getline ($fh)) { tr/;,/ ./ for @$row; say join "\t" => @$row; } close $fh;

Enjoy, Have FUN! H.Merijn

In reply to Re: ideas on how to improve existing code by Tux
in thread ideas on how to improve existing code by trolis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.