我在结构中有一些 UTF-8 编码的字符串,我使用 Data::Dumper 转储这些字符串以进行调试。
一个小测试用例是:
use utf8;
use Data::Dumper;
say Dumper({да=>"не"}
输出
{
"\x{434}\x{430}" => "\x{43d}\x{435}"
};
但我想看看
{
"да" => "не"
};
当然我的结构要复杂得多。 如何在调试时使转储结构中的字符串可读? 也许我必须在
chr
之前通过 warn/say
处理输出?
仅用于调试:
#!/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 = {
"да" => "не"
};
---
да: не
{
"да" => "не"
}
print Dumper(%mydata) =~ s/\\x\{([0-9a-f]{2,})\}/chr hex $1/ger;
@ernix 提醒我,在这种情况下,我们可能会给 Data::Dumper“旧的举升”,而是使用,
use JSON::PP;
use utf8;
sub Dumper { JSON::PP->new->pretty->utf8->encode(shift); }
my %hash = ( да => "не" );
die Dumper \%hash;
这给出了,
{
"да" : "не"
}
抱歉,但我已经测试了 eval 整个转储,并且对我的数据有些反感,所以
Data::Dumper->new(\@_)
->Indent(1)->Sortkeys(1)->Terse(1)->Useqq(0)->Dump
=~ s/((?:\\x\{[\da-f]+\})+)/eval '"'.$1.'"'/eigr;