Syntactic Confectionery Delight | |
PerlMonks |
Re^2: The 'g' modifier in compiled regexby naikonta (Curate) |
on Apr 09, 2007 at 08:16 UTC ( [id://608938]=note: print w/replies, xml ) | Need Help?? |
Basically, it boils down to: g affects the operation (match or substitution), not the regexp. There's going to be problem is you associate it with the regexp. Read on for some problems.
Well, I am aware of this issue. I was wrong about wondering whether /g was for substitution or for matching. I was wrong because /g is for both, for the action (as also pointed out by japhy), but I never thought that /g was for the regex. When you said xism are toggles, I looked again at perlre and there under "(?imsx-imsx)" entry it says, "One or more embedded pattern-match modifiers, to be turned on (or turned off, if preceded by "-")". I should have thought that they were toggles in the first place if I read more carefully earlier. Thanks for this :-) But in this case, /g is also a toggle for the normal m// or s///, right? Notice how the s doesn't affect the (?:...), only what's in it? That means $re = qr/.../g; @matches = /$re/; makes no sense since part of the regexp doesn't loop ((?:...)) and part of it does (...). Again, we'd need to do $re = qr/.../g; @matches = /$re/g;, gaining nothing. Well, that's for the current implementation. But, what if I *know* that I will use the qr/pat/g in list context for the rest of the code? And when I use it in scalar context then it's my fault. If /g is allowed in qr//, it will surely be accompanied by a note something like "only use it in list context, otherwise there will be mandatory warning your code will be ignored", or even, "..., otherwise it's compilation error". I don't dive into the source code or the p5p archive. I don't know how exactly the regex precompilation is implemented, or how it will be affected should /g is implemented in the precompiled regex (if it's possible at all). Now, you just tempted me to sense the tendention to request this feature: allow global matching modifier in the qr// so it will always match globally whenever that compiled regex is used. :-)
In Section
Seekers of Perl Wisdom
|
|