Things I always do wrong
Perl really is a nice language, but it's also finicky (as I guess all
computer things are these days). The mistakes here represent 90% of all the
bugs in my Perl programs:
- Missing semicolons
- Every line outside of conditional-checking stuff needs one, and that
can be a lot of lines. I usually leave at least one out. If you get an
uninformative error message check to make sure that the line which
caused the error and the one before it both have semicolons.
chmod +x blah.pl
- Like I said in my Unix basics refresher course, you need to tell Unix
which files are executables. I forget to do this about half the time.
The error message you get for this and the one you get if your first
line --
#!/usr/local/bin/perl -- is wrong are identical so if you get
that message, check both.
Content-type: text/html
- If you leave this out, things will look pretty much fine when you run
the script from the Unix command line, but when you try to run it from
a Web browser, you'll either get a
500 Server Error: malformed header
from script, or you'll get something asking you what you want to do
with a file in some format like X-URL-Encoded or something.
- '=' instead of '==' or 'eq' inside conditionals
- Like I said, a single = always sets the variable before it to the stuff
after it. This means the line if
($name="charlie")... will be true and
set $name to "charlie" unless for some reason
$name can't be set (in
which case it will return false).
- '@' instead of '$' when referring to individual array elements
- Single variables (known as scalars) always have their names prepended
by $ and not by @.
@names[0] can return unexpected results.
Debugging
Here are some very basic debugging tips:
- Run your script from the command line (e.g. type "
./foo.pl"). This will
help figure out basic things like missing semicolons from the error
information which Perl will return. When scripts die while being
executed from a web browser, you won't get information any more useful
than that it died.
- Make sure the output starts with the appropriate HTTP header
information. Usually this means
Content-type: text/html and a blank
line.
- To test form information from the command line, put in a temporary line
at the beginning of your script to give the script fake form
information. For example:
$buffer="name=jane+doe&zip=10003";
One last tip: You can get some fairly sophisticaed multi-part interaction by
using the form element <input type="hidden">. You can record a user's
previous actions and accumulate them as they go through a series of
interactions. For example, you could have them walk north, east, west, or
south 8 times (each time adding another hidden element to the form which
they submit to determine a direction) and if at the end, they're in a
specific place, they could get to some secret web area.
That's it
You've finished Jamie's crash course in CGI Programming with Perl. You
should now be able to get started in the cool ;-) possibilities of
interaction design on the Web.
I hope you learn to like Perl as much as I do. Happy coding.
Introduction to CGI Programming with Perl