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

Hello perl-diddler,

If you look at the documentation for qr//, you’ll see that the /g modifier is not supported:

qr/STRING/msixpodualn
perlop#Regexp-Quote-Like-Operators

Which makes sense: qr turns STRING into a regular expression, which may then be used in any number of m{...} and s{...}{...} constructs. The appropriate place to add a /g modifier is at the point of use:

use strict; use warnings; use P; my $re = qr{ (\w+) }x; my $dat = "Just another cats meow"; my @matches = $dat =~ /$re/g; P "#matches=%s, matches=%s", scalar(@matches), \@matches; exit scalar(@matches);

Output:

12:53 >perl 1645_SoPW.pl #matches=4, matches=["Just", "another", "cats", "meow"] 12:54 >

Update:

P.S. - I also just noticed that in addition to stripping out the 'g' option, the 'x' option doesn't seem to work in the regex's parens, i.e. - (?x).

I don’t understand what you’re saying here. Can you give some example code?

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: 'g' flag w/ 'qr'
by perl-diddler (Chaplain) on May 29, 2016 at 07:56 UTC
    In regards to your question: " P.S. - I also just noticed that in addition to stripping out the 'g' option, the 'x' option doesn't seem to work in the regex's parens, i.e. - (?x).

    I don’t understand what you’re saying here. Can you give some example code?"

    Please see my response to the 1st response above. Using the original data from the original question, (?x) doesn't parse the same as when it is used as a suffix. It misses the 1st match, which at the least, is counter-intuitive, if not broken, no?