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.

In reply to Re: Pack/Unpack with B32 and related query by pg
in thread Pack/Unpack with B32 and related query by Anonymous Monk

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.