如何用 Data::Dumper 显示可读的 UTF-8 字符串?
How to display readable UTF-8 strings with Data::Dumper?
我在结构中有一些 UTF-8 编码的字符串,我将这些字符串转储用于使用 Data::Dumper 进行调试。
一个小测试用例是:
use utf8;
use Data::Dumper;
say Dumper({да=>"не"}
输出
{
"\x{434}\x{430}" => "\x{43d}\x{435}"
};
但我想看
{
"да" => "не"
};
当然我的结构要复杂得多。
如何在调试时使转储结构中的字符串可读?也许我必须在 warn/say
?
之前以某种方式通过 chr
处理输出
仅用于调试:
#!/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 = {
"да" => "не"
};
---
да: не
{
"да" => "не"
}
抱歉,我已经测试了 eval 整个转储并且对我的数据有些反感所以
Data::Dumper->new(\@_)
->Indent(1)->Sortkeys(1)->Terse(1)->Useqq(0)->Dump
=~ s/((?:\x\{[\da-f]+\})+)/eval '"'..'"'/eigr;
print Dumper(%mydata) =~ s/\x\{([0-9a-f]{2,})\}/chr hex /ger;
我在结构中有一些 UTF-8 编码的字符串,我将这些字符串转储用于使用 Data::Dumper 进行调试。
一个小测试用例是:
use utf8;
use Data::Dumper;
say Dumper({да=>"не"}
输出
{
"\x{434}\x{430}" => "\x{43d}\x{435}"
};
但我想看
{
"да" => "не"
};
当然我的结构要复杂得多。
如何在调试时使转储结构中的字符串可读?也许我必须在 warn/say
?
chr
处理输出
仅用于调试:
#!/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 = {
"да" => "не"
};
---
да: не
{
"да" => "не"
}
抱歉,我已经测试了 eval 整个转储并且对我的数据有些反感所以
Data::Dumper->new(\@_)
->Indent(1)->Sortkeys(1)->Terse(1)->Useqq(0)->Dump
=~ s/((?:\x\{[\da-f]+\})+)/eval '"'..'"'/eigr;
print Dumper(%mydata) =~ s/\x\{([0-9a-f]{2,})\}/chr hex /ger;