Frage

I use Devel::REPL. When dumping structures it currently uses some escapes like \x{17d} for accented characters in strings. How can I make it directly output UTF-8 instead? Here’s an example:

$ re.pl
$ {'žůžo' => ['bříško']}
$HASH1 = { "\305\276\305\257\305\276o" => [ "b\305\231\303\255\305\241ko" ] };

Interesting thing is that simply entering the string outputs it directly:

$ re.pl
$ 'žůžo'
žůžo

This should be the “bare bones” version:

$ perl -MDevel::REPL -e 'my $repl = Devel::REPL->new; $repl->load_plugin("DDS"); $repl->run'
$ {'žůžo' => ['bříško']}
$HASH1 = { "\305\276\305\257\305\276o" => [ "b\305\231\303\255\305\241ko" ] };

So it looks like Data::Dump::Streamer is the culprit, but I didn’t get further. I have found some discussion about Unicode in Data::Dumper over at Perl Monks, but that obviously doesn’t help here. Cursory glance at Data::Dump::Streamer documentation didn’t reveal anything interesting as far as Unicode or monkey patching go.

War es hilfreich?

Lösung 2

After reading the source of DDS, I came up with this patch based on the Perl Monks code:

$ cat quote.rc
{
    no warnings 'redefine';
    sub Data::Dump::Streamer::_qquote {
        my $s = shift;
        return "'$s'";
    }
}
$ re.pl --rcfile ./quote.rc
$ {'žůžo' => ['bříško']}
$HASH1 = { 'žůžo' => [ 'bříško' ] };

I have no idea what could get broken by the change, but since my use case is quite simple, I’m happy with this hack. If my house burns down because of it, I’ll post an update.

Andere Tipps

You can't, at least for cases where Devel::REPL calls Data::Dump::Streamer. DDS will always encode any characters not in the \x20-\x7e range.

You could modify DDS to have an option to do otherwise.

The dumper has no way to know your string (C5 BE ...) is the UTF-8 encoding of Unicode code points. Specifically:

  1. It has no way to know that the string represents text.
  2. Even if it did, it has no way to know the text is encoded.
  3. Even if it did, it has no way to know the encoding used is UTF-8.

So you're asking for something very specific to your data structure. You could make a customized plugin based on Devel::REPL::Plugin::DDS.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top