I'm only using _utf8_on in this example, in the original code, the string already had its utf8 flag on
utf8::upgrade and utf8::downgrade are the proper way to convert between internal encodings.
But I don't think using a string in pack should result in something that depends on the internal representation of the string : the internal representation should be internal :)
Exactly. In 5.10.0, exactly the same string is produced no mater what the internal encoding is.
use strict;
use warnings;
use Carp qw( croak );
sub avoid_utf8_internally {
my ($s) = @_;
utf8::downgrade($s, 1)
or croak("Non-bytes found in input");
return $s;
}
sub use_utf8_internally {
my ($s) = @_;
utf8::upgrade($s);
return $s;
}
my $file_num;
for my $s (
avoid_utf8_internally("bj\x{f6}rk"),
use_utf8_internally("bj\x{f6}rk"),
"b" x 1000,
) {
my $packed = pack("V/a*", $s);
printf("%s -> %s\n", length($s), length($packed));
open(my $fh, '>', 'packed'.++$file_num) or die;
binmode $fh; # No crlf mucking.
print $fh $packed;
}
>perl script.pl
5 -> 9
5 -> 9
1000 -> 1004
>debug packed1
-rcx
CX 0009
:
-d100 l9
0B14:0100 05 00 00 00 62 6A F6 72-6B ....bj.rk
-q
>debug packed2
-rcx
CX 0009
:
-d100 l9
0B14:0100 05 00 00 00 62 6A F6 72-6B ....bj.rk
-q
>debug packed3
-rcx
CX 03EC
:
-d100 3EC
0B14:0100 E8 03 00 00 62 62 62 62-62 62 62 62 62 62 62 62 ....bbbbb
+bbbbbbb
0B14:0110 62 62 62 62 62 62 62 62-62 62 62 62 62 62 62 62 bbbbbbbbb
+bbbbbbb
...
0B14:04D0 62 62 62 62 62 62 62 62-62 62 62 62 62 62 62 62 bbbbbbbbb
+bbbbbbb
0B14:04E0 62 62 62 62 62 62 62 62-62 62 62 62 bbbbbbbbb
+bbb
-q
5.8.8, on the other hand, *is* depended on the internal encoding.
>debug packed1
-rcx
CX 0009
:
-d100 l9
0B14:0100 05 00 00 00 62 6A F6 72-6B ....bj.rk
-q
>debug packed2
-rcx
CX 0009
:
-d100 l9
0B14:0100 05 00 00 00 62 6A C3 B6-72 ....bj..r
-q
I don't like how utf8 is handled in perl,
You shouldn't even have to know about the internal encoding. Fixing this is an ongoing process, and that's precisely why packed was changed in 5.10.0. Why are complaining about such a fix?!