Perl Tutorial

 

Table of Contents

Debugging the Program

In this step you'll add breakpoints to the program and "debug" it. Adding breakpoints lets you to run the program in chunks, making it possible to watch variables and view output as it is generated. Before you begin, ensure that line numbering is enabled in Komodo (View|View Line Numbers).

  1. Set a breakpoint On the "parse.pl" tab, click in the grey margin immediately to the left of the code on line 9 of the program. This will set a breakpoint, indicated by a green circle.
  2. Run the debugger Select Debug|Start (or <F5>, or use the Debug Toolbar). In the Debugging Options dialog box, click OK to accept the defaults. The debugger will process the program until it encounters the first breakpoint.

Komodo Tip Debugger commands can be accessed from the Debug menu, by shortcut keys, or from the Debug Toolbar. For a summary of debugger commands, see Debugger Command List.

  1. Watch the debug process Notice that the breakpoint turns red, which indicates that the debugger is running. A yellow arrow on the breakpoint indicates the position at which the debugger has halted. Click on the "mailexport.xml" tab. Komodo informs you that the file has changed. Click Yes to reload the file.
  2. View variables In the Output Pane, click the Debug Variables tab. On the Proximity tab, the input and output files have been assigned values; the Registers tab shows reserved Perl processing registers.
  3. Line 9: Step In Select Debug|Step In. "Step In" is a debugger command that causes the debugger to execute the current line and then stop at the next processing line (notice that the lines between 9 and 16 are raw output indicated by "here" document markers).
  4. Line 16: Step In On line 16, the processing transfers to the module Text::CSV_XS. Komodo opens the file CSV_XS.pm and stops the debugger at the active line in the module.
  5. Line 61: Step Out Select Debug|Step Out. The Step Out command will make the debugger execute the function in Text::CSV_XS and pause at the next line of processing, which is back in parse.pl on line 19.
  6. Line 19: Step Over Select Debug|Step Over. The debugger will process the function in line 19 without opening the module containing the "getline" function.

Komodo Tip What do the debugger commands do?

  • Step In executes the current line of code and pauses at the following line.
  • Step Over executes the current line of code. If the line of code calls a function or method, the function or method is executed in the background and the debugger pauses at the line that follows the original line.
  • Step Out when the debugger is within a function or method, Step Out will execute the code without stepping through the code line by line. The debugger will stop on the line of code following the function or method call in the calling program.
  1. Line 21: Set Another Breakpoint Click in the grey margin immediately to the left of the code on line 21 to set another breakpoint.
  2. Line 21: Step Out It appears that nothing happened. However, the debugger actually completed one iteration of the "while loop" (from lines 21 to 51). To see how this works, set another breakpoint at line 37, and Step Out again. The debugger will stop at line 37. On the Debug Variables tab of the Output Pane, look at the data assigned to the $record variable. Then Step Out, and notice that $record is no longer displayed, and the debugger is back on line 21. Step Out again, and look at the $record variable - it now contains data from the next record in the input file.
  3. Line 37: Stop the Debugger Select Debug|Stop to stop the Komodo debugger.

Perl Pointer Did you notice that output wasn't written to mailexport.xml after every iteration of the while loop? This is because Perl maintains an internal buffer for writing to files. You can set the buffer to "autoflush" using the special Perl variable "$|".


Top Previous Next