in reply to Re: signed and unsigned templates in pack/unpack
in thread signed and unsigned templates in pack/unpack

The returned pid is used in a sub which checks if the pid already exists. Due the fact, pack and unpack using the same template but returning different values this is a big problem.

print pack ('N', -3544)."\n"; # gives 'ÿÿò(' print unpack ('N', 'ÿÿò(')."\n"; # gives '4294963752'

Shouldn't unpack return -3544 again?

Replies are listed 'Best First'.
Re^3: signed and unsigned templates in pack/unpack
by BrowserUk (Patriarch) on Mar 02, 2010 at 12:59 UTC

    You're packing a negative number with an unsigned template 'N', which means it will be treated as a large positive value. And when you unpack it, it will return that large positive value.

    The solution is don't use an unsigned template for packing numbers than can be negative. Use a signed template:

    $p = pack 'j', -3544;; print unpack 'j', $p;; -3544

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I see... Thanks a lot :-)