Perl 使用调试器';s x指令,用于精确打印非ASCII字符串
[注意:这个问题是我的一个问题的衍生。在我发布之前的问题时,我没有意识到它实际上问了两个不同的问题。多亏了tripleee的评论,我找到了其中一个问题的答案。下面的问题仅限于剩下的问题。]Perl 使用调试器';s x指令,用于精确打印非ASCII字符串,perl,debugging,unicode,utf-8,output,Perl,Debugging,Unicode,Utf 8,Output,[注意:这个问题是我的一个问题的衍生。在我发布之前的问题时,我没有意识到它实际上问了两个不同的问题。多亏了tripleee的评论,我找到了其中一个问题的答案。下面的问题仅限于剩下的问题。] 我将使用下面的简短演示脚本来说明这个问题 # -*- coding: utf-8 -*- use strict; use feature 'unicode_strings'; use POSIX 'locale_h'; use locale; use utf8; setlocale( LC_CTYPE,
我将使用下面的简短演示脚本来说明这个问题
# -*- coding: utf-8 -*-
use strict;
use feature 'unicode_strings';
use POSIX 'locale_h';
use locale;
use utf8;
setlocale( LC_CTYPE, 'de_DE.UTF-8' );
binmode( STDOUT, ':utf8' );
my $non_ascii = 'ßäöüÄÖÜ';
print "$non_ascii\n";
my @non_ascii = split //, $non_ascii;
print "$_\n" for @non_ascii;
$DB::single = 1; 1; # return control to DB
(最后一行实际上是一个断点。)
好的,现在我在Perl调试器下运行它:
% perl -d dbtest.pl
Loading DB routines from perl5db.pl version 1.37
Editor support available.
Enter h or 'h h' for help, or 'man perldebug' for more help.
main::(dbtest.pl:8): setlocale( LC_CTYPE, 'de_DE.UTF-8' );
DB<1> c
ßäöüÄÖÜ
ß
ä
ö
ü
Ä
Ö
Ü
main::(dbtest.pl:17): $DB::single = 1; 1; # return control to DB
DB<1>
但是如果我现在使用x
指令漂亮地打印@non_ascii
数组的内容,则输出不再正确:
DB<3> x \@non_ascii
0 ARRAY(0x7fab6196f790)
0 '\337'
1 '\344'
2 '\366'
3 '\374'
4 '\304'
5 '\326'
6 '\334'
DB<4> binmode( STDOUT, ':utf8' );
DB<5> x \@non_ascii
0 ARRAY(0x7fab6196f790)
0 '\337'
1 '\344'
2 '\366'
3 '\374'
4 '\304'
5 '\326'
6 '\334'
DB<6>
DB x\@非ascii码
0阵列(0x7fab6196f790)
0 '\337'
1 '\344'
2 '\366'
3 '\374'
4 '\304'
5 '\326'
6 '\334'
DB二进制模式(标准输出“:utf8”);
DB x\@非ascii码
0阵列(0x7fab6196f790)
0 '\337'
1 '\344'
2 '\366'
3 '\374'
4 '\304'
5 '\326'
6 '\334'
分贝
如何获得
x
以产生人类可读的输出?好的,我想出来了。解决办法是
DB<10> binmode( $DB::OUT, ':utf8' )
DB<11> x \@non_ascii
0 ARRAY(0x7fdae891e2a8)
0 'ß'
1 'ä'
2 'ö'
3 'ü'
4 'Ä'
5 'Ö'
6 'Ü'
DB<12>
DB-binmode($DB::OUT':utf8')
DB x\@非ascii码
0阵列(0x7fdae891e2a8)
0 'ß'
1 'ä'
2 'ö'
3 'ü'
4 'Ä'
5 'Ö'
6 'Ü'
分贝
可以将binmode($DB::OUT,':utf8')
设置放在自己的.perldb
中,使其持久化
DB<10> binmode( $DB::OUT, ':utf8' )
DB<11> x \@non_ascii
0 ARRAY(0x7fdae891e2a8)
0 'ß'
1 'ä'
2 'ö'
3 'ü'
4 'Ä'
5 'Ö'
6 'Ü'
DB<12>