Please note Text::CSV's documentation:
open my $fh, "<:encoding(utf8)", "test.csv" or die "test.csv: $!"; while ( my $row = $csv->getline( $fh ) ) { $row->[2] =~ m/pattern/ or next; # 3rd field should match push @rows, $row; }
Let the $csv object read from the file handle (and parse the line), instead of:
... while (my $line = <CSV>) { chomp $line; if ($csv->parse($line)) { ...
In the documentation, $row is an array reference, pointing to the array that contains the line's parsed fields. You can just use @$row to get all those fields.
It's not clear to me what chomp (@fields); is doing in your script. It looks like you're expecting @fields to contain the parsed fields from the currently-read line. But there wouldn't be any newlines in @fields, so chomping it is unnecessary.
Also, as a side note, consider using lexical variables (my) for file handles, instead of barewords. For example:
open my $CSV, '<:encoding(utf8)', $file or die "Cannot open $file: $!\ +n"
In reply to Re: Read text file - Encoding problem?
by Kenosis
in thread Read text file - Encoding problem?
by better
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |