Google Answers Logo
View Question
 
Q: help with perl cgi script ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: help with perl cgi script
Category: Computers > Programming
Asked by: asm_internet-ga
List Price: $12.00
Posted: 12 Aug 2005 18:22 PDT
Expires: 11 Sep 2005 18:22 PDT
Question ID: 555165
Hi, 
I wish to access a text file via http and use it as a datafile in a 
subroutine. It is basically an obcenity filter. I have 4 fields (e.g.
$in{'RealName'})


The contents of these fields are checked against the records of the
datafile (one word or phrase per line) and if there is a match, there
is one result (result A). Otherwise there is another result (result
B).


use LWP::Simple; 


$remotelist = get "http://www.domainname.com/classifieds/smut.list"; 
## grabbing the file 


@abc=split(/\n/,$remotelist); 
## split by line 


open (BADLIST, "@abc"); 
@badlist = <BADLIST>; 
close(BADLIST); 
  foreach $badword (@badlist) { 
 chop ($badword) 


## each line into an array - right?? 


  if (($in{'RealName'} =~/\b$badword\b/i) ||   ($in{'EMailAddress'} 
=~/\b$badword\b/i) ||    ($in{'AdSubject'} =~ /\b$badword\b/i) || 
($in{'Description'} =~ /\b$badword\b/i)) { 


### here i am trying to match incoming field data which may 
### include one or more words with lines in the smut file 
### maybe my logic is bad - can't figure it out 
### smut file has word or multiple words on each line 


{&PrintHeader} 
print "found a bad word"; 
        exit; 
} else { 
        {&PrintHeader} 
print "good"; 
exit; 


} 
} 


// end 

the text file looks like: 


word 
bad word 
very bad word 
words 
bad words 
very bad words 
etc..
Answer  
Subject: Re: help with perl cgi script
Answered By: palitoy-ga on 13 Aug 2005 01:50 PDT
Rated:5 out of 5 stars
 
Hello asm_internet-ga

Thank-you for your question.

I think you are nearly there with your script but have got in a muddle
over getting the "bad words" into an array.  If each line is ended
with a \n character (this might not be the case on some systems) you
can remove the open...close section of your code.

Let me know if the following piece of code suits your needs.  If you
have any further questions or help on this subject please ask for
clarification and I will do my best to respond swiftly.


###############################################
#!/usr/bin/perl

print "Content-type: text/html\n\n";

use LWP::Simple;

# set some variables for clarity later on
$RealName = $in{'RealName'};
$EmailAddress = $in{'EMailAddress'};
$AdSubject = $in{'AdSubject'};
$Description = $in{'Description'};

# get the remote smut.list
$remotelist = get "http://www.domainname.com/classifieds/smut.list";
# place each line of the smut.list into an array
# in other words - split the file by the \n character
@badwords=split(/\n/,$remotelist);
# loop through each element in the array
foreach $badword (@badwords) {
  # remove the \n character
  chop ($badword);
  # if the variable matches the phrase in the smut.list then print
  if (($RealName =~/\b$badword\b/i) || ($EmailAddress
=~/\b$badword\b/i) || ($AdSubject =~ /\b$badword\b/i) || ($Description
=~ /\b$badword\b/i)) {
    print "found a bad word\n";
  }
  else {
    print "good\n";
  }; # end if
}# end foreach
# exit the program
exit(0);
asm_internet-ga rated this answer:5 out of 5 stars and gave an additional tip of: $3.00
thanks palitoy - yes i did muddle up the array - thanks for getting me
through it. I twiddled with what you gave me and got it to work
perfectly

Comments  
Subject: Re: help with perl cgi script
From: palitoy-ga on 14 Aug 2005 01:08 PDT
 
Thanks for the rating and the generous tip, they are both much appeciated.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy