Says kokr:
In scalar context m//g doesn't return size of the list it would give back when called in list context,
It's not supposed to do that. m//g in scalar context has a very interesting result:
my $s = "123 45 6 789"; while ($s =~ m/\d+/g) { print "> $&\n"; }
This prints:
> 123 > 45 > 6 > 789
2.The \G anchor is useless - perlre states, that it should be used only at the beginning of pattern,
It's not useless. If you change the pattern in the example above to /\G\d+/g you get a different result. But here's a typical example of how one might use \G:
my $s = "123 carrots 45 6 bananas 789"; while (1) { $s =~ /\G(\d+)/gc and print "NUMBER $1\n" and next; $s =~ /\G\s+/gc and print "SPACE\n" and next; $s =~ /\G([a-z]+)/gc and print "WORD $1\n" and next; $s =~ /\G$/gc and last; }
This prints:
NUMBER 123 SPACE WORD carrots SPACE NUMBER 45 SPACE NUMBER 6 SPACE WORD bananas SPACE NUMBER 789
What happens if you remove the 'useless' \G's? You get a very different result:
NUMBER 123 NUMBER 45 NUMBER 6 NUMBER 789
3.and most important - m//g in scalar context doesn't reset pos (matches only once).
Here you have some confusion, but I can't tell what it is amongst the other confusions in your articles. Did you realize that the /./g in print "#7: ",   /./g; was in list context, not scalar context? Did you realize that assigning $_ = "123" will reset pos($_)? Did you realize that m//g isn't supposed to 'reset' pos unless the match fails? In fact, the whole point of /g is that it does not reset pos. Ordinary matches, without /g, reset pos before matching begins.

Consider this:

my $s = "123 carrots 45 6 bananas 789"; while ($s =~ /(\d+)/g) { print "'$1' at position ", pos($s)-length($1), "\n"; }
The output is:
'123' at position 0 '45' at position 15 '6' at position 18 '789' at position 29
So clearly pos is doing something. Now let's reset pos:
my $s = "123 carrots 45 6 bananas 789"; while ($s =~ /(\d+)/g) { print "'$1' at position ", pos($s)-length($1), "\n"; pos($s) += 13; }
Now the output is different:
'123' at position 0 '5' at position 16 '89' at position 30
The first match is as before. But the pos($s) += 13 forces the current match position forward, into the middle of the 45, so that the next match sees only the 5 part. After matching the 5, the next pos($s) += 13 jumps past the 6 entirely, into the middle of the 789.

I ask you if it's proper behaviour / undocumented feature / bug? Or maybe I have missed something?
A combination of all of these, I think. #1 is proper behavior. #2 seems to be a case of your having missed something. #3 is an undocumented feature, but it's undocumented because it doesn't exist. But also the behavior of \G and /g is very badly documented in general.

I hope this helps, but I'm not sure what your objection is, so I can't address it directly.

--
Mark Dominus
Perl Paraphernalia


In reply to Re: m//g behaves strange... by Dominus
in thread m//g behaves strange... by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.