开发者

utf8::all -- How to force <STDIN/OUT> to big-5

This is my test code: I am using win32 strawberry perl.

use utf8::all; my $input = ;

when key in a Chinese character,

it shows the error:

utf8 "\xAD" does not map to unicode .....

I have also write a script with utf8::all to print Chinese characters, can't success. If just use utf8 or without utf8, I can print Chinese characters by encoding them.

How t开发者_JAVA技巧o set the to other encoding?


How to set the to other encoding?

With utf8::all, you can't. The encoding UTF-8 is hardcoded everywhere in it. The module is named, after all, utf8::all and not big5::all.

You must decode/encode explicitely, see http://p3rl.org/UNI. You said you're on Windows, so employ the encoding cp950.

use Encode qw(decode encode);

my @arguments_as_characters = decode 'cp950', @ARGV;
open my $file_handle, '<:encoding(cp950)', $file_name;
print encode 'cp950', $data_to_stdout;


How to open STDIN, STDOUT in cp950?

When you run your program, the standard streams are already open! You can modify the I/O layer with binmode.

binmode STDOUT, ':encoding(cp950)';


There is a workaround to set the codepage of Windows Command Prompt to UTF-8, but it's not a deploy-able solution.

My suggestion is don't bother, just use Big5 all the way in Perl (and stick to CP950/zh-tw Windows), or use text file I/O for input/output.

Or, to be adventurous, use bash/perl within cgywin that use UTF-8 out of box instead.


use open IO => ':encoding(big5)';

or

use open IO => ':encoding(cp950)';

or

use open IO => ':locale';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜