strange output of pack in perl 5.8.0

D

Dotan Halevi

Hi all.
I had tried these two one liners over several perl distributions (perl
version 5.6 and down, OS = Linux , Solaris, Windows) and got the same GOOD
results :
___________ test #1 ___________________
$ perl -e 'print(pack("v", 0x1234))'| hexdump
0000000 1234
0000002
________________ test #2 _______________
$ perl -e 'print(pack("v", 0xabcd))'| hexdump
0000000 abcd
0000002
_____________ end ______________________

but on RedHat Linux 9.0 , whence
$ rpm -q perl
perl-5.8.0-88

Test # 2 produces these strange results :

__________ test #2 over RedHat 9.0, perl 5.8.0-88 __________
$ perl -e 'print(pack("v", 0xabcd))'| hexdump
0000000 8dc3 abc2
0000004
___________ end ________________________

What is wrong here ???


Thanks, Dotan
 
B

Brian McCauley

Dotan said:
__________ test #2 over RedHat 9.0, perl 5.8.0-88 __________
$ perl -e 'print(pack("v", 0xabcd))'| hexdump
0000000 8dc3 abc2
0000004
___________ end ________________________

What is wrong here ???

It would appear that somewhere between pack() and STDOUT it's getting
utf8 encoded.

I assue that the output of pack() is a byte string but when you pass it
to a utf8 encoded filehandle it interprets the bytes of the string as
Unicode code-points.

I think this has to do with some sort of locale setting since I don't
see in on my (non-Redhat) Linux box.

I'd guess it's fixable by explicitly saying binmode(STDOUT) but I can't
check since I can't reproduce the problem.
 
D

Dotan Halevi

Brian, thanks.
I'm using a non-US locale, and the binmode did the magic:

$ perl -e 'binmode(STDOUT);print(pack("v", 0xabcd))'| hexdump
0000000 abcd
0000002

Dotan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top