On many occasions with CGI scripts, I would want to define a "custom HTML tag", the tag would just tell the CGI to run a subroutine to add a cool effect to the final page (for example "Welcome to my site!" would rainbowify the text between the two tags, using all the appropriate tags and stuff).
But whenever I try a while() loop on a (.*?) type of thing, it gets on a deep recursive loop (or at least that's what its symptoms are. It sits and devours CPU like there's no tomorrow).
For example, if I have code like this:
Code:
while ($string =~ /<rainbow>(.*?)<\/rainbow>/i) {<br /> # Get the text and rainbowify it.<br /> my $text = $1;<br /> my $rainbow = &rainbow($text);<br /><br /> # Filter the new text in.<br /> $string =~ s/<rainbow>$text<\/rainbow>/$rainbow/i;<br />}
The first part of the regexp to filter the old text for the new, it should be exactly the same as the regexp in the while() statement, because $text hasn't been changed (a new variable $rainbow represents the modified text).
So anytime I do something like that, it gets caught in the loop (for debugging I would have a $loops counter that would kill the script after 50 or 100 loops or so, and that always does kill the script).
So I'm thinking the s/// isn't working properly; it's not removing the text that was matched, so the while() loop is always true, even after it's gotten to every instance of .
However, what I always end up having to do is replacing the tags with capitilized keywords. So the new code looks like this:
Code:
# Filter tags into keywords.<br />$string =~ s/<rainbow>/RAINBOWSTART/ig;<br />$string =~ s/<rainbow>/RAINBOWEND/ig;<br /><br /># Search for these keywords.<br />while ($string =~ /RAINBOWSTART(.*?)RAINBOWEND/i) {<br /> my $text = $1;<br /> my $rainbow = &rainbow($text);<br /><br /> $string =~ s/RAINBOWSTART(.*?)RAINBOWEND/$rainbow/i;<br />}
(the difference between using (.*?) and $text in the s/// isn't the problem, I've tried all combinations possible for that regexp).
So, long story short, it seems that CGI's don't seem to like while loops that have a (.*?) type of syntax to them.
Does anybody know why it does this? Or is there some really obvious error in my s/// that I'm not seeing? The same kind of filtering ( style) seems to work just fine when I run it as a local Perl app, but not as a CGI. :unsure: _________________ Current Site (2008) http://www.cuvou.com/