The key is to ensure you're reading the input (and possibly also writing the output) using the right PerlIO layer — in this case, ':encoding(UTF-8)'. If you're using <> or <ARGV> (and possibly also STDOUT), then you may want to use the open pragma.
#!perl
use strict;
use warnings;
use open qw( :encoding(UTF-8) :std );
while (my $record = <>) {
chomp $record;
if ($record =~ m/^(pd\.\d+),([^,]+),([^,]+)$/) {
my ($field1, $field2, $field3) = ($1, $2, $3);
print "$field1\n";
print "$field2\n";
print "$field3\n";
}
}
exit 0;
I recommend you use the Text::CSV module to parse the CSV records instead of a regular expression pattern. This will allow you to parse records that have literal commas in the text.
UPDATE: I changed ':utf8' to ':encoding(UTF-8)' based on something I read in Moritz's fine article titled Character Encodings in Perl.
|