Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^6: Understanding pack and unpack changes for binary data between 5.8 and 5.10

by ikegami (Pope)
on Mar 13, 2009 at 15:10 UTC ( #750448=note: print w/replies, xml ) Need Help??


in reply to Re^5: Understanding pack and unpack changes for binary data between 5.8 and 5.10
in thread Understanding pack and unpack changes for binary data between 5.8 and 5.10

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?!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://750448]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2018-07-22 18:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (455 votes). Check out past polls.

    Notices?