Tips: When developing your script...
- Develop your program in stages. Once part of it works,
save the working version to another file (or use a source code
control system like RCS) before continuing to improve it.
- When running interactively, show the user signs of activity.
There is no need to dump everything to the screen (unless requested to),
but a few words or a number change every few minutes will show that
your program is doing something.
- Define all constant values at the beginning of your script, or
inside a subroutine named 'initialize', 'initVars' or similar.
- Use as few 'hardcoded' filenames as possible. Define permanent
file paths at the beginning of your script (or inside the 'initVars' subroutine).
- Assume nothing regarding your script environment. Always check before
accessing a directory or file, to be sure they are available and that you
can perform the operation you intend to. If the check fails, report to the user.
- Ask a friend to run and test your software. Try to run it from a different
directory. The /tmp directory is a good choice.
- Store all temporary files in the /tmp directory. This is the ideal
place for files that are to be used during the execution of your
software but that don't need to remain once the run is done.
- Use the '$$' default variable for assigning a unique id to your
temporary files, as follows: $myTmpFile = "/tmp/myFile$$";
- Subroutines should rely on global variables as little as possible
or not at all. Receive parameters in my() variables.
- Use subroutine and variables names that reflect their purpose in life.
- Provide block comments for all subroutines, describing the
inputs, outputs, and functionality
- Comment your script. Any information on what it is doing or why might be
useful to you a few months later.
- Decide on a coding convention and stick to it. For example,
- for variable names, begin globals
with a capital letter and privates (my) with a lower case letter
- indent new control structures with (say) 2 spaces
- line up closing braces, as in:
if (....) {
...
...
}
- Add blank lines between sections to improve readibility
While developing a .cgi script, runtime errors are not returned
to you, but sent to the server.
In order to be able to receive the errors generated by your
script, you may request CGI to send the errors to the browser
with the following syntax:
#!/usr/local/bin/perl
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
once you are sure that your script works as expected,
please replace it back to the 'standard'
#!/usr/local/bin/perl
use CGI qw(:standard);
Also remember that you may execute the .cgi scripts from
the Unix command line, adding the parameters after the
name of the script, as in the following example:
myScript.cgi num=10 name=John
Send other suggestions to Jaime.Prilusky@weizmann.ac.il