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

Recently we came across a script on our system that began hanging when we ported it to perl 5.6.0 (our system is currently running 5.005). We tracked it down and found that there was one regular expression that was causing the trouble. We've simplified the regex down to a one liner.
perl -e '$_ = "a"; while (m/(\G|^)(a)/g) {print "[$2]\n";}'

We corrected the code to look like (in essense) the following (because (\G|^) is redundant):
perl -e '$_ = "a"; while (m/(\G)(a)/g) {print "[$2]\n";}'

My question is, why does the first example hang under perl 5.6.0. Is there anybody with insight into what changed in the engine. Although the first regex is a bad example of what to do, this still seems like a bug in 5.6.0.

I'm posting this both to find an answer, and to invite more discussion on "\G" in regex's for newbies.



my @a=qw(random brilliant braindead); print $a[rand(@a)];

Edit: chipmunk 2001-05-15

Replies are listed 'Best First'.
Re: regex (\G) vs. (^\G)
by chipmunk (Parson) on May 15, 2001 at 20:24 UTC
    Yep, this is a bug alright. It's hit a lot of programmers, unfortunately.

    This bug was created by an optimization in the regular expression engine. As I understand it, here's the basic idea: if the regex contains \G, then the engine doesn't need to worry so much about the value of pos() when it's setting up for the match, because that'll be taken care of when the \G is matched.

    Unfortunately, the optimization doesn't account for the \G being optional; when that's the case, then the value of pos() isn't taken into account and the regex matches the same part of the string over and over again.

    The best way to avoid the bug is to make sure when you use \G that any successful match will include \G.

    BTW, your first example doesn't actually hang; it goes into an infinite loop, printing [a] endlessly.

      Good point about the hang. On our system where we discovered the problem it did actually hang.

      Thanks for the info -- that answer seems to make a lot of sense, and looks like a rather big caveat for regex writers.

      my @a=qw(random brilliant braindead); print $a[rand(@a)];
Re (tilly) 1: regex (\G) vs. (^\G)
by tilly (Archbishop) on May 15, 2001 at 20:18 UTC
    This is a bug. It came up before at Infinite m//g ??. I think it is reported somewhere, but if an upgrade to 5.6.1 doesn't fix it, and nobody volunteers a bug ID, it is certainly worthy of a perlbug report...
      Um..Yeah. I need to stop posting things. hookbot works at my company. This is an issue we discovered. I've been busy and haven't had time to post it. Looks like he did (without mentioning to anybody). Sorry for the wasted time on this post. Look at Infinite m//g ?? instead (and send any "++" to hookbot).

      my @a=qw(random brilliant braindead); print $a[rand(@a)];
Re: regex (\G) vs. (^\G)
by Rhandom (Curate) on May 15, 2001 at 20:02 UTC
    By the way, the title is supposed to be
    regex (\G) vs. (^|\G)
    Rather than
    regex (\G) vs. (^\G)
    But there is nowhere to go back and edit the title (feature request).

    my @a=qw(random brilliant braindead); print $a[rand(@a)];