7

我在结构中有一些 UTF-8 编码的字符串,出于调试目的,我使用Data::Dumper转储这些字符串。

一个小测试用例是:

use utf8;
use Data::Dumper;
say Dumper({да=>"не"}

它输出

{
  "\x{434}\x{430}" => "\x{43d}\x{435}"
};

但我想看看

{
  "да" => "не"
};

当然,我的结构要复杂得多。调试时如何使转储结构中的字符串可读?也许我之前必须通过chr某种方式处理输出warn/say

4

3 回答 3

7

仅用于调试:

#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
use utf8;
use Data::Dumper;
binmode STDOUT, ':utf8';

CASE_1: {
    # Redefine Data::Dumper::qquote() to do nothing
    no warnings 'redefine';
    local *Data::Dumper::qquote = sub { qq["${\(shift)}"] };
    # Use the Pure Perl implementation of Dumper
    local $Data::Dumper::Useperl = 1;

    say Dumper({да=>"не"});
}

CASE_2: {
    # Use YAML instead
    use YAML;
    say Dump({да=>"не"});
}

CASE_3: {
    # Evalulate whole dumped string
    no strict 'vars';
    local $Data::Dumper::Terse = 1;

    my $var = Dumper({да=>"не"});
    say eval "qq#$var#" or die $@;
}

__END__
$VAR1 = {
          "да" => "не"
        };

---
да: не

{
  "да" => "не"
}
于 2018-05-24T13:53:41.840 回答
1

print Dumper(%mydata) =~ s/\\x\{([0-9a-f]{2,})\}/chr hex $1/ger;

于 2020-01-24T09:13:47.023 回答
-1

抱歉,但我已经测试了 eval 整个转储,并且对我的数据有些反感,所以

Data::Dumper->new(\@_)
  ->Indent(1)->Sortkeys(1)->Terse(1)->Useqq(0)->Dump
  =~ s/((?:\\x\{[\da-f]+\})+)/eval '"'.$1.'"'/eigr;
于 2018-10-11T05:39:48.467 回答