LanX has asked for the wisdom of the Perl Monks concerning the following question:
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:
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.$str = 'ABC'; pos($str) = 1; while (/.\G/g) { print $&; }
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
but no endless loop!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.
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!
|
|---|