'perlop' says (*emphasis mine*):perl -we'use strict;use P; use Types::Core qw(typ); my $re = qr{ab}; P "re(=%s), has ref %s, and type %s", $re, ref $re, typ $re; ' re(=(?^:ab)), has ref Regexp, and type REGEXP
Going by:Binary "=~" binds a *scalar expression* to a *pattern match*.
It must be the case that both m{} and qr{} are *both* pattern matches. Isn't a "pattern match" a "regular expression pattern"?perl -we'use strict;use P; my $str="part"; my @match_m = $str =~ m{^(.).*?(.)}; my @match_qr = $str =~ qr{^(.).*?(.)}; sub p_Ar($) { P "#=%d, content=(%s)", scalar(@{$_[0]}), $_[0]}; + P "res1:%s\nres2:%s", p_Ar \@match_m, p_Ar \@match_qr; ' res1:#=2, content=(['p', 'a']) res2:#=2, content=(['p', 'a'])
The fact that the =~ treats them the same, but the 'g' flag only works on one of them seems counter-intuitive. I realize that "behind the scenes", documentation says they "don't", but why, might not, the 'g' flag apply to sub-pattern, so, at least, things like "\G" would work inside "qr"? (Note, \G is is defined as a legal zero width assertion that appears "usable" in a "qr" pattern (but I'm not sure to what effect w/o "(?g)).
After more than a bit of experimenting, I found '\G' is usable, but a bit awkward to use inside 'qr' op, since it only will work when wrapped with an 'm{}', though even there, for some reason, it returns an extra pair of matches that contain undef:
> perl -we'use strict; use P qw(:undef="(undef)"); my $qr_string = q((?:\G(\w)\W{2}(\w))*); my $qr = qr[$qr_string]; my $base_pat=q(p--t); sub p_Ar($) { P "#=%d, content=(%s)", scalar(@{$_[0]}), $_[0]}; our (@match_mstr , @match_mqr , @match_qr , @match_qr2 , @tst_names); @tst_names = (qw(mstr mqr qr qr2)); local * p_matches; *p_matches = sub ($) { no strict "refs"; $_ = $base_pat x $_[0]; @match_mstr = $_ =~ m{$qr_string}g; @match_mqr = $_ =~ m{$qr}g; @match_qr = $_ =~ $qr; @match_qr2 = $_ =~ qr{$qr}; P qq(For str="%s:\n).(qq(%10s:%s\n) x @tst_names), $_ , (map { ("res_".$_, p_Ar(\@{"match_".$_})) } @tst_names); 0; }; my $c=1; while (3>=$c) { p_matches($c++) } ' For str="p--t: res_mstr:#=4, content=(['p', 't', (undef), (undef)]) res_mqr:#=4, content=(['p', 't', (undef), (undef)]) res_qr:#=2, content=(['p', 't']) res_qr2:#=2, content=(['p', 't']) For str="p--tp--t: res_mstr:#=6, content=(['p', 't', 'p', 't', (undef), (undef)]) res_mqr:#=6, content=(['p', 't', 'p', 't', (undef), (undef)]) res_qr:#=2, content=(['p', 't']) res_qr2:#=2, content=(['p', 't']) For str="p--tp--tp--t: res_mstr:#=8, content=(['p', 't', 'p', 't', 'p', 't', (undef), (unde +f)]) res_mqr:#=8, content=(['p', 't', 'p', 't', 'p', 't', (undef), (unde +f)]) res_qr:#=2, content=(['p', 't']) res_qr2:#=2, content=(['p', 't'])
ARG!!!... I'm getting a headache.
p.s. -- how does one get unicode characters to display in '<code>'?
Those ∄'s (∄), above, are ugly (supposed to be symbol for "There does not exist", i.e.: undef. --- which seems to display ok in normal text, but not inside a '<code>' block. *sigh*
UPDATE: removed/replaced version of code that used the default undef (∄) symbol to use "(undef)" instead.
In reply to Re^2: 'g' flag w/'qr'
by perl-diddler
in thread 'g' flag w/'qr'
by perl-diddler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |