IMHO this code example in perlre#Assertions is wrong or am I missing something?

Note that the rule for zero-length matches is modified somewhat, in that contents to the left of \G is not counted when determining the length of the match. Thus the following will not match forever:

$str = 'ABC'; pos($str) = 1; while (/.\G/g) { print $&; }
It will print 'A' and then terminate, as it considers the match to be zero-width, and thus will not match at the same position twice in a row.

The while loop tries to match $_ not $str.

so this works like it should

use warnings; $str = 'ABC'; pos($str) = 1; while ($str=~/.\G/g) { print $&; }

BUT what's really confusing me is that pos($str) is empty afterwards!

Now appending this code to the latter

print pos($str); #line6 while ($str=~/.\G/g) { print $&; }

produces  Use of uninitialized value in print at ... line 6. and an endless loop!

OK ... now at the same time running this code:

use warnings; $str = 'ABC'; print pos($str); # pos($str) = 1; while ($str=~/.\G/g) { print $&; }

produces

Use of uninitialized value in print at /home/lanx/B/PL/PM/re_escG.pl l +ine 2. Use of uninitialized value $& in print at /home/lanx/B/PL/PM/re_escG.p +l line 5.
but no endless loop!

Sorry???

if pos() is uninitialized why does it produce in one case an endless loop and in another it doesn't?

Cheers Rolf

UPDATE: Yes, I know that:

Currently, the "\G" anchor is only fully supported when used to anchor to the start of the pattern.(perlretut)
... but pos() is defined to be the position after the last match, thus in our case with a final \G in the pattern it shouldn't change at all... IMHO there's no reason why this shouldn't work... and most probably this bug in pos is the source of the problem!

In reply to [bugs?] perldoc perlre, \G and pos() by LanX

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.