Perl Tricks


How to enforce a non-blank field

there must be a perl shortcut that can do this... but i don't know what it is.
    # trim sitename
    $sitename=~s/^\s*(.*?)\s*$/$1/;
    if($sitename eq "") {&error("sitename cannot be blank"); }

How to handle both sendmail and smtp mail

this code (untested) was submitted to me by admin@wolfpower.every1.net
$mailtype = "1";        # "1"=sendmail  ""=smtp
...
if ($mailtype eq "1") {
    open (MAIL,"|/usr/lib/sendmail");
    print MAIL "To: $email\n";
    print MAIL "From: $myemail\n";
    print MAIL "Subject: Subject\n\n";
    print MAIL "this place is here - wish you were beautiful!\n";
    close MAIL;
} else {
    use Net::SMTP;

    $smtp = Net::SMTP->new($mailserver); # connect to an SMTP server
    $smtp->mail($myemail);     # use the sender's address here
    $smtp->to($email);        # recipient's address
    $smtp->data();                      # Start the mail

    # Send the header.
    $smtp->datasend("To: $email\n");
    $smtp->datasend("From: $myemail\n");
    $smtp->datasend("Subject: Subject\n");
    $smtp->datasend("\n");

    # Send the body.
    $smtp->datasend("this place is here - wish you were beautiful!\n");
    ...
    $smtp->datasend("\n");
    $smtp->dataend();                   # Finish sending the mail
    $smtp->quit;                        # Close the SMTP connection
}


How to add a popup window to an existing page

add this code between the "<html>" and the "<body>" tags in an existing page and a window will pop up everytime the page is opened. this works only with browsers with javascript enabled.
<html>
<script language=JavaScript>
<!--
window.open("/ezhp2000/banner.html", 
"w3adIAYJAAII",
"width=515,height=125");
//-->
</script>

this is the HTML of a sample popup window:

<HTML>
<BODY BGCOLOR="#ffffff" TEXT="#400040" LINK="#0000ff" VLINK="#ff0000">
<CENTER>
  <IMG SRC="/ezhp2000/ezhomea2.gif" 
  WIDTH="468" HEIGHT="60"> <BR>
  <H3>
    (you can place your own banner here)
  </H3>
</CENTER>
</BODY></HTML>

How to make an EZ script generate a page with a popup window

most of you already know that my EZ scripts create new pages by simply "print"-ing the necessary HTML. so if you want new HTML to be added to your generated pages you have to insert the script commands (ie. print commands) that print all the needed HTML. for your convenience, i've coded the perl script segment below so you merely cut-n-paste from it and include it in your script. (see above to see what a sample popup page contains.)
print "<html>...";
&emit_popup_html;            // insert this between the html and body tags
print "<body>...";
...
// add this code at the end of the script
sub emit_popup_html {
    print "<script language=JavaScript>";
    print "<!--";
    print "window.open(\"/ezhp2000/banner.html\",";
    print "\"w3adIAYJAAII\",";
    print "\"width=515,height=125\");";
    print "//-->";
    print "</script>";
    }

How to use a ROT13 subroutine

i know this is is a very simplistic way of "hiding" information but it might be sufficient for some simple applications. anyway, the idea of ROT13 is that you "rotate" each letter in a string 13 letters away such that "a" becomes "n", "b" becomes "m", etc. the beauty of this simple algorithm is that you can use the same subroutine in translating from plain text to rotated text and back.
...
print "Content-type: text/html\n\n";
$st="tom,dick,harry";
$w=&rot13($st);
print "<br>inp=$st oup=$w"; 
$r=&rot13($w);
print "<br>inp=$w oup=$r"; 
exit;
...
sub rot13 {
  ($inp) = "@_";
  $inp =~ y/a-zA-Z/n-za-mN-ZA-M/ ;
  return $inp;
}
...

How to enforce a valid hexadecimal color value

...
if ($colorfld !~ /^[0-9a-fA-F]{6}$/) {
    &input_error("invalid color value, enter 6 hex digits (without #)");
    exit;
    }
...
sub input_error {
    local ($errmsg) = @_;
    print "content-type: text/html\n\n";
    print "<html><head><title>Error</title></head>\n";
    print "<body>";
    print "<h1>Input Error!</h1><h3>$errmsg</h3>\n";
    print "<p>Please go back and correct</p></body></html>\n";
    }
...

How to enforce a valid HTTP/FTP URL entry

...
if ($siteurl !~ /^(f|ht)tp:\/\/(\w|\-)+\.\w+/) {
    &input_error("invalid site URL - $siteurl");
    exit;
    }
...
sub input_error {
    local ($errmsg) = @_;
    print "content-type: text/html\n\n";
    print "<html><head><title>Error</title></head>\n";
    print "<body>";
    print "<h1>Input Error!</h1><h3>$errmsg</h3>\n";
    print "<p>Please go back and correct</p></body></html>\n";
    }
...

How to enforce a valid EMail Id Entry

...
if ($FORM{'emailid'} !~ /^\w+(\.)*\w+\@(\w|\-)+\.\w+/) {
   &no_emailid;
}
...
sub no_emailid {
    print "Content-type: text/html\n\n";
    print "<html><head><title>ERROR: No EMail Id</title></head>\n";
    print "<body bgcolor=#FFFFFF text=#000000>";
    print "<center><h1>Invalid EMail ID</h1></center>\n";
    print "You entered an invalid email id! Please go back and correct"; 
    print "</form></body></html>\n";
    exit;
    }
...

How to allow the textbox to accept "return" instead of "&&"

(submitted by Karl-Heinz Klug)
Hello Manny,

a short modification to your scripts.


$in{'Description'}=~s/\cM//g;
$in{'Description'}=~s/\n\n/<p>/g;
$in{'Description'}=~s/\n/<br>/g;
$Description = $in{'Description'};

with this modification the Textbox accepts "return" instead of && :-)

Regards,

Karl

How to add a bad-words filter

(submitted by Eric Wabant)

# The @badwords array contains the list of "bad words" that you want to prevent
@badwords = ("fuck", "shit", "piss", "cunt", "bitch", "asshole", "ass", 
    "tits", "tit", "suck", "cock", "cocks", "dick", "dicks");
sub check_badwords {
    $badwords="off";
    @check_fields = ("realname", "username", "url", "city", "state",
    "country", "comments");
    foreach $badword (@badwords) {
        foreach $check_field (@check_fields) {
            if ($FORM{$check_field} =~ /$badword/) {
                $badwords="on";
                }
            }
        }
    if ($badwords eq "on") {
        &badwords_error;
        exit;
        }
    }
 
sub badwords_error {
   print "Content-type: text/html\n\n";
   print "<html><head><title>ERROR: Bad Words Found!</title></head>\n";
   print "<body bgcolor=#FFFFFF text=#000000>";
   print "<center><h1>Bad Words Found!</h1></center>\n";
   print "Please keep our website wholesome and clean by refraining from using vulgar words!"; 
   print "</form></body></html>\n";
    }

How to add banners to generated pages

  1. create a text file containing the EXTRA code (html or javascript, etc) you would need to add to a page to give it a banner. for example
    <table border="1"><tr><td bgcolor="yellow">
    my banner goes here
    </td></tr></table>
    

  2. wherever you see a quote mark (") or an ampersand (&) or a backward slash (\), insert a backward slash (\) before that character. then enclose each line in quotes and add the print command at the beginning and a semicolon at the end. the above would become:
    print "<table border=\"1\"><tr><td bgcolor=\"yellow\">";
    print "my banner goes here";
    print "</td></tr></table>";
    
  3. insert these statements in the script in the section where a page is being created, right after the point where "<body...>" statement is generated.