Re: convert encoding
by Discipulus (Canon) on Feb 23, 2017 at 09:24 UTC
|
| [reply] [d/l] [select] |
Re: convert encoding
by Corion (Patriarch) on Feb 23, 2017 at 09:17 UTC
|
See Encode - assuming that you have not added any decoding/encoding yet, you would decode the string on reading in and encode the string on output:
use Encode qw( decode encode );
my $str = decode('Latin-1', "Nach R\x{fc}cksprache mit");
# ... whatever
# Assume that STDOUT is UTF-8:
my $str_out = encode( 'UTF-8', $str );
print $str;
As you don't tell us the encoding of your input and the encoding you need in your output, it's hard to give more concrete advice. | [reply] [d/l] [select] |
|
|
| [reply] |
|
|
The output of Data::Dumper will always show Nach R\x{fc}cksprache mit because Data::Dumper tries to restrict its output to 7-bit ASCII, at least with some options.
If you want something else, please show a SSCCESCCEE, because I don't really understand what you're asking for and it's most easy to demonstrate in ten lines of code.
| [reply] [d/l] |
|
|
|
|
|
|
Re: convert encoding
by haukex (Archbishop) on Feb 23, 2017 at 09:51 UTC
|
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.
| [reply] [d/l] |
|
|
| [reply] [d/l] |
|
|
| [reply] |
|
|
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?
| [reply] [d/l] |
|
|
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.
| [reply] |
|
|
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)
| [reply] [d/l] [select] |
|
|
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?
| [reply] |
Re: convert encoding (retain encoding with eDumper)
by 1nickt (Canon) on Feb 23, 2017 at 11:52 UTC
|
use strict; use warnings; use feature 'say';
use Data::Dumper::AutoEncode;
use utf8;
my $str = 'Nach Rücksprache mit';
say Dumper $str;
say eDumper $str;
__END__
Output:
$ perl 1182603.pl
$VAR1 = "Nach R\x{fc}cksprache mit";
$VAR1 = 'Nach Rücksprache mit';
(But as others have said, Data::Dumper is not usually the best choice for data serialization or generating final program output.)
Hope this helps!
The way forward always starts with a minimal test.
| [reply] [d/l] [select] |