http://qs321.pair.com?node_id=11115271


in reply to Convert JSON to Perl and back with unicode

After some source diving into the (Perl) source of the pure Perl implementation of Data::Dumper, and some confusion, because usually Perl uses the XS version of Data::Dumper, which behaves differently in this case, I found the following approach works. It monkey-patches Data::Dumper (instead of inheriting from it, because Data::Dumper isn't written that way):

#!perl use strict; use warnings; use utf8; use charnames ':full'; # just so I can use ASCII to represent Greek le +tters use Data::Dumper; $Data::Dumper::Useperl = 1; $Data::Dumper::Useqq='utf8'; sub Data::Dumper::qquote { local($_) = shift; s/([\\\"\@\$])/\\$1/g; return qq("$_") unless /[[:^print:]]/; # fast exit if only printabl +es # Here, there is at least one non-printable to output. First, trans +late the # escapes. s/([\a\b\t\n\f\r\e])/$Data::Dumper::esc{$1}/g; # no need for 3 digits in escape for octals not followed by a digit. s/($Data::Dumper::low_controls)(?!\d)/'\\'.sprintf('%o',ord($1))/eg; # But otherwise use 3 digits s/($Data::Dumper::low_controls)/'\\'.sprintf('%03o',ord($1))/eg; # all but last branch below not supported --BEHAVIOR SUBJECT TO CH +ANGE-- my $high = shift || ""; if ($high eq "iso8859") { # Doesn't escape the Latin1 printables if ($Data::Dumper::IS_ASCII) { s/([\200-\240])/'\\'.sprintf('%o',ord($1))/eg; } elsif ($] ge 5.007_003) { my $high_control = utf8::unicode_to_native(0x9F); s/$high_control/sprintf('\\%o',ord($1))/eg; } } elsif ($high eq "utf8") { # Some discussion of what to do here is in # https://rt.perl.org/Ticket/Display.html?id=113088 # use utf8; # $str =~ s/([^\040-\176])/sprintf "\\x{%04x}", ord($1)/ge; } elsif ($high eq "8bit") { # leave it as it is } else { s/([[:^ascii:]])/'\\'.sprintf('%03o',ord($1))/eg; #s/([^\040-\176])/sprintf "\\x{%04x}", ord($1)/ge; } return qq("$_"); }; my $pv = { "\N{GREEK SMALL LETTER ALPHA}\N{GREEK SMALL LETTER BETA}" => "\N{G +REEK CAPITAL LETTER GAMMA}\N{GREEK CAPITAL LETTER DELTA}" }; print Dumper $pv;