Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 221
Беров Avatar asked May 23 '18 12:05

Беров


2 Answers

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

---
да: не

{
  "да" => "не"
}
like image 151
ernix Avatar answered Nov 15 '22 08:11

ernix


print Dumper(%mydata) =~ s/\\x\{([0-9a-f]{2,})\}/chr hex $1/ger;
like image 38
leszekrabka Avatar answered Nov 15 '22 08:11

leszekrabka