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

Hi Monks. I have a badwords routine that checks a text file of bad words (1 per line) and then sends an email to the admin of a suspect word is posted. My question is how to get the suspect word to print in the email. This should be simple but I'm missing something. I've commented out the lines that make the badword print to the screen and I can't get it to print in the email. People write and ask why the spam filter was triggered and I have no way to know. Thanks.
sub filter_bad_words_suspect { $bad_words_file = "/bad-words-suspect.txt"; $badword_found = 0; open(BADWORDS,"$bad_words_file") || die "*** Could not open bad wo +rds list.\n"; @badwords = <BADWORDS>; close(BADWORDS); foreach $badword (@badwords) { # Strip any extra CR/LF's $badword =~ s/\n//g; $badword =~ s/\r//g; if (($description =~ /$badword/i) || ($email =~ /$badword/i) +|| ($title =~ /$badword/i) || ($contact =~ /$badword/i) || ($organiza +tion =~ /$badword/i)) { $badword_found = 1; # If a bad word is found, highlight all occurances of it #$contact =~ s/($badword)/<B>$1<\/B>/ig; #$email =~ s/($badword)/<B>$1<\/B>/ig; #$title =~ s/($badword)/<B>$1<\/B>/ig; #$description =~ s/($badword)/<B>$1<\/B>/ig; #$organization =~ s/($badword)/<B>$1<\/B>/ig; } # end if (($name =~ /$badword/i) || ($email =~ /$badword/i). +.. } # end foreach $badword (@badwords) &error2(bad_words) if ($badword_found == 1); return; } # end filter_bad_words subroutine sub error2 { require "all-common.sub" || die "Error loading all-common.sub"; $error = $_[0]; $admin_email = "info\@123.com"; open (MAIL, "|$mailprog $admin_email") || die "Can't open +$mailprog!\n"; print MAIL "From: Test Server <admin@123.com>\n"; print MAIL "To: $admin_email\n"; print MAIL "Subject: Error: Suspect Words\n"; print MAIL "Content-type: text/plain\n"; print MAIL "The spam filter was triggered.\n\n"; print MAIL "Name: $name\n"; print MAIL "Username: $username\n"; print MAIL "Email: $email\n"; print MAIL "IP: $IP\n"; print MAIL "Badword: $badword\n"; close (MAIL); return; }

Replies are listed 'Best First'.
Re: Badwords Routine
by toolic (Bishop) on Aug 15, 2009 at 03:41 UTC
    &error2(bad_words) if ($badword_found == 1);
    'bad_words' is a bareword. You would have gotten a compile error if you had used use strict;. I suspect this is part of your problem. See also use strict and warnings.
      I figured it out:
      $bad_entered =($badword);
      Then I just pring $bad_entered. Thanks.

        Or maybe push(@bad_entered, $badword);, in case more than one bad word is found. You can then print the list of bad words (@bad_entered) in the email.

Re: Badwords Routine
by Perlbotics (Archbishop) on Aug 15, 2009 at 20:21 UTC

    You might find the CPAN modules Regexp::Common and Regex::Common::Profanity useful, especially if your target language is English or Austrian/German. Some of these modules even cope with spelling v^r1a+10n2.
    Maybe using or analysing these modules helps to ease the pain of collecting and maintaining a dictionary of bad words in the first place?