User Control Panel
Advertisements

HELP US, HELP YOU!

while() loops in CGI

 
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl
View unanswered posts
Author Message
Cer
Upgraded Agent
Upgraded Agent


Joined: 03 Feb 2004
Posts: 3776
Location: Michigan
Reputation: 146.9
votes: 4

PostPosted: Fri Apr 01, 2005 6:42 pm    Post subject: Reply with quote

Okay, first I'll explain what I'm doing here:

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/
Back to top
mattaustin
Sentinel
Sentinel


Joined: 19 Jul 2004
Posts: 556
Location: Los Angeles, CA
Reputation: 50.7
votes: 1

PostPosted: Fri Apr 01, 2005 7:09 pm    Post subject: Reply with quote

the first thing i notice is

while ($string =~ /RAINBOWSTART(.*?)RAINBOWEND/i) {

i would thinkg needs to be

while ($string =~ /RAINBOWSTART(.*?)RAINBOWEND/ig) {

_________________
[ matt ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl All times are GMT
Page 1 of 1

 



Protected by phpBB Security phpBB-TweakS
phpBB Security Has Blocked 9 Exploit Attempts.
Antispam Captcha Mod by phpbb-security.com
Powered by phpBB © 2001, 2005 phpBB Group