我尝试将字符串转换为 utf8。
#!/usr/bin/perl -w
use Encode qw(encode decode is_utf8);
$str = "\320\300\304\310\323\321 \316\320\300\312\313";
Encode::from_to($str, 'windows-1251', 'utf-8');
print "converted:\n$str\n";
在这种情况下,我得到了我需要的东西:
# ./convert.pl
converted:
РАДИУС ОРАКЛ
但是如果我使用外部变量:
#!/usr/bin/perl -w
use Encode qw(encode decode is_utf8);
$str = $ARGV[0];
Encode::from_to($str, 'windows-1251', 'utf-8');
print "converted:\n$str\n";
什么都没发生。
# ./convert.pl "\320\300\304\310\323\321 \316\320\300\312\313"
converted:
\320\300\304\310\323\321 \316\320\300\312\313
这是第一个示例的转储:
SV = PV(0x1dceb78) at 0x1ded120
REFCNT = 1
FLAGS = (POK,pPOK)
PV = 0x1de7970 "\320\300\304\310\323\321 \316\320\300\312\313"\0
CUR = 12
LEN = 16
第二个:
SV = PV(0x1c1db78) at 0x1c3c110
REFCNT = 1
FLAGS = (POK,pPOK)
PV = 0x1c5e7e0 "\\320\\300\\304\\310\\323\\321 \\316\\320\\300\\312\\313"\0
CUR = 45
LEN = 48
我试过这个方法:
#!/usr/bin/perl -w
use Devel::Peek;
$str = pack 'C*', map oct, $ARGV[0] =~ /\\(\d{3})/g;
print Dump ($str);
# ./convert.pl "\320\300\304\310\323\321 \316\320\300\312\313"
SV = PV(0x1c1db78) at 0x1c3c110
REFCNT = 1
FLAGS = (POK,pPOK)
PV = 0x1c5e7e0 "\320\300\304\310\323\321\316\320\300\312\313"\0
CUR = 11
LEN = 48
但这又不是我需要的。你能帮我得到第一个脚本中的结果吗?
使用这个之后
($str = shift) =~ s/\\([0-7]+)/chr oct $1/eg
正如鲍罗丁建议的那样,我明白了
SV = PVMG(0x13fa7f0) at 0x134d0f0
REFCNT =
FLAGS = (SMG,POK,pPOK)
IV = 0
NV = 0
PV = 0x1347970 "\320\300\304\310\323\321 \316\320\300\312\313"\0
CUR = 12
LEN = 16
MAGIC = 0x1358290
MG_VIRTUAL = &PL_vtbl_mglob
MG_TYPE = PERL_MAGIC_regex_global(g)
MG_LEN = -1