|Perl: the Markov chain saw|
Re: Pack/unpack - understanding the '@' and '.' templactesby 7stud (Deacon)
|on Mar 31, 2013 at 10:01 UTC||Need Help??|
Also note you can set the input string's pointer at any position at any time with @:
@29 A3 @0 A3 @39 A3
Here's a full example:
I understand what C/xa is doing,...
Maybe not. The 'C' reads the first byte from the input string and treats it as an integer. After that read, the position pointer for the string advances to the start of the second byte. Next, '/x' says to use the integer on top of the stack as the repeat count for 'x'. What the heck is the stack and where does it come from? According to the docs, the stack is...
…an internal stack of integer arguments unpacked so far..
That means if you use the template 'C2 /A' on the string '\001\002hello', then the stack looks like this:
The first integer that was unpacked, 1, was placed on top of the stack, then the second integer that was read, 2, was placed on top of the stack. Subsequently, the '/A' pops the value off the top of the stack, i.e. 2, and instead of being inserted into the results array the 2 becomes the repeat count for the 'A':
So what happens when you apply the template 'C/xa' to the string '\003\003\003abcd'? The 'C' says to read one byte from the input string and treat it as an integer. After that read, the position pointer for the input string advances to the start of the second byte, and the 3 from the first byte is placed on top of 'the stack':
\003\003\003abcd ^ | position pointer after reading 'C' stack: 3
Then '/x' pops the 3 off the stack and uses it as the repeat count for the 'x', and as a result the 3 doesn't get inserted into the results array. Then because 'x' means to skip a byte and the repeat count is 3, unpack() skips 3 bytes in the input string starting at the location of the position pointer:
\003\003\003abcd ^ | position pointer after reading a 'C' \003\003\003abcd ^ | position pointer after reading 'C/x'Finally, the 'a' in the template reads the character 'b'.
perldoc pack and perlpacktut have the briefest mention of "@" and (to my eyes) no mention of ".".
Whoever wrote the pack tutorial and pack docs should be shot. I vote for deleting the current pack tutorial and pack docs, and starting with what Loops wrote.