Saturday, January 1, 2011

How can I output UTF-8 from Perl?

In the environment:
export PERL_UNICODE=SDL

on the commandline:
perl -CSDL -le 'print "\x{1815}";

or with binmode:
binmode(STDOUT, ":utf8");          #treat as if it is UTF-8
binmode(STDIN, ":encoding(utf8)"); #actually check if it is UTF-8
 
or with PerlIO:
open my $fh, ">:utf8", $filename
    or die "could not open $filename: $!\n";
 

open my $fh, "<:encoding(utf-8)", $filename
    or die "could not open $filename: $!\n";
 
or with the open pragma:
use open ":encoding(utf8)";
use open IN => ":encoding(utf8)", OUT => ":utf8";

No comments: