Perl Debugging Tools - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Perl Debugging Tools

Description:

Like warn, prints the concatenated list to STDERR. ... Simply check your program for compilation errors, without bothering to actually run the program. ... – PowerPoint PPT presentation

Number of Views:110
Avg rating:3.0/5.0
Slides: 20
Provided by: pauld98
Category:

less

Transcript and Presenter's Notes

Title: Perl Debugging Tools


1
Perl Debugging Tools
  • warn() die()
  • DataDumper
  • BDeparse
  • __DATA__
  • perldebug
  • ptkdb

2
warn LIST
  • Concatenates LIST, and prints result to STDERR.
    Program flow then continues uninterrupted.
  • If LIST is empty, uses the default message
    "Warning Something's 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 debugging messages, it's 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
Token Markers
  • The special marker __END__ can be used to give an
    absolute end to your code.
  • The parser will not look at any text below this
    marker. Type anything you wish here.
  • The marker __DATA__ is used as a
    pseudo-filehandle
  • Within your code, use DATA as an open input
    filehandle.
  • ltDATAgt reads from text below the __DATA__ marker.
  • Useful when debugging code, to avoid continuously
    editing input file
  • Very helpful when emailing for help
  • You can send me a small test script that shows
    your problem, without saying "I have a text file
    that has . . . "
  • hint, hint. . .

6
DataDumper
  • Very useful function for seeing exactly what is
    contained in a variable
  • 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)
  • for now, just accept the fact that a reference is
    created by prepending a \ to the variable name
  • perldoc DataDumper for more info

7
Useful command line switches
  • -w
  • obsolete. use warnings instead
  • -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
  • -e
  • Evaluate a "one-liner", and return
  • perl -e 'a 42 print "\a a\n"'

8
More Useful Switches
  • -n Automatically open each file on the command
    line and loop through.
  • perl ne 'print unless _ lt 10' .txt
  • !/usr/bin/env perl for (_at_ARGV) if (!open my
    f, 'lt', _) warn "Can't open _ !"
    else while (ltfgt) print unless _ lt
    10

9
One step further
  • -p Automatically open each file on the command
    line and loop through, printing each line.
  • perl pe '_substr(_,0,10)' .bat gtout.txt
  • !/usr/bin/env perl for (_at_ARGV) if (!open my
    f, 'lt', _) warn "Can't open _ !"
    else while (ltfgt) _ substr(_, 0,
    10) continue print

10
Just two more. . .
  • -i used with p or n to edit files "inline"
  • perl pi.bkp -e '_substr(_,0,10)' .bat
  • All .bat files are backed up as .bat.bkp, and
    the original .bat files are edited in place.
  • -M use a module from the command line
  • perl MDataDumper e' print Dumper(\f)'
  • for more information on all command line
    switches, see perldoc perlrun

11
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 isn't actually a separate program. Instead,
    -d inserts debugger library code, then executes
    your program as normal, halting before first
    run-time executable statement.

12
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

13
Debugger commands
  • s Single-step, entering functions
  • n Single-step over functions
  • ltENTERgt repeat the last s or n command
  • r run to end of current subroutine
  • b set breakpoints
  • d or D delete breakpoints
  • c continue until next breakpoint
  • L list breakpoints
  • T trace the stack calls
  • w set watch variables
  • p or x print or examine variables
  • l list code

14
Debugger
  • Those are just a quick summary of the available
    commands. For more information, use h or h h
    within the debugger, or readperldoc perldebug

15
A Better (?) Debugger
  • ptkdb
  • (Perl ToolKit DeBugger)
  • Graphical debugger, written in Perl, making use
    of Perl/Tk
  • On a Unix machine in an Xwindows environment, use
    CPAN
  • perl MCPAN e'install DEVELptkdb'
  • On Windows, using Activestate, use PPM
  • ppm
  • search ptkdb
  • install ltgt
  • if search yields no results, run this first, then
    try again
  • repository add BdP http//www.bribes.org/perl/ppm

16
Running ptkdb
  • perl dptkdb file.pl
  • creates a GUI window, with your code on the left
    (with line numbers)
  • On right, panel of expressions, breakpoints, and
    subroutines
  • click on any line number to set break point
  • click the breakpoints tab to give a break point a
    condition (ex i 5)
  • Use buttons at top to step in/over, run to next
    breakpoint, or run to cursor.
  • Expressions panel auto-updates after every
    executed line.
  • Hover mouse over a variable to popup its value

17
BDeparse
  • The B modules are different than most. They
    exist for digging into the perl internals. They
    are called via the O module.
  • Just nod your head. It's easier.
  • To run a script through Deparseperl MODeparse
    file.pl

18
Deparsing
  • The result of running your script through
    BDeparse is code that more closely approximates
    what Perl sees once it's finished optimizing and
    blowing away the syntactic sugar.
  • perl MODeparse e'print "_ gt f_\n" for
    sort fa cmp fb keys f'
  • foreach _ (sort fa cmp fb keys f)
    print "_ gt f_\n"

19
Why Deparse?
  • Deparse is great for those situations in which
    you start to believe that Perl isn't seeing your
    code the same way you are.
  • One common occurrence is with precedence.
  • Provide a p option, and Deparse will use
    parentheses to explicitly show every level of
    precedence.
  • perl MODeparse,-p file.pl
  • For more information, perldoc BDeparse
Write a Comment
User Comments (0)
About PowerShow.com