It took me a long time to see the cause of this, (but it was processing 1 GB of data), and I thought it might prove interesting/amusing to others.

Greatly simplified from the original that was processing data from a file, this program never terminates. Can you see why?

#! perl -slw use strict; my @bits = unpack '(a3)*', 'abcdefghijklmnopqrstuvwxyz'; 1 while "@bits[3,5,7]" =~ m[vwx]g;

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re: Program enters infinite loop, can you see why?
by ikegami (Patriarch) on Dec 07, 2010 at 03:58 UTC

    Each pass, the interpolation creates a new scalar to match against. Since the scalar has never been matched against, the match starts from the beginning of the string.

    It's an easy mistake to make, especially since string literals don't look like functions.

      (Ray's lightbulb comes on.) Thanks Ikegami! This explanation makes best sense.
Re: Program enters infinite loop, can you see why?
by JavaFan (Canon) on Dec 07, 2010 at 10:09 UTC
    It's one of the cases where quoting your variables can have an unexpected effect:
    $x = "abc"; say 1 while $x =~ /c/g; say 2 while "$x" =~ /c/g;
    will print a single 1, and an unlimited number of 2s.
Re: Program enters infinite loop, can you see why?
by Anonymous Monk on Dec 07, 2010 at 03:56 UTC
    Because "@bits[3,5,7]" always includes vwx (7)

      By that logic, this should also hang, but it doesn't:

      C:\test>perl -wE"1 while 'vwx' =~ m[vwx]g;"

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        interpolation always interpolates

      That doesn't really explain anything. In the following, what you said applies to $x, yet it doesn't loop forever.

      #! perl -slw use strict; my @bits = unpack '(a3)*', 'abcdefghijklmnopqrstuvwxyz'; my $x = "@bits[3,5,7]"; 1 while $x =~ m[vwx]g;
Re: Program enters infinite loop, can you see why?
by pajout (Curate) on Dec 07, 2010 at 10:14 UTC
    The condition of the cycle is NOT changed due one loop...