The first byte of $packed is the length, and the rest is the string. Just as if you had initialized $packed like so:
my $s = 'the quick brown fox'; $packed = pack 'Ca*', length $s, $s;

To unpack it, leave out the '/'

my ($len, $val) = unpack 'Ca*', $packed; print "$len:$value"; __END__ output: 19:the quick brown fox

If it is a bug it's a documentation bug; the meaning is very unclear. Using the '/' is only documented in pack, not in unpack - but it behaves differently in unpack than pack. With 'C/a*' as the template, unpack returns that many bytes. IE, whatever 'C/' represents will be how long a string is returned. Change it to 5 when you unpack it and you'll just get "the q".

With pack, it will prepend the length of the item packed.

Update: I missed the point about scalar versus list context. That is weird - probably a bug! From the pack docs:

The *length-item* is not returned explicitly from "unpack".
In scalar context, the length-item is returned (using AS 5.6.1 build 633).

In reply to Re: unpack 'C/a*' and context weirdness by jsprat
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.