Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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 problems if you associate it with the regexp. Read on for some problems.


xism options are toggles. Their absence signify the opposite of their presense. If g were an option, its absence would mean "don't loop".

Given that g must apply to the entire match or substitute operation. It makes no sense for it to affect only a part of it, so
$re = qr/.../g; /$re/; and
$re = qr/.../; /$re/g; would make no sense.
You can't both loop and not loop.

Only
$re = qr/.../g; /$re/g; and
$re = qr/.../; /$re/; would make sense, so
nothing would be gained.


There's another subbtle, but ugly problem. Consider

$str = 'Nothing but perl can parse Perl'; print($str =~ /perl/gi ?1:0,"\n"); # Prints '1' print($str =~ /noth/gi ?1:0,"\n"); # Prints '0' print($str =~ /perl/i ?1:0,"\n"); # Prints '1' print($str =~ /noth/i ?1:0,"\n"); # Prints '1'

Also consider

$str = 'Nothing but perl can parse Perl'; while (/perl/gi) { # Loops twice } while (/perl/i) { # Loops forever }

A match in scalar context using g and one not using it are simply not interchangeable. If g were to be a modifier on compiled regexps, allowing compiled regexps to be used in a match operator in scalar context would severly weaken that code.

Obviously, disallowing compiled regexp in a match in scalar context is not acceptable. So what's the solution? An operator that compiles a regexp for scalar context and one that compiles a regexp for list context? yuck! It's simpler to let the user handle g.

my @matches = ($g ? /$re/g : /$re/);

or

for (...) { my ($re, $g) = @$_; my @matches = ($g ? /$re/g : /$re/); ... }

Finally, consider

>perl -le"print qr/.../s" (?s-xim:...)

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.


In reply to Re: The 'g' modifier in compiled regex by ikegami
in thread The 'g' modifier in compiled regex by naikonta

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-24 11:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found