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;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|