Yes, you are right. In your specific situation, the issue you were having can be cleared up by eliminating unnecessary capturing, which is what the (?:....) construct does. That's the optimal solution. But the implications of your question are what interested me, and why I responded with the example of how to turn off warnings. I was interested in responding to the question of what to do when you can't easily redesign the regular expression -- perhaps where the redesign would add unwanted complexity, for example.

My explanation was intended to explain one way to handle situations where an undefined backreference cannot be avoided simply by eliminating capturing. That is also a good trick to have up your sleeve. It is possible that you could have come to us with a regexp that couldn't have been easily simplified, in which case, just as in my example, another solution has to be found.

The example I provided showed a situation where an undefined backreference was an intentional component of the script's design, and couldn't be eliminated without unnecessarily complicating the script. In that sort of a situation, the warning generated is expected but unsightly. Since we know why we're getting the warning, and we know in the specific example that I provided that the warning isn't important, it's perfectly fine to simply shut it off in the narrowest possible scope. Warnings exist to let us know we might have just made a mistake in our code. But sometimes our judgment has to trump the warning.

Think of the warnings as being similar to your car's navigation system. It might tell you to take exit 15B, but you may know something the nav system doesn't; that there's road work at 15B. So you take exit 14 instead. The GPS complains, and you press the mute button, or the detour button to silence it. And you end up getting home faster, having avoided the construction delay. Lexical scoping for warnings exists for exactly this reason. There are times where you, the programmer, want to do something that usually would be construed as a mistake, but in your particular case happens to be exactly what you need and intend to do. When this happens, press mute (shut off the warning, in the narrowest possible scope).

Thanks again for the question: I love these ones that generate some food for thought.


Dave


In reply to Re^3: undefined backreferences by davido
in thread undefined backreferences by frasco

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.