【问题标题】:Perl unicode support from console (@ARGV) /Windows/来自控制台 (@ARGV) /Windows/ 的 Perl unicode 支持
【发布时间】:2017-02-15 13:51:59
【问题描述】:

我正在尝试在 perl 脚本中获取 unicode 字符作为参数:

C:\>perl test.pl ö

#----
# test.pl
#----
#!/usr/bin/perl
use warnings;
use strict;

my ($name, $number) = @ARGV;

if (not defined $name) {
    die "Need name\n";
}

if (defined $number) {
    print "Save '$name' and '$number'\n";
    # save name/number in database
    exit;
}

if ($name eq 'ö') {
    print "Fetch umlaut 'oe'\n";
} elsif ($name eq 'o') {
    print "Fetch simple 'o'\n";
} else {
    print "Fetch other '$name'\n";
}

print "ü";

我得到了输出:

Fetch simple 'o'
ü

我已经在 python 3 中测试了代码(算法)并且它可以工作,所以我得到“ö”。 但显然在 perl 中还有一些我必须添加或设置的东西。 不管是草莓 Perl 还是 ActiveState Perl。我得到了同样的结果。

提前致谢!

【问题讨论】:

    标签: perl unicode argv


    【解决方案1】:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $encoding_in;
    my $encoding_out;
    my $encoding_sys;
    BEGIN {
        require Win32;
    
        $encoding_in  = 'cp' . Win32::GetConsoleCP();
        $encoding_out = 'cp' . Win32::GetConsoleOutputCP();
        $encoding_sys = 'cp' . Win32::GetACP();
    
        binmode(STDIN,  ":encoding($encoding_in)");
        binmode(STDOUT, ":encoding($encoding_out)");
        binmode(STDERR, ":encoding($encoding_out)");
    }
    
    use Encode qw( decode );
    
    {
        my ($name, $number) = map { decode($encoding_sys, $_) } @ARGV;
    
        if (not defined $name) {
            die "Need name\n";
        }
    
        if (defined $number) {
            print "Save '$name' and '$number'\n";
            # save name/number in database
            exit;
        }
    
        if ($name eq 'ö') {
            print "Fetch umlaut 'oe'\n";
        } elsif ($name eq 'o') {
            print "Fetch simple 'o'\n";
        } else {
            print "Fetch other '$name'\n";
        }
    
        print "ü";
    }
    

    此外,您应该添加 use feature qw( unicode_strings ); 和/或使用 UTF-8 对文件进行编码并添加 use utf8;。

    【讨论】:

    • 您对Encode::Locale 有意见吗?它似乎取代了你那里的几行代码。
    • @tjd,看起来你确实可以使用它。随意发布答案,我会投票甚至删除我的答案!
    • 它不起作用。我只得到简单的'o'ü显然它会自动将“ö”解析为“o”。这是为 MSWin32-x64-multi-thread 构建的 perl 5,版本 24,subversion 0 (v5.24.0)。
    • 这个已经过测试。你能提供程序填充的编码变量的值吗?
    • @ikegami 我可以更改编码,但 sys 编码保持不变。编码输入:cp866 编码输出:cp866 编码系统:cp1251 或编码输入:cp65001 编码输出:cp65001 编码系统:cp1251
    【解决方案2】:

    除了 ikagami 的好答案之外,我还是 Encode::Locale 模块的粉丝,该模块会自动为当前控制台的代码页创建别名。它适用于 Win32、OS X 和其他风格的 *nix。

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    # These two lines make life better when you leave the world of ASCII
    # Just remember to *save* the file as UTF8....
    use utf8;
    use feature 'unicode_strings';
    
    use Encode::Locale 'decode_argv';         # We'll use the console_in & console_out aliases as well as decode_argv().
    use Encode;
    
    binmode(STDIN,  ":encoding(console_in)");
    binmode(STDOUT, ":encoding(console_out)");
    binmode(STDERR, ":encoding(console_out)");
    
    decode_argv( );   # Decode ARGV in place
    my ($name, $number) = @ARGV;
    
    if (not defined $name) {
        die "Need name\n";
    }
    
    if (defined $number) {
        print "Save '$name' and '$number'\n";
        # save name/number in database
        exit;
    }
    
    if ($name eq 'ö') {
        print "Fetch umlaut 'oe'\n";
    } elsif ($name eq 'o') {
        print "Fetch simple 'o'\n";
    } else {
        print "Fetch other '$name'\n";
    }
    
    print "ü";
    

    也许它只是语法糖,但它易于阅读并促进跨平台兼容性。

    【讨论】:

    • 未定义子程序 &main::decode_argv 在 vi​​dCmpr2.pl 第 18 行调用。
    • @Banish 你是对的。我忘记在使用行中导入该符号。谢谢提醒。
    • 是的,我必须导入包。现在它编译成功了,仅此而已。我已经测试了它更改控制台代码页(chcp 1252)甚至使用 perl6。它将“ö”解析为简单的“o”。 Python和java解析它没有问题。
    • PS:对不起。我错了。它仅适用于 python。它不适用于 java。
    • @Banish 最后一步是确保您要比较的字符串采用相同的(标准化)形式。
    【解决方案3】:

    我认为这个问题的代码答案很明确但不完整:

    • 那样的话,构建一个包含所有代码页+源代码的脚本非常复杂,而且更难移植:ö可能是拉丁字母用户都知道,但 の 或 렌 也存在...

    • 它们可能在特定代码页中使用字符运行正常,但如果在其外部使用字符,它们将失败(cmet 中的某些用户可能就是这种情况)。请注意Windows' Code Pages are previous to Unicode。

    • 根本问题是 Windows 的 Perl 5 编译时不支持 Unicode,正如 Windows 所理解的那样:它只是 linux 代码的一个端口,因此,几乎所有 Unicode 字符在到达 Perl 代码之前就被破坏了。

    A 提供了更长的技术解释(和 C 补丁!)。 Sinan Unur 的页面 Fixing Perl's Unicode problems on the command line on Windows: A trilogy in N parts(在 Artistic License 2.0 下)。

    所以(但不适合胆小的人):重新编译 perl.exe 是可能的,并且在 Windows 中几乎完全符合 Unicode。希望有一天它们会被集成到源代码中......直到它们我已经恢复some detailed instructions to patch perl.exe here。

    另请注意,需要具有完全 Unicode 支持的适当命令控制台。一个快速的解决方案是使用 ConEmu,但 Windows 的 cmd.exe could also work after some heavy tweaks。

    【讨论】:

      【解决方案4】:

      我不知道这是否是非常场景的解决方案,但我可以在调用我的脚本时使用参数“-CAS”。

      例子:

      脚本_1:

      use strict;
      use utf8;
      
      $|++; # Prevent buffering issues
      
      
      my ($arg) = @ARGV;
      save_to_file('test.txt', $arg);
      
      sub save_to_file{   
      
          my ($filename, $content) = @_;
      
          open(my $fh, '>:encoding(UTF-8)', $filename) or die "Can't open < $filename: $!";;
          print $fh $content;
          close $fh;
      
          return;
      }
      

      Script_2 调用 1:

      use strict;
      use utf8;
      
      
      execute_command();
      
      sub execute_command {
      
      
          my $command = "perl -CAS simple_utf_string.pl äääöööü";
      
          # Execute command
          print "The command to run is: $command\n";
          open my $command_pipe, "-|:encoding(UTF-8)", $command or die "Pipe from $command failed: $!";
          while (<$command_pipe>) {
              print  $_;
          }
      }
      

      结果:text.txt:

      äääöööü
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-13
        • 2012-03-11
        • 1970-01-01
        • 1970-01-01
        • 2010-09-07
        • 1970-01-01
        • 1970-01-01
        • 2016-10-18
        相关资源
        最近更新 更多