1 2 3 4 5 6
my $data = join "", @replaced_data; $data = Encode::decode("utf-8", $data); $data = substr( $data, 0, $maxlen ); use bytes; return $data . '…';
1 2 3 4
# Handlerroutine für Textinhalt registrieren $p->handler( text => sub { my $s = shift; push @replaced_data, $s; }, "text" # <--- Decodierung ist nicht mehr notwendig
2023-05-09T16:09:02 GwenDragonCode (perl): (dl )use bytes;
QuoteUse 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. In that case, you may wish to read some of the perl Unicode documentation: perluniintro, perlunitut, perlunifaq and perlunicode.
2023-05-10T05:19:43 rostiEin print Dumper \%INC zeigt mir auch daß bytes.pm per Default geladen wird.
1
2
3
4
5
6
7
8
9
10
11
12
$ perl -M5.020 -MData::Dumper -e 'say for keys %INC'
warnings/register.pm
strict.pm
Carp.pm
overloading.pm
bytes.pm
constant.pm
Data/Dumper.pm
warnings.pm
Exporter.pm
XSLoader.pm
$
QuoteIf 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.
2023-05-11T11:44:37 GwenDragonIst das so ein Perl-Geheimnis, das nur Leuten zuteil wird, die sehr oft auf Perl-COnventions/-Workshops waren?
Du hast Recht, Rosti, das ist auch auch ein ziemlich arschiger arroganter Hinweis.
Mir fällt dazu ein dem Kerl entgegen zu rufen: "WTF! Don't be a dick!"
2023-05-11T13:40:47 hajNein, das ändert auch nichts. Ist halt ein schlechter Stil, Nutzer so anzumotzen und ihr Denken mental quasi verdreht oder falsch zu nennen. Ich hab auch keinen Bock, mich mit männlichen Perl-Nerds anzulegen.Willst Du seine Mail-Adresse haben?
Quotemit bytes kann man "kaputte" Strings erzeugen, die weder als Characters noch als Bytes brauchbar sind.
1 2
my $out = encode "utf-8", "Ein '$ae_3' kostet 1$euro"; print encode "utf-8", $out;
1 2 3 4 5 6 7 8 9 10 11
use 5.020; my $ae_1 = chr 0xe4; my $ae_2 = "\N{U+e4}"; say '$ae_1 ist gleich $ae_2' if ($ae_1 eq $ae_2); { use bytes; print $ae_1; print $ae_2; }
QuoteDas ist schon etwas dreist, meinen Beispielcode so zu verändern, dass er zu Deinen Thesen passt.
1 2 3 4 5 6 7 8 9 10 11 12
use 5.020; use warnings; use Encode; my $ae_1 = chr 0xe4; print encode 'UTF-8', $ae_1; { use bytes; print $ae_1; }
1 2 3 4 5 6 7 8 9
use strict; use warnings; use Encode; my $c = chr 0xE4; my $cc = pack "U", 0xA4; print encode 'utf8', encode 'utf8', $c; # kaputt print "\n"; print encode 'utf8', encode 'utf8', $cc; # auch kaputt
2023-05-11T07:15:03 rostiAber vielleicht kannst Du ja mal erklären warum man Pragma bytes nur für Debugging verwenden soll und ansonsten nicht.
my $ae_1 = chr 0xe4;
my $ae_2 = "\N{U+e4}";
my $ae_3 = "\N{LATIN SMALL LETTER A WITH DIAERESIS}";
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
use 5.020; use Encode; my $ae_1 = chr 0xe4; my $ae_2 = "\N{U+e4}"; my $ae_3 = "\N{LATIN SMALL LETTER A WITH DIAERESIS}"; my $euro = "\N{EURO SIGN}"; { say "Ohne 'use bytes', mit Encode"; say "1 = 2" if $ae_1 eq $ae_2; say "2 = 3" if $ae_2 eq $ae_3; say encode 'UTF-8', " mit encode: ae_1 = $ae_1, ae_2 = $ae_2, ae_3 = $ae_3"; say encode 'UTF-8', " mit encode: Ein '$ae_1' kostet 1$euro"; say "-" x 50; } { use bytes; say "Mit 'use bytes'"; say "1 = 2" if $ae_1 eq $ae_2; say "2 = 3" if $ae_2 eq $ae_3; say "ohne encode: ae_1 = $ae_1, ae_2 = $ae_2, ae_3 = $ae_3"; say encode 'UTF-8', " mit encode: ae_1 = $ae_1, ae_2 = $ae_2, ae_3 = $ae_3"; say "ohne encode: Ein '$ae_1' kostet 1$euro"; say encode 'UTF-8', " mit encode: Ein '$ae_1' kostet 1$euro"; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
use strict; use warnings; # auf diese Art und Weise entstehen # alle Zeichen mit UTF-8-Kodierung # es gilt die Charactersemantic my $ae_1 = chr 0xe4; my $ae_2 = "\N{U+e4}"; my $ae_3 = "\N{LATIN SMALL LETTER A WITH DIAERESIS}"; my $euro = "\N{EURO SIGN}"; print $euro; # Wide character in print, Fehler! use bytes; # schalte um auf bytesemantic print $euro; # Ausgabe OK
2023-05-11T14:03:44 rosti... Das gilt auch für die Scriptdatei selbst, lenght "ä" liefert per Default also 2.
1 2 3 4 5 6
#! /usr/bin/env perl use strict; use warnings; use 5.010; say length("ä");
1
2
3
4
5
6
7
8
9
10
11
12
13
$ file -i /tmp/t.pl ; perl /tmp/t.pl
/tmp/t.pl: text/plain; charset=iso-8859-1
1
# Datei rekodiert zu utf-8
$ file -i /tmp/t.pl ; perl /tmp/t.pl
/tmp/t.pl: text/plain; charset=utf-8
2
# Datei editiert und ein "use utf8;" eingefügt
$ file -i /tmp/t.pl ; perl /tmp/t.pl
/tmp/t.pl: text/plain; charset=utf-8
1
1
2
3
4
$ file -i /tmp/t.pl; perl /tmp/t.pl
/tmp/t.pl: text/plain; charset=utf-8
Can't use global $^▒▒▒▒▒▒▒ in "my" at /tmp/t.pl line 9, near "my $▒"
Unrecognized character \xA4 in column 6 at /tmp/t.pl line 9.
1 2 3 4 5 6 7 8 9 10
#! /usr/bin/env perl use strict; use warnings; use 5.010; say length("ä"); my $ä = "test"; say $ä;
QuoteEs beweist vor allem, dass auch der Perl-Interpreter erfahren muss, welche Kodierung die Skriptdatei hat!
QuoteVor allem, wenn man anfängt, z.B. Sonderzeichen in Variablennamen zu verwenden....
1 2 3 4 5
use strict; use warnings; use Encode; my $cc = pack "CC", 195, 164; print encode "utf8", $cc; # Müll
1 2 3 4 5
use strict; use warnings; my $cc = pack "CC", 195, 164; use bytes; # mit oder ohne print $cc; # korrekt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
use strict; use warnings; use Encode qw( encode decode); use 5.010; # Byte Sequenz my $cc = pack "CC", 195, 164; # übernehme die Bytesequenz als utf8 kodierte Daten # encode() ist hier falsch! Siehe auch: https://metacpan.org/pod/Encode#TERMINOLOGY + https://metacpan.org/pod/Encode#encode + https://metacpan.org/pod/Encode#decode my $uu = decode('UTF-8', $cc); # Schalte Ausgabekanal auf utf8 binmode(*STDOUT, 'utf8'); # Terminal auf utf8 schalten; weil dieser Test auf Windows gemacht wurde, gleich eingebaut system("chcp 65001"); # Gebe das UTF8 Zeichen aus say $uu; # kein Müll