I wanna introduce an amazing tool that come with perl but probably
some of you might not aware of it, which I found was really helpful
for me. It is the debugger mode. You can invoke debugger mode in the
command line by preceding "-d" to your perl script. For example,
assuming you have a perl script named "test.pl", you can invoke
debugger mode by typing following commands in the terminal "perl -d
test.pl". So, you might be curious why using debugger mode? Here is
the couple advantages by using debugger mode;
1) when perl program does not generate your desired result, very
often, you will need to output your intermediate step results to a
file to see whether it's doing something weird. With debugger mode,
you can inspect the value of a variable by simply typing "p
$variable", or by typing "x $variable" for Hash or Array (Yes, it does
the same thing as Data::Dumper).
2) you can set breakpoints wherever and whenever you want.
Let me demonstrate below: I have a test.pl which is catted below.
zhigang@ubuntu:~$ cat -n test.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 my $i = 10;
5 for (my $j = 0; $j <= 10; $j++){
6 print ":D---$j\n";
7 }
zhigang@ubuntu:~$ perl -d test.pl
##invoke the debugger mode
Loading DB routines from perl5db.pl version 1.33
Editor support available.
Enter h or `h h' for help, or `man perldebug' for more help.
main::(test.pl:4): my $i = 10;
DB<1> l
## type l (it is lowercase of L) to see where you are which is
indicated by ==>
4==> my $i = 10;
5: for (my $j = 0; $j <= 10; $j++){
6: print ":D---$j\n";
7: }
DB<1> b 6
## set breakpoint at line 6
DB<2> c
## type c will continue you current position which is line4 to your
specified breakpoint which is line 6
main::(test.pl:6): print ":D---$j\n";
DB<2> p $j
## inspect the local variable $j, which is 0
0
DB<3> b 6 $j == 7 ##
Here you I instruct the debugger to set a breakpoint when local
variable value is equal to 7
DB<4> c
:D---0
:D---1
:D---2
:D---3
:D---4
:D---5
:D---6
main::(test.pl:6): print ":D---$j\n";
So much for it. Please you can direct to the here
(http://www.thegeekstuff.com/2010/05/perl-debugger/) and here
(http://perldoc.perl.org/perldebug.html) for more information.
--
---------------------------------------------------------------------------------------------
PhD Candidate in Plant Biology
Department of Botany and Plant Sciences
University of California, Riverside