最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Passing UTF-8 arguments to commands in Perl on Windows - Stack Overflow

matteradmin6PV0评论

I am trying to build a template for Perl scripts so that they would do at least most of the basic things right with UTF-8 and would work equally well on Linux and Windows machines.

One thing in particular escaped me for a while: the difficulty of passing UTF-8 strings as arguments to system commands. It seems to me that there is no way not to have arguments double UTF-8 encoded before they reach the shell (that is, I understand that there is a layer that ignores that the command and its arguments are already properly UTF-8 encoded, takes it for Latin-1 or something of the sorts, and encodes it again as UTF-8). I could not find a way to cleanly avoid this layer of encoding.

Take this script:

#!/usr/bin/perl

use v5.14;

use utf8;
use feature 'unicode_strings';
use feature 'fc';
use open ':std', ':encoding(UTF-8)';
use strict;
use warnings;
use warnings FATAL => 'utf8';

use constant IS_WINDOWS => $^O eq 'MSWin32';

# Set proper locale
$ENV{'LC_ALL'} = 'C.UTF-8';

# Set UTF-8 code page on Windows
if (IS_WINDOWS) {
  system("chcp 65001 > nul 2>&1");
};

# Use Win32::Unicode::Process on Windows
if (IS_WINDOWS) {
  eval {
    require Win32::Unicode::Process;
    Win32::Unicode::Process->import;
  };
  if ($@) {
    die "Could not load Win32::Unicode::Process: $@";
  };
};


# Show the empty directory
print "---\n" . `ls -1 system*` . "---\n";

my $utf = "test-тест-מבחן-परीक्षण-
Post a comment

comment list (0)

  1. No comments so far