in reply to Re^2: undefined backreferences
in thread undefined backreferences
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
|
|---|