When reading from the CSV, follow the documentation and use
my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 }); # ~~~~~~~~~~~ open my $fh, "<:encoding(utf8)", "test.csv" or die "test.csv: $!"; # ~~~~~~~~~~~~~~~
When encoding to JSON, be sure to decode the string, as Cpanel::JSON::XS works with UTF-8 encoded strings.
#!/usr/bin/perl use strict; use warnings; use feature qw{ say }; use charnames ':full'; use Cpanel::JSON::XS; use Encode qw{ encode }; my $decoded = qq({"yellow horse": "\N{LATIN SMALL LETTER Z WITH CARON} +lu\N{LATIN SMALL LETTER T WITH CARON}ou\N{LATIN SMALL LETTER C WITH C +ARON}k\N{LATIN SMALL LETTER Y WITH ACUTE} k\N{LATIN SMALL LETTER U WI +TH RING ABOVE}\N{LATIN SMALL LETTER N WITH CARON}"}); my $encoded = encode('UTF-8', $decoded); my $structure = decode_json($encoded); binmode *STDOUT, ':encoding(UTF-8)'; say $structure->{'yellow horse'};
Note that I also set the encoding of the output handle.
I can't show you how to set the encoding/decoding properly for the database as you haven't told us what driver you use.
NOTE: In real life, I'd use use utf8; and type "žluťoučký kůň" directly in the script, but PerlMonks can't display code containing non-latin1 characters. Some people even recommend to write your code this way, but I find utf-8 more readable.
In reply to Re: Substitute and converting to UTF8
by choroba
in thread Substitute and converting to UTF8
by tomred
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |