Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Perl中正确使用编码为Windows-1251的环境变量?_Windows_Perl_Character Encoding - Fatal编程技术网

如何在Perl中正确使用编码为Windows-1251的环境变量?

如何在Perl中正确使用编码为Windows-1251的环境变量?,windows,perl,character-encoding,Windows,Perl,Character Encoding,我在Windows中设置了一个环境变量作为TEST=abc.,它使用Windows-1252代码页。现在,当我运行一个Perl程序test.pl时,这个环境值正确地出现了 当我通过system(…)或Win32::Process从test1.pl调用另一个Perl代码-test2.pl时,环境变得混乱 有人能提供为什么会出现这种情况以及解决方法吗 我使用的perl版本是5.8 如果我的理解是正确的,perl在内部使用utf-8,因此初始过程-test1.pl直接从Windows-1252→ ut

我在Windows中设置了一个环境变量作为
TEST=abc.
,它使用
Windows-1252
代码页。现在,当我运行一个Perl程序
test.pl
时,这个环境值正确地出现了

当我通过
system(…)
Win32::Process
test1.pl
调用另一个Perl代码-
test2.pl
时,环境变得混乱

有人能提供为什么会出现这种情况以及解决方法吗

我使用的
perl
版本是5.8


如果我的理解是正确的,
perl
在内部使用
utf-8
,因此初始过程-
test1.pl
直接从
Windows-1252
→ <代码>utf-8。当我们调用另一个进程时,是否应该转换回
Windows-1252
代码页?

这与Perl的内部字符串编码无关,而是需要正确解码来自外部的数据。我将提供测试用例。这是西欧Windows XP上的草莓Perl 5.10

test1.pl:

use Devel::Peek;
print Dump $ENV{TEST};
use Encode qw(decode);
my $var = decode 'Windows-1252', $ENV{TEST};
print Dump $var;

system "B:/sperl/perl/bin/perl.exe B:/test2.pl";
test2.pl:

use Devel::Peek;
print Dump $ENV{TEST};
use Encode qw(decode);
my $var = decode 'IBM850', $ENV{TEST};
# using Windows-1252 again is wrong here
print Dump $var;
执行:

> set TEST=abc£
> B:\sperl\perl\bin\perl.exe B:\test1.pl
输出(缩短):

Windows对文本环境(IBM850)使用的编码与图形环境(Windows-1252)使用的编码不同,这一事实让您大吃一惊。专家必须解释这种现象的深层次细节

编辑:

可以通过启发式(这意味着它有时无法做正确的事情,尤其是对于如此短的字符串)确定编码。最好的通用解决方案是基于


有一些方法可以隐式解码外部数据,例如和,但是它们只处理文件流和程序参数。到目前为止,必须明确地解码来自环境的信息。不管怎样,我更喜欢它,explicite向维护程序员展示了您所思考的问题。

如果您提供最少的代码来演示问题,您将更快地得到答案。如果你只是用文字来描述,那么还有很多想象和困惑。为什么在你所问的六个问题中,你收到的十几个答案中没有一个能被接受?谢谢daxim的回答。很少有问题-1)可以确定来自外部世界的字符串使用什么编码吗?这将有助于在perl中进行相应的解码。2) 有没有一种方法可以指示perl在内部处理解码部分,而不是我们自己决定外部世界使用的编码-KartleeThanks提醒你。我现在这样做是为了我认为可以接受的。
SV = PVMG(0x982314) at 0x989a24
  FLAGS = (SMG, RMG, POK, pPOK)
  PV = 0x98de0c "abc\243"\0
SV = PV(0x3d6a64) at 0x989b04
  FLAGS = (PADMY, POK, pPOK, UTF8)
  PV = 0x9b5be4 "abc\302\243"\0 [UTF8 "abc\x{a3}"]
SV = PVMG(0x982314) at 0x989a24
  FLAGS = (SMG, RMG, POK, pPOK)
  PV = 0x98de0c "abc\243"\0
SV = PV(0x3d6a4c) at 0x989b04
  FLAGS = (PADMY, POK, pPOK, UTF8)
  PV = 0x9b587c "abc\302\243"\0 [UTF8 "abc\x{a3}"]