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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |