Perl Debugging Tools

Document Sample
Perl Debugging Tools Powered By Docstoc
					Perl Debugging Tools
    warn() & die()
     Data::Dumper
      B::Deparse
      __DATA__
       perldebug
         ptkdb
               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#]"
                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.
            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!!
                   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.
  – <DATA> 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. . .
                 Data::Dumper
• Very useful function for seeing exactly what is
  contained in a variable
• To use, first import the module:
• use Data::Dumper;
• 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 Data::Dumper for more info
       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";'
         More Useful Switches
• -n : Automatically open each file on the command
  line and loop through.
• perl –ne 'print unless $_ < 10' *.txt
  – #!/usr/bin/env perl
    for (@ARGV){
         if (!open my $f, '<', $_){
              warn "Can't open $_: $!";
         } else {
              while (<$f>) {
                   print unless $_ < 10;
              }
         }
    }
               One step further
• -p : Automatically open each file on the command line
  and loop through, printing each line.
• perl –pe '$_=substr($_,0,10)' *.bat >out.txt
   – #!/usr/bin/env perl
     for (@ARGV){
          if (!open my $f, '<', $_){
               warn "Can't open $_: $!";
          } else {
               while (<$f>) {
                    $_ = substr($_, 0, 10);
               } continue
                    print;
               }
          }
     }
               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 –MData::Dumper –e'… print Dumper(\$f)'
• for more information on all command line
  switches, see
  perldoc perlrun
           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.
                    now what?
• The debugger halts just before the first run-time
  executable statement, and prints that statement to
  the screen, giving the prompt:
• DB<1>
• 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
              Debugger commands
•   s – Single-step, entering functions
•   n – Single-step over functions
•   <ENTER> – 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
               Debugger
• Those are just a quick summary of the
  available commands. For more
  information, use h or h h within the
  debugger, or read:
  perldoc perldebug
              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 DEVEL::ptkdb'
• On Windows, using Activestate, use PPM
  –   ppm
  –   search ptkdb
  –   install <#>
  –   if search yields no results, run this first, then try again:
       • repository add BdP http://www.bribes.org/perl/ppm
               Running ptkdb
• perl –d:ptkdb 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
                B::Deparse
• 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 Deparse:
  perl –MO=Deparse file.pl
                Deparsing
• The result of running your script through
  B::Deparse is code that more closely
  approximates what Perl sees once it's finished
  optimizing and blowing away the syntactic
  sugar.
• perl –MO=Deparse   –e'print "$_ => $f{$_}\n" for
  sort {$f{$a} cmp   $f{$b}} keys %f'
• foreach $_ (sort   {$f{$a} cmp $f{$b}} keys %f)
  {
      print "$_ =>   $f{$_}\n";
  }
               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 –MO=Deparse,-p file.pl
• For more information,
  perldoc B::Deparse