in reply to Substitution Loop

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