Well it doesn't explicitly break the recursion (it just doesn't recurse anymore, but in the end that's quite the same). I'd say the trick is that grep checks all elements of @_, while your test on $_[0][0] only checks the first array (which happens to be the first to be empty, you're actually lucky that this array was the smallest, with [1,2,3,4,5,6],[1,2],[1,2,3],[1,2,3,4,5] your code might have worked.

And no, map in void context isn't so bad. It's just considered bad practice, often the reason given is that it's wasteful to build a new list (the output of map) just to throw it away, but also most of the time you can replace it by a for loop, which makes the intent clearer (map indicates that you want to transform a list, while for indicates that you want to iterate over it). map {print shift @$_} @_; for example can be translated to print shift @$_ for @_;


In reply to Re^3: understand and prevent 'Out of memory!' during sub recursion by Eily
in thread understand and prevent 'Out of memory!' during sub recursion by Discipulus

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.