in reply to 'g' flag w/'qr'

The short answer for why is that m// is a different command compared to m//g (resp. s/// vs s///g ).

The /g variants are for repetitive matches / looping !

It's comparable to switching between if and while , which are obviously different beasts.

If you look at other languages like eg javascript¹ you'll notice that they are implemented using different keywords.

( I wished the perldocs were clearer here because it's causing confusion)

Furthermore are regular expressions meant to be combinable/nestable , but a sub expression with /g doesn't make any sense.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

¹) couldn't reproduce this easily, JS is very close to Perl in many aspects,

I had probably another language in mind... Python, PHP?

Replies are listed 'Best First'.
Re^2: 'g' flag with 'qr' (different commands) (updated)
by perl-diddler (Chaplain) on May 30, 2016 at 01:47 UTC
    You said: "short answer for why is that m// is a different command compared to m//g".

    I was noting the similarity between these forms:

    my $foo="regexp"; my @matches = $foo =~ /(re)ge(xp)/; #and my $regex=qr{(re)ge(xp)}; my @matches = $foo =~ $regex;
    In both of the above, "@matches" will get 2 entries "re" and "xp". I find it unfortunate that if your regexp was doubled, there is no way to directly use qr w/'g' i.e. one must use the m{} or // type form as in:
    > perl -we'use strict;use P; my $regex=qr{(re)ge(xp)}; my @matches0 = "regexpregexp" =~ /(re)ge(xp)/g; my @matches = "regexpregexp" =~ $regex; my @matches1 = "regexpregexp" =~ /$regex/g; P "matches=%s, 0=%s, 1=%s", \@matches, \@matches0, \@matches1; ' matches=["re", "xp"], 0=["re", "xp", "re", "xp"], 1=["re", "xp", "re", + "xp"]
      I find it unfortunate ...

      Sometimes life's like that.

      So far, you've only advocated the utility and desirability of code like
          my $regex = qr{...}g;
          ...;
          ... $string =~ $regex;
      in which
          ... $string =~ $regex;
      would be equivalent to the current usage
          ... $string =~ /$regex/g;
      given that  qr//g is not now supported.

      I think you need to consider the implications of the cases raised by haukex here. These (and I'm sure many other) cases would need to be given defined behaviors. Surely, you do not intend the  qr//g feature to apply only to the  $string =~ $regex case? If so, this feature would seem to fall squarely into the "some developer's personal 'pet feature'" category.


      Give a man a fish:  <%-{-{-{-<

      > I find it unfortunate that. ..

      I don't.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!