For the “native” way of doing this, you can convert when copying using this method:
Set the mode in the memory file in the desired mode and read it. This will do the conversion when the characters are read.
use strict; use warnings; my $utf_str = "αβΩ"; #alpha; bravo; omega print "$utf_str is ", length $utf_str, " characters\n"; use open ':encoding(utf8)'; open my $fh, '<', \$utf_str; my $new_str; { local $/; $new_str=<$fh>; } binmode(STDOUT, ":utf8"); print "$new_str ", length $new_str, " characters"; #output: αβΩ is 6 characters αβΩ 3 characters
If you want to convert the encoding into place, you can use this:
my $utf_str = "αβΩ"; print "$utf_str is ", length $utf_str, " characters\n"; binmode(STDOUT, ":utf8"); utf8::decode($utf_str); print "$utf_str is ", length $utf_str, " characters\n";
However, you should not shy away from Encode .
source share