How to display readable UTF-8 strings with Data::Dumper?

2.6k Views Asked by At

I have some UTF-8 encoded strings in structures which I am dumping for debugging purposes with Data::Dumper.

A small test case is:

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

It outputs

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

but I want to see

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

Of course my structure is quite more complex. How can I make the strings in the dumped structure readable while debugging? Maybe I have to process the output via chr somehow before warn/say?

3

There are 3 best solutions below

4
On BEST ANSWER

Just for debugging:

#!/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 = {
          "да" => "не"
        };

---
да: не

{
  "да" => "не"
}
2
On

sorry but I had tested eval whole dump and had got some repugnancy for my data so

Data::Dumper->new(\@_)
  ->Indent(1)->Sortkeys(1)->Terse(1)->Useqq(0)->Dump
  =~ s/((?:\\x\{[\da-f]+\})+)/eval '"'.$1.'"'/eigr;
1
On

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