Here's some example code that might help you understand.

#!/usr/bin/perl $x ="somestringyoulike"; $y = pack("p", $x); # Null terminated $z = pack("P",$x); # Fixed length print '$x length : ', length($x), "\n"; print '$y length : ', length($y), " (it's a pointer.)\n"; print '$z length : ', length($y), " (it's a pointer.)\n"; print 'unpack $y : ', unpack("p",$y), "\n"; $x = "a new string"; print 'unpack $y : ', unpack("p",$y), "\n"; # $y still points to $ +x print '$z 1 byte : ', unpack("P",$z), "\n"; # The first byte of $ +z print '$z 4 bytes: ', unpack("P4",$z), "\n"; # The first 4 bytes. print '$z 17 bytes: ', unpack("P17",$z), "\n"; # The whole 17 bytes. # Remember that $y is null terminated... so there must be # a null in the string when we unpack 17 bytes of $z # because they point to the same thing. This finds that null... $s = unpack("P17", $z); $s =~ /(.)\0(.)/; print '\0 between: ', "$1 and $2\n";
-sauoq
"My two cents aren't worth a dime.";

In reply to Re: Using pack with P and p? by sauoq
in thread Using pack with P and p? by diotalevi

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.