The feature is that perl returns one element for each capture group to preserve numbering (so $tmp[0] is the same as $1, $tmp[1] is $2 etc ...).
For example with my ($_a, $_b, $_c) = "ac" =~ /(a)?(b)?(c)?/;, you'll have $_c = 'c' and 'b' undef.
Removing the extra undef at the end of the list, or even in the middle for some special cases could have been possible, but it is actually fairly easy to do it yourself: @tmp = grep defined, /(a)?(b)?(c)?/; so the decision has been to provide the result with all the available information (undef matches at the end indicate that some groupes have not matched, this may be useful information) and let the user decide what to do with it, rather than choose on the user's behalf what information is useful or not.

Edit: so in your case (without the implicit match on $_), the solution would be: my @tmp = grep defined, "list $_[0]\n" =~ /^list\s+(\w+)(?:,(\w+))*$/m;


In reply to Re: Surprising capture of undef with zero repetitions by Eily
in thread Surprising capture of undef with zero repetitions by bengo

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.