pack for 0x0A creates the invalid value

Posted by takajun on 2018-06-24 05:23

I created the following perl source'

my $val = 10;
open(OUT, ">", "out.bin") || die "cannot open file\n";
print OUT pack("C", $val);
close(OUT);

I expect the content of out.bin is 0x0A, but it is 0x0A0D.
"0x0D" is added after 0x0A.
I also get 0x000A0D in case of pack("S", $val), and 0x0000000A0D in case of pack("V", $val).

I can get the correct result in case of $val=9 or $val=11.

Is this a bug for ActivePerl?
Tested with 5.26.1 for x64, 5.26.0 for x86, and 5.24.2 for x86.

Regards,
Jun TAKADA

grahams
ActiveState Staff
Mon, 2018-06-25 10:10

If you're passing binary data you need to use raw mode for your filehandle. As it is, you're getting CR/LF translation.
https://perldoc.perl.org/functions/binmode.html