One of Perl's better-kept secrets is its built-in debugger, a powerful utility that allows developers to rapidly track down errors in their Perl scripts. This article introduces you to the Perl debugger, explaining how to use it to step through scripts, set breakpoints, inspect variables and objects, watch expressions and perform stack traces.
You can set a watch-expression (an expression which triggers a halt if its value changes) with the "w" command, as in the following example:
DB<1> w $count DB<<3>> L Watch-expressions: $count
Now that the watch-expression has been set, you can see how it works by writing some code to alter the value of the $count variable. The debugger will display a message every time the variable's value changes.
DB<2> for ($count=0; $count<=10; $count++) { print "Hello!"; }; Watchpoint 0: $count changed: old value: undef new value: '0' DB<<3>> s main::((eval 13)[/usr/lib/perl5/5.8.0/perl5db.pl:17]:2): 2: for ($count=0; $count<=10; $count++) { print "Hello!"; };; DB<<3>> Watchpoint 0: $count changed: old value: '0' new value: '1' main::((eval 13)[/usr/lib/perl5/5.8.0/perl5db.pl:17]:2): 2: for ($count=0; $count<=10; $count++) { print "Hello!"; };; DB<<3>> Hello! main::((eval 13)[/usr/lib/perl5/5.8.0/perl5db.pl:17]:2): 2: for ($count=0; $count<=10; $count++) { print "Hello!"; };; DB<<3>> s Watchpoint 0: $count changed: old value: '1' new value: '2' for ($count=0; $count<=10; $count++) { print "Hello!"; };
Watch-expressions can be deleted with the "W" command; either specify the expression
DB<<3>> W $count
or delete all currently-set expressions with the * wildcard.
DB<<4>> W * Deleting all watch expressions ...
Note that adding watch-expressions can result in a performance penalty, so you should try and restrict yourself to not more than three or four at any given time.