How can I use environment variables encoded as Windows-1251 in Perl correctly?

I have an environment variable set to Windows as TEST=abc£which uses a Windows-1252codepage. Now when I run the Perl program test.pl, this environment value appears correctly.

When I call other code Perl - test2.plfrom test1.plor through system(..)or Win32::ProcessWednesday distorted.

Can someone provide information, why it can be and how to solve it?

The version perlI'm using is 5.8.

If my understanding is correct, it perluses utf-8internally, so the initial process - I test1.plgot it right from Windows-1252utf-8. When we invoke another process, should we return to the Windows-1252code page?

+5
source share
1 answer

This has nothing to do with Perl's internal string encoding, but with the need to correctly decode data coming from outside. I will put a test case. This is Strawberry Perl 5.10 on West European Windows XP.

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;

Execute:

> set TEST=abc£
> B:\sperl\perl\bin\perl.exe B:\test1.pl

Output (shortened):

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}"]

, Windows (IBM850), (Windows-1252). .

Edit:

( , , ) . Encode:: Detect/Encode:: Detect:: Detector, Mozilla nsUniversalDetector.

, open pragma/IO -C, . . , , , , topic .

+8

All Articles