++ :)

For those who are staring at this in confusion like I did a few minutes ago, '.' returns (adds to the stack) the current position in bytes relative to the start of the current () group. This is obvious (knowing what '.' does in the pack case) now that I've seen it but I didn't understand it on my own.

So to explain how the second template works (the left part, the right part is basically the same except instead of moving right a lot and then left a little, it moves left a little then right at lot, to avoid moving out of the string):

If L is the length of the data (2 in the case of "Hi"), the second byte will contain the value L+2 (1 for the type byte, 1 for the length byte, and L for the data). So:

One of the things that puzzled me is why the number after X was twice the number of bytes in the header. With a N bytes long header, we first move N times by reading them, then move L+N bytes because the length includes N. So we are indeed at position L+2N


In reply to Re^2: unpack less than indicated length by Eily
in thread unpack less than indicated length by paulrh

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.