in reply to ideas on how to improve existing code

There is no efficiency issue of note. In particular, if it does the job you need to do fast enough it is sufficiently efficient. However there is a bunch of stuff that can be improved.

For a start you should always use the three argument version of open and lexical file handles:

open my $csvIn, '<', $fileName or die "Failed to open $fileName: $!";

It also helps if error strings provide more than just the bare minimum of information. For an open including the file name that was being used (rather than hoping it is what you think it ought to be) helps a huge amount.

Skipping the first two lines in the loop that handles the rest of the lines is ok, but not near as clear as skipping them outside the loop:

<$csvIn> for 1 .. 2; while (defined (my $line = <$csvIn>)) {

Using translate for simple character translation is fast and clear:

$line =~ tr/;,/ ./;

Although if you were to use regular expressions you should note that there is no need to quote ; in a regular expression string and no need to quote anything in the replacement string (except stuff you'd quote if it were a double quoted string).

Using consistent indentation helps a lot!

I prefer to make function calls explicit by supplying parenthesis even is they aren't strictly required. At the very least you should be consistent (cf: $csv->fields and $csv->error_input).

Taking that all together you'd end up with something like the following (untested) code:

#!/usr/bin/perl use strict; use warnings; use Text::CSV; my $csv = Text::CSV->new (); my $fileName = 'File_name.csv'; open my $csvIn, '<', $fileName or die "Can't open $fileName: $!"; <$csvIn> for 1 .. 2; while (defined (my $line = <$csvIn>)) { $line =~ tr/;,/ ./; if ($csv->parse ($line)) { my @columns = $csv->fields (); print "@columns\n"; } else { my $err = $csv->error_input; print "Failed to parse line $.: $err"; } } close $csvIn;
True laziness is hard work

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.