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:
-
Not backslashing the @ symbol in email addresses (pronounced "at"); remember
that it is the symbol for an array in Perl.
-
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.
-
Not backslashing metacharacters that are used literally (like in pattern
matching expressions) will give you errors!
-
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.
-
Forgetting to close statements with a semicolon. This is pretty self explanatory.
You will receive an error message mentioning an unterminated statement.
-
Putting comment lines in loops.
-
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.
-
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).
-
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.
-
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
|