Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: The 'g' modifier in compiled regex

by naikonta (Curate)
on Apr 09, 2007 at 08:16 UTC ( [id://608938]=note: print w/replies, xml ) Need Help??


in reply to Re: The 'g' modifier in compiled regex
in thread The 'g' modifier in compiled regex

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. :-)

  • Comment on Re^2: The 'g' modifier in compiled regex

Replies are listed 'Best First'.
Re^3: The 'g' modifier in compiled regex
by rodion (Chaplain) on Apr 09, 2007 at 09:28 UTC
    Please don't request this feature. One of the big advantages of Perl is that, to a very large extent, it does what most programmers expect, when they think about it a little. As japhy's and ikegami's comments indicate, attaching looping behavior to a pre-compiled regex is difficult to make sense of, and it would prevent the same pre-compiled regex from being used in a looping and non-looping context, as your suggested error messages indicate.

    I'm confident in the Perl architects, and so I doubt they would add this type of feature, but I also think there is significant overhead to considering a suggestion. The way I see it, the more we exercise self-restraint in our suggestions, in response to feedback here in PM, the sooner we'll see a release of Perl 6.

      The way I see it, the more we exercise self-restraint in our suggestions, in response to feedback here in PM, the sooner we'll see a release of Perl 6.
      If they don't want new features past a certain date, then set that date, or suggest that it occurs. Last I checked, P6 will be done when it's done.

      I could see good coming out of suggesting something being put in place. It can cause a new idea for the architects to think about a happy medium between what the architects want and what he and some people like him expect.

Re^3: The 'g' modifier in compiled regex
by demerphq (Chancellor) on Apr 09, 2007 at 16:56 UTC

    You wont see /g being support by qr//'s. I think i can say that as a definitive statement. Part of the reason is that with the exception of the /o modifier then only modifiers allowed on a qr// are those that can apply to a subsection of a pattern. You have to ask yourself what would happen in a case like:

    my $qr_g=qr/foo/g; my $other=qr/balh $qr_g/;

    What would it mean to have half of a pattern be /g and the other half not? What sense would it make if somebody used $other specifically without /g?

    Also, the /g modifier is a property of the PMOP that will be executing a pattern like operator, whereas msix are properites of the resulting compiled pattern. In short they arent even stored in the same place.

    Forget the idea of qr//g, its not going to happen.

    ---
    $world=~s/war/peace/g

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://608938]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-26 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found