in reply to Is the documentation for Perl 5.20 'pack' correct?
I have the following script which I ran on 2 different machines:
The results were:#!/usr/local/bin/perl # use strict; use warnings; use Config; print "Byte order: $Config{byteorder}\n"; # expect byteorder='43 +21' or '1234'; my $hexstring = "01020304"; # Hex number my $decnumber = hex $hexstring; print "Hex: $hexstring is Dec: $decnumber\n"; # expect 1690 +9060 my $netnumber = pack( "N", $decnumber ); print "Dec: $decnumber is 'N': '",unpack("H8",$netnumber),"' as Ne +twork or Big-endian\n"; my $Nonnetnumber = pack( "V", $decnumber ); # Vax or little-e +ndian print "Dec: $decnumber is 'V': '",unpack("H8",$Nonnetnumber),"' as + Vax or Little-endian\n"; $Nonnetnumber = pack( "L", $decnumber ); # as native format print "Dec: $decnumber is 'L': '",unpack("H8",$Nonnetnumber),"' as + native format\n";
The results agree with Writing endian-independent code in C. My knowledge of big-endian came from writing machine to machine transfer software for IBM in the '70s.On Aix 5.2 on RS/6000 ( powerpc-ibm-aix5.2.0.0 ) # pyrperl ./pack.pl Byte order: 4321 Hex: 01020304 is Dec: 16909060 Dec: 16909060 is 'N': '01020304' as Network or Big-endian Dec: 16909060 is 'V': '04030201' as Vax or Little-endian Dec: 16909060 is 'L': '01020304' as native format On Debian Linux on 64bit AMD ( Debian 4.4.5-8 ) # pyrperl ./pack.pl Byte order: 1234 Hex: 01020304 is Dec: 16909060 Dec: 16909060 is 'N': '01020304' as Network or Big-endian Dec: 16909060 is 'V': '04030201' as Vax or Little-endian Dec: 16909060 is 'L': '04030201' as native format
The background was that IBM 32-bit mainframes were big-endian and expensive, and the new in-expensive (???) 8-bit boxes were little-endian.
As you can see the 'N' and 'V' 'pack' formats are the same on both architectures but the native formats are different. Perl's 'pack' is correct, but the documentation examples are incorrect.
Regards...Ed
"Well done is better than well said." - Benjamin Franklin
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Is the documentation for Perl 5.20 'pack' correct?
by Anonymous Monk on Jul 07, 2015 at 23:27 UTC |