in reply to convert encoding

First of all, I would question why you are using Data::Dumper at all, as there are formats that lend themselves to serialization a bit better, like JSON, YAML, or XML. If you could explain a bit more about what you need this for, and also show an SSCCE so we could see the problem for ourselves, we could maybe suggest a better solution.

The traditional way to convert the output of Data::Dumper back into Perl data is using eval.

However, the following can pose a large security risk as eval will execute any code you give it!

use open qw/ :std :utf8 /; # for printing to console my $data = q{ "Nach R\x{fc}cksprache mit" }; print "<$data>\n"; my $str = eval $data; print "<$str>\n";

One way one might try to lower the security risk is Safe, but configuring that is a bit tricky, and it is still possible to accidentally leave a door open for an attacker.

Replies are listed 'Best First'.
Re^2: convert encoding
by Corion (Patriarch) on Feb 23, 2017 at 09:54 UTC

    There also is Data::Undump, which parses a (simple) Data::Dumper string and returns the resulting data structure without needing eval.

Re^2: convert encoding
by rumpumpel1 (Novice) on Feb 23, 2017 at 10:20 UTC

    I'm using Data::Dumper because I have a complex XML structure which I need to have in a human readable format. Data::Dumper does a very nice job except for the encoding.

      I'm using Data::Dumper because I have a complex XML structure which I need to have in a human readable format.

      Data::Dumper is really more of a debugging aid than a pretty-printer. You could try out some other modules, such as Data::Printer. See also Data::Dumper with unicode for a question quite similar to yours.

      use open qw/ :std :utf8 /; use Data::Printer; my $str = "Nach R\x{fc}cksprache mit"; p $str;

      Next time, please provide all the information that you have split into multiple posts in this thread into a single post. I know what I mean. Why don't you?

Re^2: convert encoding
by rumpumpel1 (Novice) on Feb 23, 2017 at 10:09 UTC

    your code (on Windows) produces instead of the 'ü' a strange looking character which I can not show here.

    The encoding seems to have changed, but not in the intended way.

      This specific problem can be addressed by
      binmode STDOUT, ':encoding(cp437)';
      (The number is referring to the code page, and has to be adjusted according to your configuration. The DOS command chcp can tell you which code page is in use in your CLI)

      I admit I did only test my code on Linux (where it produces the "ü").

      However, given your post here, I am wondering whether Data::Dumper is the best choice. If you could explain what you are trying to accomplish in that code, that would help us help you. See also How do I post a question effectively?