Thread ISO und UTF (10 answers)
Opened by FunBruno at 2016-11-24 13:14

Raubtier
 2016-11-24 21:50
#185659 #185659
User since
2012-05-04
1054 Artikel
BenutzerIn

user image
Lies https://perlgeek.de/de/artikel/charsets-unicode

Dort wird dir die Benutzung von encode&decode erkärt. Du musst den String also mit dem Encoding "MIME-Header" decoden. Anschließend musst du bestimmen, in welcher Kodierung der String rausgeschrieben werden soll.

Also wenn du UTF-8 rausschreiben willst:
Code (perl): (dl )
1
2
use Encode;
say encode("UTF-8", decode("MIME-Header", "=?UTF-8?Q?Schl=C3=A4fst_du_unter_L=C3=84RM=3F_W?= =?UTF-8?Q?ow_respect_?="))'


Wenn du unter Windows bist, willst du vermutlich eher in cp1252 rausschreiben:
Code (perl): (dl )
1
2
use Encode;
say encode("cp1252", decode("MIME-Header", "=?UTF-8?Q?Schl=C3=A4fst_du_unter_L=C3=84RM=3F_W?= =?UTF-8?Q?ow_respect_?="))'


Oder gar in cp850:
Code (perl): (dl )
1
2
use Encode;
say encode("cp850", decode("MIME-Header", "=?UTF-8?Q?Schl=C3=A4fst_du_unter_L=C3=84RM=3F_W?= =?UTF-8?Q?ow_respect_?="))'



Welches davon das richtige ist, hängt davon ab, was du machen willst. Es könnte auch sein, dass du erst einmal gar kein encode machen willst und mit dem Textstring weitermachen willst. Das wird dir aber hoffentlich nach Lektüre des verlinkten Artikels klar.

Von "use bytes", wie von Janus vorgeschlagen, würde ich allerdings dringend abraten. Perldoc:bytes sagt dazu:
Quote
This pragma reflects early attempts to incorporate Unicode into perl and has since been superseded. It breaks encapsulation (i.e. it exposes the innards of how the perl executable currently happens to store a string), and use of this module for anything other than debugging purposes is strongly discouraged. If you feel that the functions here within might be useful for your application, this possibly indicates a mismatch between your mental model of Perl Unicode and the current reality.

Last edited: 2016-11-24 21:51:51 +0100 (CET)

View full thread ISO und UTF