Skip to content

Instantly share code, notes, and snippets.

@esobchenko
Created December 6, 2018 15:11
Show Gist options
  • Save esobchenko/e8ee43b4d14d54988e4707c28bc060f1 to your computer and use it in GitHub Desktop.
Save esobchenko/e8ee43b4d14d54988e4707c28bc060f1 to your computer and use it in GitHub Desktop.
perl encode decode utf8
#!/usr/bin/env perl
use strict;
use warnings;
use Encode qw/encode decode _utf8_on/;
use utf8;
binmode(STDOUT,':utf8'); # suppress "widecharacter in print" warnings
my $foo = "it's ascii";
my $bar = "это юникод";
my @a = ($foo, $bar);
for my $s (@a) {
printf "raw: is_utf8('$s') == %d\n", utf8::is_utf8($s);
my $e = encode("utf8", $s);
print "encoded octets = ";
print "0x$_ " for unpack "(H2)*", $e;
print "\n";
my $d = decode("utf8", $e);
printf "decoded: is_utf8('$d') == %d\n", utf8::is_utf8($d);
print "decoded octets = $d\n";
print "-" x 50;
print "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment