Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Infinite m//g ??

by hookbot (Acolyte)
on May 02, 2001 at 01:17 UTC ( #77134=perlquestion: print w/replies, xml ) Need Help??

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

    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.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://77134]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2021-06-23 12:49 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (120 votes). Check out past polls.