Open
Description
Where
https://perldoc.perl.org/perlpacktut
Description
Issue 1:
The example at the end of "The Basic Principle" packs "byte contents from a string of hexadecimal digits".
The code is pack( 'H2' x 10, 30..39 )
. It is not really straightforward to see 30 as a "hexadecimal digits".
Why making it unnecessarily confusing?
The following would be easier for beginners, avoiding "misunderstanding", which is the purpose of this tutorial.
my $s = pack( 'H2' x 10, '30'..'39');
print "$s\n";
Issue 2:
Since there are unicode strings and byte strings, it is not clear what can be unpacked. It seems unpacking unicode strings may have unexpected result.
#!/usr/bin/perl -w
use v5.34;
use utf8;
use strict;
use warnings;
use Encode qw(encode decode);
my $s = "0123456789😀";
my $b = encode "UTF8", $s;
say "Unpack unicode string 1: ", unpack( '(H2)*', $s);
say "Unpack unicode string 2: ", unpack( 'H*', $s);
say "Unpack bytes: ", unpack( 'H*', $b);
{
use bytes;
say "Unpack unicode string 3: ", unpack( 'H*', $s);
}
The output is:
Character in 'H' format wrapped in unpack at .\t.pl line 11.
Unpack unicode string 1: 3031323334353637383900
Character in 'H' format wrapped in unpack at .\t.pl line 12.
Unpack unicode string 2: 3031323334353637383900
Unpack bytes: 30313233343536373839f09f9880
Unpack unicode string 3: 30313233343536373839f09f9880