So to get both pieces of information I have to call unpack twice?
my $len = unpack 'C/a*', $packed; my ($data) = unpack 'C/a*', $packed;
No... you might have missed that spot in the docs that say "back up a byte". I will use it to effectively unpack the same byte twice, with one unpack(), and one template.
print join ":", unpack 'CXC/a*', $packed;
resulting in
19:the quick brown fox
Another option, though less user-friendly, would be to use the '@' template, which you can use to reposition the current pointer — think of it as a form of seek().
print join ":", unpack 'C@0C/a*', $packed;
the "0" being the absolute position — here, the start of the string. Actually, it turns out that if you omit it, zero is used as a default.

The user-unfriendlyness is in the fact that you need to know the exact absolute position to seek to — making it impractical to encorporate it in the middle of a larger template — and not a relative one. Well, that's what the "X" is for, if you want to go backwards, and "x", to go forward.

print join ":", unpack 'C@C/a*X15a5x7a*', $packed;
which results in:
19:the quick brown fox:quick:fox

In reply to Re: unpack 'C/a*' and context weirdness by bart
in thread unpack 'C/a*' and context weirdness by BrowserUk

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.