hookbot has asked for the wisdom of the Perl Monks concerning the following question:

I have a line of perl like so:
perl -e '$_ = "a"; while (m/(?:\G|^)(a)/g) {print "[$1]\n";}'
but when I upgrade from 5.005 to 5.6.0, everything hangs. How can this happen, and why was this bug installed into Perl? Is it an attempt to punish people for using regex's styles that are supposedly depricated or something? I would think that most old perl code should also work on the new perl, and, at least, any pattern match with a "g" option should not cause an infinite loop, eh?

BTW, I just changed to this and it works on both perls:

perl -e '$_ = "a"; while (m/\G(a)/g) {print "[$1]\n";}'

Replies are listed 'Best First'.
(bbfu) Re: Infinite m//g ??
by bbfu (Curate) on May 02, 2001 at 02:17 UTC

    It seems to me that the RE engine, prefering earlier matches, would take the ^ every time. I'm surprised that it doesn't do that in 5.005.

    Of course, pos (which controls where \G matches) isn't explicitly documented as defaulting to 0. So I suppose it's a matter of which undocumented behaviour you want to rely on. ;-)

    Perhaps a documentation update is in order?

    Update: I stand corrected. :-) tilly tells me that /g matches are never supposed to start before the old match left off. Of course that makes sense. Not sure why I didn't realize that before. :-P

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.

Re (tilly) 1: Infinite m//g ??
by tilly (Archbishop) on May 03, 2001 at 06:47 UTC
    I have no clue whether this is known, but this looks a heck of a lot like a bug to me.

    A /g match is supposed to start where the previous one left off. Not before it.

    Also FYI the intent is to preserve backwards compatibility. If that didn't happen, it is generally by accident. (There are some exceptions to this rule where backwards compatibility is ruled not important enough, but they are rather few in number.)