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

I have the following error and I'm not sure what's causing it. Any ideas? Substitution loop at elists.cgi line 165. Line 165 is the 2nd line in this conditional statement::
if ($FORM{'from'} ne $theword) { $FORM{'from'} =~ s/($sep|^\s*)//sg; $FORM{'from'} =~ s/(\0|`|\.?\.\\|\.?\.\/)//sg; $FORM{'fstname'} =~ s/($sep|^\s*|^htm$|\s*$)//sg i +f $FORM{'fstname'}; $FORM{'fstname'} =~ s/(\0|`|\.?\.\\|\.?\.\/)//sg; }
Any help would be greatly appreciated. Thanks.

Replies are listed 'Best First'.
Re: Substitution Loop
by blakem (Monsignor) on Dec 08, 2001 at 01:32 UTC
    As a quick fix, you might want to use \s+ instead of \s* in all of the above regexes... Replacing "0 or more spaces" with the empty string is pointless and wasteful.... there are "0 or more spaces" between each and every character in your string!! Use \s+ "1 or more spaces" instead. It might also be the cause of that error, but I can't be sure.
    # This: s/\s*//; # is better written as: s/\s+//;

    -Blake

Re: Substitution Loop
by danboo (Beadle) on Dec 08, 2001 at 00:48 UTC
    From 'perldoc perldiag':

    Substitution loop (P) The substitution was looping infinitely. (Obviously, a substitution shouldn't iterate more times than there are characters of input, which is what happened.) See the discussion of substitution in the section on Quote and Quote-like Operators in the perlop manpage.

    What is the value of $sep when you get this warning?

    Cheers,

    - danboo

      $sep = |(pipe symbol)
      $FORM{'from'} = (an email address)

      I'm not sure what is going on here. Still trying to figure it out.
Re: Substitution Loop
by RandallB (Initiate) on Jan 08, 2007 at 16:15 UTC
    I've run into the same kind of thing, but it seems size-related in my case. I'm munging through several overly large (1-2GB) PST and MDB files, trying to do plaintext searches. When I do some pre-emptive cleanup, I get the error; I'm sure there's a better way to do this, but...
    local $/ ; open FILE, $file ; my $data = <FILE> ; #yes, I have enough memory close FILE ; $data =~ s/[^[:ascii:]]+//g ; while ($data =~ /($regex)/g) { ... }
    Any MS Office files under the 1GB mark seem to happily go through the cleanup - others die miserably with the [seemingly] unrecoverable "Substitution loop in..." error.
    v5.8.8 built for x86_64-linux-thread-multi
      Ditto. This seems like perl must keep a loop counter on substitutions to detect the error, because I'm doing this on a 1.5GB file, and it errors. Split that in half, and it passes (doing this on 64-bit perl for memory needs). cat the first half back onto itself (to make sure it is just duplicate of the first and not a source of the error) to get it back up to 1.5GB, fails. -JM