I don't know, this works fine for me:
$s = pack "L", 0x03141593; # 4 byte string $p = pack "P", $s; # pointer printf "%08X\n", unpack "L", unpack "P4", $p;
Result:
03141593
It would appear to me that your code looks fine.

update No it doesn't. pack "P", $string returns a packed pointer, i.e. a 4 byte structure, containing a packed integer with the numeric pointer value. unpack "P", $packed_pointer does the reverse: first unpacking the address from the 4 bytes, and then unpacking the string it points at. So it does a double unpack.

$s = "Kettle Of Fish"; # 14 byte string $p = pack "P", $s; # packed pointer printf "Length of the pointer: %d\n", length $p; printf "10 byte string: %s\n", unpack "P10", $p;
Result:
Length of the pointer: 4
10 byte string: Kettle Of 

In summary: you'd have to convert your numerical pointer to a packed integer, and then unpack using "P4". You can then unpack the integer in that string.

$int= unpack "L", unpack "P4", pack "L", $addr;

In reply to Re: unpack "P" and a horrible death by bart
in thread unpack "P" and a horrible death by theorbtwo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.