I'm using a subroutine I found as part of a program to convert a file with thousands of IP ranges to the minimum number of CIDR blocks. I have tested it and can't find a situation where it doesn't work.

The problems is I would like to understand HOW IT WORKS. Here is the declaration, calling statement and the subroutine:

my (@chunks, @fbits, @lbits); ..... ..... do_chunk(\@chunks, \@fbits, \@lbits); .... .... sub do_chunk { my ($chunks, $fbits, $lbits) = @_; my (@prefix, $idx1, $idx2, $size); # Find common prefix. After that, next bit is 0 for $fbits and 1 for # $lbits is 1. A split a this point guarantees the longest suffix. $idx1 = 0; $idx1++ while ($idx1 <= $#$fbits and $$fbits[$idx1] eq $$lbits[$idx1]); @prefix = @$fbits[0 .. $idx1 - 1]; $idx2 = $#$fbits; $idx2-- while ($idx2 >= $idx1 and $$fbits[$idx2] eq '0' and $$lbits[$idx2] eq +'1'); # Split if $fbits and $lbits disagree on the length of the chunk. if ($idx2 >= $idx1) { $size = $#$fbits - $idx1; do_chunk ($chunks, $fbits, [ @prefix, (split //, '0' . '1' x $size) ]) +; do_chunk ($chunks, [ @prefix, (split //, '1' . '0' x $size) ], $lbits) +; } else { $size = $#$fbits - $idx2; push @$chunks, [ (bits2num( [ @prefix, (split //, '0' x $size) ])), @$ +fbits - $size ]; } }

Now for my questions:

  1. What does the "\" do in "\@" in the calling arguments?
  2. In the subroutine I'm confused by the use of $#$, $$ and @$
  3. Last (for now) since this subroutine recursively calls itself, how will the second call shown below ever get executed?
if ($idx2 >= $idx1) { $size = $#$fbits - $idx1; do_chunk ($chunks, $fbits, [ @prefix, (split //, '0' . '1' x $size) ]) +; do_chunk ($chunks, [ @prefix, (split //, '1' . '0' x $size) ], $lbits) +; }

Thanks in advance


In reply to Total Syntax Confusion Plus by RobertJ

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.