in reply to Pack/Unpack with B32 and related query

I came up this example, which could give a very intuitive answer:

use strict; use warnings; my $a = "abcdabcd"; { my $b = join('.',unpack('CCCC',$a)); print $b, "\n"; } { my $b = join('.',unpack('CCCC',substr($a,0,4))); print $b, "\n"; } { my $b = join('.',unpack('(CCCC)*',$a)); print $b, "\n"; } { my $b = join('.',unpack('(CCCC)*',substr($a,0,4))); print $b, "\n"; }

The output is:

97.98.99.100 97.98.99.100 97.98.99.100.97.98.99.100 97.98.99.100

Here is the explanation:

  1. The pattern in the first block says: take the first 4 bytes, and interprete each of them as unsigned char. The bold part is an implicit substr.
  2. The second block uses the same pattern, but it substr $a first. Base on the explanation of the first block, now we know that what the program does is "take the first 4 bytes of the first 4 bytes", which has no difference from 'take the first 4 bytes'. So this produces the same thing as case 1.
  3. Now we switch to a new pattern '(CCCC)*', which says repeatedly group every 4 consecutive bytes together. Without substr, as $a is 8 bytes long, it matches the pattern twice, or more precisely once but produces two groups. So it uses all 8 bytes.
  4. Same pattern as case 3, but substr'd down to 4 bytes first, now you only have enough bytes to match the pattern once and produce one group.