Information on how to use the debugger is found by typing at your command line:
perldoc perldebug
Here is a sample of the output:
NAME
perldebug - Perl debugging
DESCRIPTION
If you invoke Perl with the -d switch, your script runs under the Perl source debugger. This works like an interactive Perl environment, prompting for debugger commands that let you examine source code, set breakpoints, get stack backtraces, change the values of variables, etc. This is so convenient that you often fire up the debugger all by itself just to test out Perl constructs interactively to see what they do. For example:
$ perl -d -e 42
In Perl, the debugger is not a separate program the way it usually is in the typical compiled environment. Instead, the -d flag tells the compiler to insert source information into the parse trees it's about to hand off to the interpreter. That means your code must first compile correctly for the debugger to work on it. Then when the interpreter starts up, it preloads a special Perl library file containing the debugger.
The program will halt right before the first runtime executable statement (but see following regarding compile-time statements) and ask you to enter a debugger command. Contrary to popular expectations, whenever the debugger halts and shows you a line of code, it always displays the line it's about to execute rather than the one it has just executed.
Any command not recognized by the debugger is directly executed (eval'd) as Perl code in the current package. (The debugger uses the DB package for keeping its own state information.)
For any text entered at the debugger prompt, leading and trailing whitespace is first stripped before further processing. If a debugger command coincides with some function in your own program, merely precede the function with something that doesn't look like a debugger command, such as a leading ; or perhaps a +, or by wrapping it with parentheses or braces.
<continues here>
To invoke the Perl debugger, use the -d switch. It allows you to examine your program in an interactive-type environment after it has successfully compiled. After each line, the script will stop and ask for a command. The line you will be looking at is the next line that will be executed, not the previous one. The prompt contains the current package, function, file and line number, and the current line. Following is a list of the debug commands.
Once you start the debugger, all the debugging commands are listed by typing h at the debug prompt, or h h if you can't read what is displayed.
To exit the debugger, type q for quit or R for restart.
$ perl -d exer.1 Loading DB routines from $RCSfile: app01.xml,v $$Revision: 4.0.1.2 $$Date: 2008/05/04 10:31:07 $ Emacs support available. Enter h for help. main'(exer.1:3): print "Today is ", 'date'; DB<1> h T Stack trace. s Single step. n Next, steps over subroutine calls. r Return from current subroutine. c [line] Continue; optionally inserts a one-time-only breakpoint at the specified line. <CR> Repeat last n or s. l min+incr List incr+1 lines starting at min. l min-max List lines. l line List line. l List next window. - List previous window. w line List window around line. l subname List subroutine. f filename Switch to filename. /pattern/ Search forwards for pattern; final / is optional. ?pattern? Search backwards for pattern. L List breakpoints and actions. S List subroutine names. t Toggle trace mode. b [line] [condition] Set breakpoint; line defaults to the current execution line; condition breaks if it evaluates to true, defaults to 1. b subname [condition] Set breakpoint at first line of subroutine. d [line] Delete breakpoint. D Delete all breakpoints. a [line] command Set an action to be done before the line is executed. Sequence is: check for breakpoint, print line if necessary, do action, prompt user if breakpoint or step, evaluate line. A Delete all actions. V [pkg [vars]] List some (default all) variables in package (default current). X [vars] Same as "V currentpackage [vars]". < command Define command before prompt. > command Define command after prompt. ! number Redo command (default previous command). ! -number Redo numberth-to-last command. H -number Display last number commands (default all). q or ^D Quit. p expr Same as "print DB'OUT expr" in current package. = [alias value] Define a command alias, or list current aliases. command Execute as a Perl statement in current package. DB<1> l 3: print "Today is ", 'date'; 4: print "The name of this \uperl script\e is $0.\n"; 5: print "Hello. The number we will examine is 125.5.\n"; 6: printf "The \unumber\e is %d.\n", 125.5; 7: printf "The \unumber\e is %d.\n", 125.5; 8: printf "The following number is taking up 20 spaces and is right-justified.\n"; 9: printf "|%-20s|\n", 125; 10: printf "\t\tThe number in hex is %x\n", 125.5; 11: printf "\t\tThe number in octal is %o\n", 125.5; 12: printf "The number in scientific notation is %e\n", 125.5; DB<1> q (quit)
h | Lists help messages for all debugger commands. |
h p | Lists a help message for debugger command p. |
Listing parts of a script:
Getting out of the debugger:
q | Quit the debugger. |
<Ctrl>-d | Quit the debugger. |
R | Restart the debugger and a new session. |
Breakpoints:
Breakpoints allow you to set a place where the program will stop so you can examine what's going on. They must be set on lines that start an executable statement.
Printing variable values:
Tracing:
T | Produces a stack backtrace listing of what subroutines were called. |
t | Toggles trace mode. |
Aliases:
= Lists all aliases. | |
= ph print "$hashref–>{Science}–>{Lou}" | ph is an alias for printing a hash value. |