Perl Debugging Tools - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Perl Debugging Tools

Description:

If LIST is empty, uses the default message 'Warning: Something's wrong' ... t - toggle Trace Mode. If on, every line of the program is printed as it is evaluated ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 20
Provided by: pauld98
Category:
Tags: debugging | perl | toggle | tools

less

Transcript and Presenter's Notes

Title: Perl Debugging Tools


1
Perl Debugging Tools
  • warn()
  • die()
  • DataDumper
  • perldebug

2
warn LIST
  • Concatenates LIST, and prints result to STDERR.
    Program flow then continues uninterupted.
  • If LIST is empty, uses the default message
    Warning Somethings wrong
  • if last element of LIST does not end in a
    newline, appends at filename, line line

3
die LIST
  • Like warn, prints the concatenated list to
    STDERR. Program then exits, with the current
    value of ! as its exit code.
  • (! is an error code in numeric context, and the
    corresponding error message in string context)
  • Like warn, will print the file name and line
    number (and input line number, if applicable), if
    the last element of LIST does not end with a
    newline.

4
Special Tokens
  • In your internal error messages, its often
    helpful to know what line (and sometimes what
    file) program execution was at when the message
    was printed.
  • Two tokens __FILE__ and __LINE__
  • Hold the filename and linenumber, respectively.
  • Note These are NOT variables, so do not try to
    interpolate them!!

5
DataDumper
  • http//www.perldoc.com/perl5.6/lib/Data/Dumper.htm
    l
  • Very useful function for seeing exactly what is
    in a complex structure.
  • To use, first import the module
  • use DataDumper
  • Then, call Dumper with a reference to the
    structure you want to investigate, and print the
    results
  • print Dumper(\bigHash)

6
Dumper return value
VAR1 'Lalli' gt
100,
95,
86 ,
'Smith' gt
87, 92,
100
  • Dumper returns a pretty-printed string. The
    string contains the entire expanded contents of
    the data structure. Any internal data structure
    is also expanded.

7
Useful command line switches
  • -w
  • Know it. Use it. Love it.
  • -c
  • Simply check your program for compilation errors,
    without bothering to actually run the program.
  • Very useful for large programs, or when machine
    resources are tight (for example, your CGI
    homework next week)
  • -e
  • Evaluate one line of perl code, and return
  • ex
  • perl -e a 42 print \a a\n

8
The Perl Debugger
  • Command-line, interactive debugging program,
    written (of course) in Perl.
  • Start the debugger by supplying the -d switch
  • perl -d myfile.pl
  • This isnt actually a separate program. Instead,
    -d inserts debugger library code, then executes
    your program as normal, halting before first
    run-time executable statement.

9
now what?
  • The debugger halts just before the first run-time
    executable statement, and prints that statement
    to the screen, giving the prompt
  • DBlt1gt
  • It then waits for you to type a debugger command
    or a perl statement to be evaluated
  • Maybe most important command is h h
  • compact listing of debugger commands
  • Also simply h
  • expanded listing of debugger commands
  • precede any command with to page the output
  • (so, give h instead of just h)

10
Debugger commands
  • Stepping and Running
  • s - Single-step through the program. Execute one
    statement at a time. If statement is a function
    call, go into that function and halt at the first
    statement
  • (equiv of MSVCs Step Into command)
  • n - Single-step on same level. Execute one
    statement at a time. If statement is a function
    call, evaluate the function, and continue at next
    statement
  • (equiv of MSVCs Step Over command)
  • ltENTERgt - repeat the last s or n command
  • r - run until the end of the currently executing
    subroutine, display the return value, and halt at
    next line after subroutine call.

11
Debugger commands
  • Breakpoints
  • b LINE CONDITION - set a breakpoint at line
    number LINE if CONDITION is true. Debugger flow
    will halt only if CONDITION is true when it
    reaches LINE. Omit CONDITION to always halt.
    Omit LINE to set the breakpoint at the current
    about-to-be-executed line.
  • b SUBNAME CONDITION - set a breakpoint at the
    first line of the subroutine SUBNAME. Flow halts
    only if CONDITION is true when SUBNAME is called.
    Omit CONDITION to always halt.

12
Debugger Commands
  • Breakpoints, continued
  • d LINE - delete the breakpoint at line number
    LINE. Omit LINE to delete the breakpoint at the
    current about-to-be-executed line.
  • D - delete all breakpoints (careful! there is no
    undelete!)
  • L - list all breakpoints and their associated
    actions
  • c - continue execution (ie, run all statements)
    until the next breakpoint
  • c LINE - set a one-time breakpoint at LINE, run
    to that LINE, and remove the breakpoint.

13
Debugger Commands
  • Tracing
  • T - Excute a stack backtrace. This shows the
    sequence of function calls that took your program
    to the current point of execution
  • (ie, foo was called by bar at line 35 bar was
    called by baz at line 453 baz was called by
    ...)
  • t - toggle Trace Mode. If on, every line of the
    program is printed as it is evaluated
  • W EXPR - add EXPR as a global watch expression.
    Program will halt whenever value of EXPR changes.
  • W - delete all watch expressions (careful! There
    is no undelete!!)

14
Debugger Commands
  • Display
  • p EXPR - print EXPR. Literally. This is perls
    print function, with output redirected to the
    Debugger console
  • x EXPR - pretty-print EXPR, expanding nested
    structures in the same manner as DataDumper
  • V PKG - do an x on each global variable in the
    package PKG
  • X - do a V on the currently-executing package

15
Debugger Commands
  • Locating Code
  • l - list the next few lines. Several optional
    arguments
  • l LINE - list line number LINE
  • l SUBNAME - list first few lines of SUBNAME
  • l MININCR - list INCR1 lines, starting at line
    number MIN
  • l MIN-MAX - list lines MIN through MAX
  • - - list the previous few lines.
  • w LINE - list a window surrounding line LINE,
    or current line if omitted.
  • . - return internal debugger pointer to the
    current line.
  • /PATTERN/ - search forward for PATTERN
  • ?PATTERN? - search backward for PATTERN
  • S PATTERN - list subroutine names matching
    PATTERN, or all subroutines if PATTERN omitted.

16
Debugger Commands
  • Actions Command execution
  • a LINE COMMAND - execute COMMAND every time LINE
    is evaluated. Omit LINE for current line, omit
    COMMAND to delete the command
  • A - delete all actions
  • lt EXPR - evaluate perl expression before every
    debugger prompt
  • ltlt EXPR - add another expression
  • lt ? - list all expressions
  • lt - delete all expressions
  • gt EXPR - same as lt, but execute expression after
    the debugger prompt. (associated commands
    likewise)

17
Debugger Commands
  • Actions Command Execution,continued
  • COMMAND - specify a debugger command to be
    executed before each prompt (as opposed to a perl
    expression)
  • COMMAND - add an additional command
  • ? - list all commands
  • - delete all commands
  • ! - repeat the previous command.
  • ! NUMBER - repeat the NUMBERth command
  • ! -NUMBER - repeat the NUMBERth-from-last command

18
Debugger Commands
  • Miscellaneous Commands
  • q - quit the debugger
  • D - quit the debugger
  • R - restart the debugger, with breakpoints,
    actions, etc preserved
  • ALIAS VALUE - define a new debugger comand
    VALUE named ALIAS
  • ALIAS - print the value of ALIAS
  • - list all current aliases

19
For more help with Debugger
  • Camel, chapter 20
  • perldoc perldebug
Write a Comment
User Comments (0)
About PowerShow.com