Perl
  Home arrow Perl arrow Page 5 - Using The Perl Debugger
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PERL

Using The Perl Debugger
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 27
    2003-06-25


    Table of Contents:
  • Using The Perl Debugger
  • Step By Step
  • Breaking Free
  • A Watchful Eye
  • Acts Of Madness
  • Test Drive

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Using The Perl Debugger - Acts Of Madness
    ( Page 5 of 6 )

    The Perl debugger also allows you to define actions - code that is executed when a specific line is reached. This comes in handy when you need to try a piece of code with different variable values, or print notification before specific lines are executed.

    Actions are set with the "a" command, which needs a line number and a line of code to be executed before the debugger executes the specified line. Consider the following example, which demonstrates:


    DB<7> l 24-44
    24==> foreach $line (@file)
    25 {
    26 # split on record delimiter
    27: @record = split(":", $line);
    28
    29 # print username if UID >= 500
    30: if ($record[2] >= 500)
    31 {
    32: print "$record[0]($record[2])\n";
    33: $count++
    34 }
    35 }
    DB<10> b 30
    DB<11> a 30 print "Checking UID $record[2]\n"

    In this case, when the debugger reaches line 30, it will automatically halt (because I set a breakpoint) and also print the value of the second element of the array $record (because I told it to via the "a" command).


    DB<21> c
    Checking UID 3
    main::(passwd.pl:30): if ($record[2] >= 500)
    main::(passwd.pl:31): {
    DB<21> c
    Checking UID 534
    all(534)
    main::(passwd.pl:30): if ($record[2] >= 500)
    main::(passwd.pl:31): {
    Checking UID 516
    ...

    Actions can be deleted with the "A" command, which can be supplied with either a line number


    DB<12> A 27

    or the * wildcard.


    DB<13> A *
    Deleting all actions...

    * Following The Trail

    As you might have guessed from my frequent use of this command in some of the previous examples, the "L" command provides a list of all currently-set breakpoints, actions and watch-expressions.


    DB<4> L
    passwd.pl:
    27: @record = split(":", $line);
    break if (1)
    action: print $line
    Watch-expressions:
    @record

    You can also obtain a list of previously-entered debugger commands with the "H" command, which displays a command history,


    DB<7> H
    6: b 11
    5: A *
    4: B *
    3: l 19-25
    2: v 18
    1: l 12

    and even repeat previous commands with the "!" command.


    DB<7> ! 4
    B *
    Deleting all breakpoints...

    You can obtain a stack trace with the "T" command, as in the following
    example:


    DB<5> T
    . = DBI::_setup_driver('DBD::mysql') called from file `/usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi/DBI.pm' line 629 $ = DBI::install_driver('DBI', 'mysql') called from file `/usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi/DBI.pm' line 497 $ = DBI::connect('DBI', 'DBI:mysql:database=db198;host=localhost', 'root', 'secret', ref(HASH)) called from file `mailer.pl' line 16

    This is fairly easy to decipher, so long as you start with the last line first. In the example above, it's clear to see that the script called the
    DBI::connect() function, which called DBI::install_driver(), which in turn invoked DBI::_setup_driver() from the DBI.pm module. This stack trace is useful to evaluate the control flow within your program and the libraries it interacts with.

    You can also customize the debugger with the "o" command, which lets you alter debugger options. Try typing it at the debugger prompt and you should see something like this:


    DB<1> o
    hashDepth = 'N/A'
    arrayDepth = 'N/A'
    CommandSet = '580'
    dumpDepth = 'N/A'
    DumpDBFiles = 'N/A'
    DumpPackages = 'N/A'
    DumpReused = 'N/A'
    compactDump = 'N/A'
    veryCompact = 'N/A'
    quote = 'N/A'
    HighBit = 'N/A'
    undefPrint = 'N/A'
    globPrint = 'N/A'
    PrintRet = '1'
    UsageOnly = 'N/A'
    frame = '0'
    AutoTrace = '0'
    TTY = '/dev/tty'
    noTTY = 'N/A'
    ReadLine = '1'
    NonStop = '0'
    LineInfo = '/dev/tty'
    maxTraceLen = '400'
    recallCommand = '!'
    ShellBang = '!'
    pager = '|/usr/bin/less -isr'
    tkRunning = 'N/A'
    ornaments = 'us,ue,md,me'
    signalLevel = '1'
    warnLevel = '1'
    dieLevel = '1'
    inhibit_exit = '1'
    ImmediateStop = 'N/A'
    bareStringify = 'N/A'
    CreateTTY = '3'
    RemotePort = 'N/A'
    windowSize = '10'

    As you may have guessed, these are all internal debugger options which can be customized as per your requirements. To alter a particular options, simply use the "o" command followed by the option name and its value, as
    below:


    DB<4> o windowSize=15
    windowSize = '15'

    Here's a brief list of the more interesting options available for
    customization:

    "RecallCommand" - the command used to repeat previous commands ("!) by default);

    "pager" - the program to use when paging through long screens of output;

    "hashDepth" - the depth to which hashes are displayed;

    "arrayDepth" - the depth to which arrays are displayed;

    "dumpDepth" - the depth to which structures are displayed;

    "inhibit_exit" - continue debugger session even after the script has ended;

    "windowSize" - number of lines of code to display with "v" and "l" commands;



     
     
    >>> More Perl Articles          >>> More By icarus, (c) Melonfire
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek