Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Data::Dumper with unicode

by bash (Scribe)
on Oct 11, 2016 at 06:54 UTC ( [id://1173700]=perlquestion: print w/replies, xml ) Need Help??

bash has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

Is it still possible to dump unicode data with Data::Dumper in native characters not in hex-codes?

Until recent I've used trick with redefining Data::Dumper::qquote. But looks like new Data::Dumper uses XS qquote() that doesnt allow redefining or Im doing something wrong.

Replies are listed 'Best First'.
Re: Data::Dumper with unicode
by haukex (Archbishop) on Oct 11, 2016 at 07:57 UTC

    Hi bash,

    I haven't yet looked at the internals of Data::Dumper, but on the surface it seems that it does always escape Unicode. Which kind of makes sense to me, because its purpose is to output Perl data in Perl's syntax, which doesn't support Unicode characters in its source, at least not without utf8. Which leads me to ask what you're trying to accomplish overall, since there might be a better solution than Data::Dumper?

    Regards,
    -- Hauke D

      Only for debugging So data should be human readable. Yep, i can use Data::Printer. But it isn't core and requires many other packages.

        Hi bash,

        Only for debugging So data should be human readable.

        I think that's a tricky situation, because if you're debugging, it may be important to be able to see the differences between, for example, various Unicode whitespace characters. I suspect you only want to prevent certain characters from being escaped? That means you'd have to pick and choose.

        Personally I don't find Data::Printer's dependency tree to be too bad. But just in case you're looking for something else, it might be possible to roll your own with Data::Dump::Filtered:

        use warnings; use strict; use open qw/:std :utf8/; my $str = "\$\x{2142}\x{1D1A}\x{018E}\x{0500}\x{2003}\""; use Data::Dump 'pp'; use Data::Dump::Filtered qw/add_dump_filter/; add_dump_filter( sub { my ($ctx, $objref) = @_; if ($ctx->is_scalar) { #TODO: this doesn't escape, not really that great! return { dump => "\"$$objref\"" }; } return; # normal dumping } ); print '$str=',pp($str),";\n";

        Hope this helps,
        -- Hauke D

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1173700]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found