The debugger as a perl shell

Starting a Session (perl -d)

For an example of its simplest usage, start the debugger by giving it something innocuous (like a 0 (zero)) as the evaluation argument to the debugger via the command line:

This way of invoking the debugger works just as well on Microsoft Windows and Mac OS X as it does on the many flavors of Un*x.

A simple expression (perl -d -e 0)
The “-e” supplies a complete program on the command line for Perl to run –
for more info on this see perlrun

perldb@monkey> B<perl -d -e 0>
Default die handler restored.

Loading DB routines from perl5db.pl version 1.19
Editor support available.

Enter h or `h h’ for help, or `man perldebug’ for more help.

main::(-e:1): 0
DB<1>

Perl stops at the first executable statement. In this example, this is 0,
a perfectly valid (though minimal) Perl expression.

If you continue now, you’ll just fall off the end of the program.

Although this example may not immediately appear to be very useful, at this point you can in fact interact completely with the debugger. You can look at any environment variables (“V”) and you can also check out the help (“h”) page/s, and explore your surroundings.

To get the help page simply type “h” at the command prompt:

C<>
DB<1> B<h>
List/search source lines: Control script execution:
l [ln|sub] List source code         T Stack trace
– or . List previous/current line  s [expr] Single step [in expr]
w [line] List around line            n [expr] Next, steps over subs
f filename View source in file    <CR/Enter> Repeat last n or s
/pattern/ ?patt? Search forw/backw r Return from subroutine
v Show versions of modules     c [ln|sub] Continue until position
Debugger controls: L List break/watch/actions
O […] Set debugger options     t [expr] Toggle trace [trace expr]
<[<]|{[{]|>[>] [cmd] Do pre/post-prompt b [ln|event|sub] [cnd] Set breakpoint
! [N|pat] Redo a previous command d [ln] or D Delete a/all breakpoints
H [-num] Display last num commands a [ln] cmd Do cmd before line
= [a val] Define/list an alias      W expr Add a watch expression
h [db_cmd] Get help on command A or W Delete all actions/watch
|[|]db_cmd Send output to pager ![!] syscmd Run cmd in a subprocess
q or ^D Quit                           R Attempt a restart
Data Examination: expr Execute perl code, also see: s,n,t expr
x|m expr Evals expr in list context, dumps the result or lists methods.
p expr Print expression (uses script’s current package).
S [[!]pat] List subroutine names [not] matching pattern
V [Pk [Vars]] List Variables in Package. Vars can be ~pattern or !pattern.
X [Vars] Same as “V current_package [Vars]”.
For more help, type h cmd_letter, or run man perldebug for all docs.
DB<2>

A perl shell

You can run any Perl code you like in this environment, you can think of it as a form of Perl Shell.

Perhaps you can’t remember whether the results of a regular expression are given in scalar or array context. Instead of looking it up in perlre, you might choose to just try it out.

We’ll use “p”, which is just Perl’s print command in disguise, to see what is happening, first create a string $str.

C<>
DB<2> B<$str = ‘some string’>
DB<3>

Now print $str.

See “p” in examining-data.

C<>
DB<3> B<p $str>
some string
DB<4>

Now use $res to capture the return value of a regex and print the result.

C<>
DB<4> B<$res = $str =~ /(\w+)\s+(\w+)/>
DB<5> B<p $res>
1
DB<6>

Now use @res to capture and print that.

C<>
DB<7> B<@res = $str =~ /(\w+)\s+(\w+)/>
DB<8> B<p @res>
somestring
DB<9>

Use “x” to dump the results to get a clearer picture.

C<>
DB<9> B<x \@res>
0 ARRAY(0x844df5c)
0 ‘some’
1 ‘string’
DB<10>

Extract from the Perl Debugger Pocket Reference book.

Find this content useful? Share it with your friends or comment below!

Leave a Comment