使用调试器的x指令来打印非ASCII字符串 [英] Using debugger's x directive to pretty-print non-ASCII strings

查看:161
本文介绍了使用调试器的x指令来打印非ASCII字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[注意:此问题是我的早期问题的分支。当时我发布了这个早期的问题,我没有意识到它实际上提出了两个不同的问题。感谢三等人的评论,我想出了两个人之一的答案。下面的问题限制了剩下的问题。]

[NOTE: this question is a spinoff of an earlier question of mine. At the time I posted that earlier question I did not realize that it actually asks about two different issues. Thanks to a comment by tripleee, I figured out the answer to one of the two. The question below limits itself to the remaining problem.]

我将使用下面的简短演示脚本来说明问题。

I'll use the short demo script below to illustrate the problem.

# -*- 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

(最后一行实际上是一个断点。)

(The last line is effectively a breakpoint.)

确定,现在我运行在Perl调试器下:

OK, now I run this under the Perl debugger:

% 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> 

到目前为止,脚本产生了预期的输出,现在调试器已经进入单引号,

So far so good: the script produced the expected output, and now the debugger has entered into single-step mode, and is waiting for input.

此时, print 仍然会产生正确的(即,可读)输出:

At this point, print still produces the correct (i.e. human-readable) output:

  DB<1> print $non_ascii
ßäöüÄÖÜ
  DB<2> print "$_\n" for @non_ascii
ß
ä
ö
ü
Ä
Ö
Ü
  DB<3> 

但是,如果我现在使用 x 要漂亮打印 @non_ascii 数组的内容,输出不再是正确的:

But if I now use the x directive to pretty-print the contents of the @non_ascii array, the output is no longer looks right:

  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> 

如何获得 x 可读输出?

推荐答案

好的,我想出来了。解决方案是

OK, I figured it out. The solution is

  DB<10> binmode( $DB::OUT, ':utf8' )
  DB<11> x \@non_ascii
0  ARRAY(0x7fdae891e2a8)
   0  'ß'
   1  'ä'
   2  'ö'
   3  'ü'
   4  'Ä'
   5  'Ö'
   6  'Ü'
  DB<12>

可以把 binmode($ DB :: OUT,':utf8 ')设置在 .perldb 中使其持续存在。

One can put the binmode( $DB::OUT, ':utf8' ) setting in one's .perldb to make it persistent.

这篇关于使用调试器的x指令来打印非ASCII字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆