sparky8342 has asked for the wisdom of the Perl Monks concerning the following question:

Can anyone explain the following behaviour?

#!/usr/bin/perl sub extract { my($i,$y) = @_; print "Unpack $i: " . (join ',',unpack('w*',$y)) . "\n"; } $x=4294967296; foreach(1..9) { $z=pack('w*',$x); &extract($_,$z); }
Output:

Unpack 1: 2082839,9,0,0,0
Unpack 2: 34126102543,0,0,0
Unpack 3: 34126037007,0,0,0
Unpack 4: 34126037007,0,0,0
Unpack 5: 34126037007,0,0,0
Unpack 6: 34126037007,0,0,0
Unpack 7: 34126037007,0,0,0
Unpack 8: 34126037007,0,0,0
Unpack 9: 34126037007,0,0,0
This only happens once $x >= 4294967296
Thanks.

Replies are listed 'Best First'.
Re: Unpack weirdness
by jmcnamara (Monsignor) on May 30, 2002 at 14:49 UTC

    It looks like a bug:
    $ perl5.6.0 -le 'print unpack "w", pack "w", 2**32' 2082834 $ perl5.7.2 -le 'print unpack "w", pack "w", 2**32' 4294967296

    --
    John.

      I do get the same results for 5.6.0 and 5.7.2, but if I go further back in time, I get different results. Note that 2 ** 32 is bigger than what fits in a 32 bit integer, so it could matter whether your version of Perl stores the value in an 64 integer, or a double.

      What follows are result of

      print unpack "w*", pack "w*", 2 ** 32;

      of all versions of Perl since 5.003_07 (the first version that produces any output).

      Abigail

        Just out of curiousity, could you go into the set up that produced these results? I'm imagining something in which you just type a few simple commands on a script and blammo.
        ()-()
         \"/
          `