Print this Article (NS4)
Netscape Navigator
Internet Explorer
Opera
Neoplanet

Forums
HTML
General
Site Dev
Programming
Flash
Grafix (Art)

Laboratory
Smart HTML
Color Lab
Generators

Contents
Simple CGI
  1. Hello World
  2. Print function
  3. Quoting, EOF
  4. Metacharacters
  5. Special Characters
Perl Basics 1
  1. Variables
  2. Arrays
  3. Hashes
  4. Split function
  5. Subroutines
  6. Defaults
Form CGI
  1. Loops
  2. Conditions
  3. Boolean Statements
  4. Pattern Matching
Time CGI
  1. Local Time
  2. GM Time
  3. time function
Perl Basics 2
  1. Reading Files
  2. Writing Files
  3. Including Files
  4. chop function
  5. chomp function
Guestbook CGI

Redirect CGI

Poll CGI

  1. Giving Commands
  2. Voting
  3. Results Display
  4. Adding Your Vote
Password CGI
  1. Authentification
  2. Multiple Users
  3. Encryption
Mailing List CGI
  1. Sendmail
  2. Multiple Recipients
Unlimited Subdomains CGI

News Grabber CGI

  1. LWP::Simple
Message Board CGI (Part 1)

Back to the Top


Perl Case Study - Common Beginners Mistakes
By Lisa Hui

Admittedly, even an intermediate level Perl "programmer" (the not-so-Perl-newbie-ish category of people) will make lots of little errors that will give you server errors. Most of them are syntax errors but if youv'e just finished writing 1000+ lines of code, you really don't want to have to go over every line again because of a syntax error. Some of the most common ones made are listed below - and these are really the killers to watch out for:

  1. Not backslashing the @ symbol in email addresses (pronounced "at"); remember that it is the symbol for an array in Perl.
  2. Missing brackets in loop and conditional statements (if/else) { }. Sometimes you forget to close them - particularly if you have nested loops. To check for a mistake like this, go to a part in which you have loops or conditionals and make sure - starting from the nested loops and working your way outwards - that each loop as both a beginning and end bracket. If your server keeps error message logs accessible, the error will most likely mention something about "mismatched brackets." It could either mean you have too many or too few. A few good text editors will automatically indent bracketed text so it is easier to match them up.
  3. Not backslashing metacharacters that are used literally (like in pattern matching expressions) will give you errors!
  4. Omitting the line print "Content-type: text/html\n\n"; before printing anything out to the browser. You cannot include these in the quoted print lines - print qq will not work.
  5. Forgetting to close statements with a semicolon. This is pretty self explanatory. You will receive an error message mentioning an unterminated statement.
  6. Putting comment lines in loops.
  7. Barewords - if you forget to add the $ before it, you may or may not get an error message - instead in certain cases, it evaluates to a "false." If used in a condition statement, it will give an error message. Also, if used in an equality statement, a bare word will result in a server error.
  8. The conditional operators "==" and "eq" as well as "!=" and "ne" are similar but not the same. "==" and "!=" work for numerical values whereas "eq" and "ne" work with strings. For example, 8 and 8.0 are equivalent numerically (8==8.0) but (8 ne 8.0).
  9. Misspelling elsif as elseif or saying else if instead. The last one is C++ syntax. For some reason, the creators of Perl preferred not having to type out the entire expression "else" in that conditional operator.
  10. Not marking the script executable. UNIX requires that you chmod (change mode) the script so that it is executable. In other words, you must give peole trying to run the script, either from a browser or through the command line to run it on that server. Scripts should be chmodded to 755 (rwx-r-x-r-x). Directories/folders should also be chmodded to 755 if there are scripts being executed in them. Directories being written to should be chmodded to 777 in most cases. (files being written to should be chmodded 777 or 666 - whatever works for you).

General Troubleshooting

Although having to debug a script that is longer than 50 lines is time consuming, there is a way to better isolate your problem area. You should first figure out the points which would most likely be causing the problem. If you have subroutines, try removing them (cut and paste that section to a temporary page or commenting out parts. If your script stops giving you errors, then you will know that the problem lies in that snippet of code. Otherwise, you can randomly try to take out chunks of code that would most likely be problematic: loops and conditionals. But always isolate and check for syntax errors first. Repeating this experimentation process will probably help debug slightly larger scripts much quicker than recoding each line.

Make sure you know what they are doing and that an end statement will be reached. If the script just keeps stalling (the browser keeps saying that it will load but it doesn't) and you were creating files with that script just hit the stop button on your browser. The scription could be creating thousands of extraneous files in your account and taking up megabytes of space at a time. This is a really rare situation unless you sometimes write management scripts like I do, but it is an example of what careless nested loops might do :)


Free CGI-BINs

©1999 Team 26297 "Ad Infinitum Web." All rights reserved. Any reproduction of this document for commercial or redistribution purposes without the permission of the author is forbidden.