Item 39: Test things by using the debugger as a Perl shell.

Item 39: Test things by using the debugger as a Perl "shell."

The Perl debugger is, naturally, a handy tool for debugging Perl programs. But it is useful also for testing things out or for just fooling around. To enter the debugger, you need to supply a program. The program " " will suffice:

 %  perl -d -e 0  Stack dump during die enabled outside of evals.  Loading DB routines from patch level 0.94  Emacs support available.  Enter h or `h h' for help.  main::(-e:1):   0    DB<1> 

Once you're inside the debugger, your main tool for the purposes of "just testing things" will be the x command, which executes code in a list context and dumps the result in a structured way. You can also use the " Perl" commandany command that isn't recognized as a debugger command will be executed as a line of Perl. So, how does this work? Let's suppose you're curious about some bit of Perl trivia, such as whether your version of Perl allows you to delete hash slices. You start up the debugger, then type something like:

 DB<1>  %x = ( a => 1, b => 2, c => 3, d => 4 )  DB<2>  x delete @x{ 'a', 'b' }  

If you're running an older version of Perl, you'll see:

 delete argument is not a HASH element at (eval 6) line 2, <IN> chunk 2. 

Oh, well. But in a newer version, you'll see:

 DB<2>  x delete @x{ 'a', 'b' }  0  1  1  2 

The response is the return value of delete , which is a list of the deleted values.

You can examine the contents of the hash with x , but just using the direct approach won't yield a pretty result:

 DB<3>  x %x  0  'c'  1  3  2  'd'  3  4 

The result is the hash unwound into a list, which doesn't read very well. What you actually need is:

 DB<4>  x \%x  0  HASH(0x1153b0)     'c' => 3     'd' => 4 

Print reference to %x instead.

The debugger dumps hash references in a more understandable format, as you can see.

Another useful tool inside the debugger is the otherwise little-used Perl do operator. When applied to a string, do treats the string as a filename and eval s the contents of that file in the current context. This provides a pleasant alternative to typing or pasting snippets of code over and over again:

Suppose this is in the file tryme :

 @x{'A'..'J'} = 0..9; 

Create a table of characters and some associated numbers .

Now, feed it to the debugger:

 DB<10>  do 'tryme'  DB<11>  x \%x  0  HASH(0x106c50)     'A' => 0     'B' => 1     'C' => 2     'D' => 3     'E' => 4     'F' => 5     'G' => 6     'H' => 7     'I' => 8     'J' => 9 

(Hmm . . . I think we've hit a resonance in Perl's hash algorithm!)

Are you not sure exactly what variables a particular hunk of code defines? Execute it in a different package:

 DB<17> package foo; do 'tryme' 

Then take a look at its symbol table:

 DB<18>  x \%foo::  0  HASH(0x28f8bc)     'x' => *foo::x 

If you want to see the contents of the variables in a package, rather than just their names , use the V command:

 DB<19>  V foo  %x = (     'A' => 0     'B' => 1     'C' => 2     'D' => 3     'E' => 4     'F' => 5     'G' => 6     'H' => 7     'I' => 8     'J' => 9 

The V command and the X command (like V but for the current package) also can be used to dump individual variables, but I think it's simpler to use x for everything, because it works for arbitrary expressions. You, of course, may use whichever you prefer. Let your imagination run wild. Perhaps you will not use the debugger to debug code. In fact, I generally don't use itI'm an atavistic user of print statements myself . But regardless of whether you do your debugging with the debugger, keep its other uses in mind.

Effective Perl Programming. Writing Better Programs with Perl
Effective Perl Programming: Writing Better Programs with Perl
ISBN: 0201419750
EAN: 2147483647
Year: 1996
Pages: 116

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: