perldebuggingunicodeutf-8data-dumper

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


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?


Solution

  • 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 = {
              "да" => "не"
            };
    
    ---
    да: не
    
    {
      "да" => "не"
    }