For those of you (I admit I am one) who want to write s/this/that/sexgizmo, I'm sorry, I've not named this modifier /z. Rather, it is /G (unless Perl5-Porters says nay).

Here's how it's useful:

while (/name:\s*(\w+)\s+age:\s*(\d+)/g) { my ($name, $age) = ($1,$2); # ... } ### becomes while (my ($name, $age) = /name:\s*(\w+)\s+age:\s*(\d+)/G) { # ... }
What the /G modifier does is FORCE scalar context on a /.../g (global) match (so that pos() can be set accordingly), yet allow you to do this global match in list context. Now, if you've ever done a global match in list context, you know that it goes through all the matches. That means you can't write:
while (my ($name, $age) = /name:\s*(\w+)\s+age:\s*(\d+)/g) { # ... }
and expect it to work how you'd like. So, use /G there and your problems are solved.

Look for this new modifier in Perl 5.8!

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: New regex modifier!
by John M. Dlugosz (Monsignor) on Sep 12, 2001 at 01:53 UTC
    Why doesn't the /g modifier-ed match just return the matches? It would be the empty list, false, if it didn't match; or have it detect whether called in scalar or list context?

      In list context, //g returns all the matches. This new modifier will only match once, but the next time, it'll start attempting to match where the last one stopped. It's like doing:
      while (/pattern/g) { my ($x, $y, $z) = ($1, $2, $3); # ... }
      except that you can combine the regex and the assignment.

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

        Ah, now I get it. It doesn't "force scalar context" because it's returning a list, but makes it match once like /g does in scalar context.