Nowadays I'd only use $1 etc. if they are used within something like 5-10 lines of the regex. Even then I often find myself writing my ($foo,$bar) = $str =~ /^(\w+)\s+(.+)/; instead of using $1 and friends. I think the only time I've used $1 recently is in a construct like if (/^\w+\s+(.+)/) { my $foo = $1; .... In your example case, I would almost certainly use named capture groups, since it looks like the regexes are defined nowhere near the $1 variables; I'd say there's too much of a risk of someone accidentally adding a capture group somewhere and throwing off all of the indicies of the other groups. Named capture groups can also help someone trying to understand a regex later on. So in general, I find having things named, especially stored in lexical variables (i.e. watched by strict), is always preferable from a maintenance standpoint. I usually only back down from that when the scripts are really compact and/or it's a throwaway script. As for performance, you know what they say about optimization ;-)


In reply to Re: Named captures or positional variables by Anonymous Monk
in thread Named captures or positional variables by MidLifeXis

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.