Jul 2, 2013

Perl Debug

Perl Debug is simple tool to easily debug perl scripts.

Few people like me find it difficult using Debug, but to be frank this is quite simple and straight forward. It makes the life of programmer easy to trace the bugs in a fast mode.

Without this, programmer needs to write unnecessary Print statements inside code and check where exactly things going wrong.

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, dynamically pass 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.

Debug Interactively:
Andrew E. Page, written an useful CPAN module Devel::Ptkdb, using this we can debug interactively.

This makes life much easier, no need to use <debug> prompt instead one can use the interactive mode to set the break points and pass the values to arguments dynamically.


How to Debug Perl Script Interactively using above module
perl -d:ptkdb myscript.pl  (for graphical representation of debugging)

Normal Debugger
perl debugger works using -d switch
Once you execute script using -d switch, it will change to debug prompt as mentioned below :
DB<1> ....
DB<2> ....

Some commonly used commands in Debug mode:

1) "s"  (Stepping through line by line execution) 
Keep pressing "s" will execute line by line

2) "l"  (List line) 
e.g.,
l 20                   #Lists line 20
l 20-25             #Lists 20 to 25 lines of script
l <subroutine>   #Lists a sub-routine

3) "b" (Setting break point on subroutine)
    "b" <subroutine>  
     #This will allow you to set a break point (set a break point on first line of subroutine)

4) "c" <subrutine>    
#This will take you directly to that break point (set one-time bkpt at subname and continue)

5) "q"                         
#It will quit  the debugger        


No comments:

Post a Comment