Perl - Removing Bugs from Scripts
In a Nutshell - CIW Course Section 2, Part B3, Chapter 8
Debugging Perl Programs
Debugging is the process of isolating errant code that is preventing your script from executing correctly.
Perl has a fairly relaxed attitude toward syntax, not insisting that variables are declared before use, allowing functions to be called before they are defined, and permitting the omission of parentheses in certain situations. This easy going approach can make debugging more complex. The larger the script, the bigger this issue can become.
Checkpoint Charlie
OK! I'm being flippant, and probably showing my age, but checkpoints are the simplest, and often, the most effective way of isolating a section of troublesome code.
So what is a checkpoint? It is anything that let's you know where in your program you have reached. The simplest method is to include print statements at a number of points in your code so that you will know when these code sections have been executed.
print ("Section 1 Check!");
Taking this a stage further, you may include one or more variables so that the checkpoint will display the value of these variables to ensure they hold what is expected.
print("Section 1 \$myVar is: $myVar");
Remember to include the backslash to escape the scalar indicator.
Code Warning
The Perl interpreter can issue warnings about poorly constructed syntax, which it does not do by default. Including the "-w" switch in the shebang line in your code will turn this feature on.
#!c:\perl\bin\perl.exe -w
This should pick up things like: illegal variable names, missing variables, undefined filehandles, or a type mismatch.
Strict Pragma
The use of the strict module ensures that everything is done by the book. All flexibility of the interpreter is removed and the easy going approach is replaced by military precision.
Perl Debugger
The Perl debugger is a powerful, if slightly complicated, way to debug your Perl script. It is not an independent program, rather it is a mode of running the Perl interpreter.
c:\data\perlcode\perl -d mytest.pl
The debugger is invoked by including the -d switch when you start your script from the command line. This runs the interpreter in an interactive mode where you can enter commands to step through the code, interrogate variables, and set breakpoints.

