如何使用perl的utf8参数调用CMD?

时间:2018-02-26 20:41:59

标签: windows perl unicode cmd character-encoding

如何使用perl的utf8参数调用CMD而不会弄乱参数的字符?

我尝试过的一件事是将一串unicode字符转换为unicode字符代码,然后使用system($cmd)

use utf8;

`chcp 65001`;
binmode STDOUT, ":encoding(UTF-8)";
$string = "αω";
$converted_string = convert_to_unicode_code($string);
    # gets $converted_string = '\x{03B1}\x{03C9}'
$cmd = 'program "'.$converted_string.'"';
    # $cmd's value is: program "\x{03B1}\x{03C9}"
system($cmd);

sub convert_to_unicode_code {
    my $input = shift;
    $input =~ s/(.)/"\\x{" . (sprintf "%04X", ord $1) . "}"/eg;
    return $input;
}

实际上此解决方案无法按预期工作,并调用program "\x{03B1}\x{03C9}"而不是program "αω"

1 个答案:

答案 0 :(得分:1)

请参阅Win32::Unicode

αω.bat

@echo hiαω

so48996757.pl

use utf8;
use Win32::Unicode::Process qw(systemW);

system 'chcp 65001';
systemW 'αω';