ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы.



Programming Perl

Programming PerlSearch this book
Previous: 7.2 Library ModulesChapter 8Next: 8.2 Common Goofs for Novices
 

8. Other Oddments

Did you ever have a junk drawer? You know, one of those drawers where you put everything important enough to keep (like the spare key to the back door), but not important enough to have a place of its own (like the back door itself).

Well, this chapter is the junk drawer of the book. We stuffed many important (and a few not-so-important) things in this chapter. Read on.

8.1 The Perl Debugger

First of all, have you tried using the -w switch?

If you invoke Perl with the -d switch, your script runs under the Perl debugger. This works like an interactive Perl environment, prompting for debugger commands that let you examine source code, set breakpoints, dump out your function-call stack, change the values of variables, and so on. Any command not recognized by the debugger[1] is directly executed (eval'd) as Perl code in the current package.[2] This is so wonderfully convenient that you often fire up the debugger all by itself just to test out Perl constructs interactively to see what they do. Here's a common way to get that:

[1] Leading whitespace before a command would cause the debugger to think it's not a command for it, but rather for Perl, so be careful not to do that.

[2] The debugger uses the DB package for its own state information.

perl -d -e 42

In Perl, the debugger is not a separate program as it usually is in a typical programming environment. Instead, the -d flag tells the compiler to insert source information into the parse trees it's about to hand off to the interpreter. That means your code must first compile correctly for the debugger to work on it. Then when the interpreter starts up, it pre-loads a Perl library file containing the debugger itself.

8.1.1 Debugger Commands

The debugger understands the following commands:

h [command]

Prints out a help message.

If you supply another debugger command as an argument to the h command, it prints out the description for just that command. The command "h h" produces a more compact help listing designed to fit on one screen. If the output of the h command (or any command, for that matter) scrolls past your screen, just precede the command with a leading pipe symbol so it's run through your pager:

DB<1> |h
p expr

Same as "print DB::OUT expr" in the current package. In particular, since this is just Perl's own print function, this means that nested data structures and objects are not dumped, unlike with the x command. The DB::OUT handle is opened to /dev/tty (or perhaps an editor window) no matter where standard output may have been redirected to.

x expr

Evals its expression in a list context and dumps out the result in a pretty-printed fashion. Nested data structures are printed out recursively, unlike with the print command above.

V [pkg [vars]]

Display all (or some) variables in package (defaulting to the main package) using a data pretty-printer. (Hashes show their keys and values so you see what's what, control characters are made printable, nested data structures print out in a legible fashion, and so on.) Make sure you type the identifiers without a type specifier such as $ or @, like this:

V DB filename line

In place of a variable name, you can use ~pattern or !pattern to print existing variables whose names either match or don't match the specified regular expression.

X [vars]

Same as V currentpackage [vars].

T

Produce a stack backtrace. See below for details on its output.

s [expr]

Single step. Executes until it reaches the beginning of another statement, descending into subroutine calls. If an expression is supplied that includes function calls, it, too, will be single-stepped.

n

Next. Executes over subroutine calls, until it reaches the beginning of the next statement at this same level.

<CR>

Repeat last n or s command.

c [line]

Continue, optionally inserting a one-time-only breakpoint at the specified line.

l

List next few lines.

l min+incr

List incr+1 lines starting at min.

l min-max

List lines min through max.

l line

List a single line.

l subname

List first few lines from subroutine.

-

List previous few lines.

w [line]

List window (a few lines) around the given line, or the current one if no line is supplied.

.

Return debugger pointer to the last-executed line and print it out.

f filename

Switch to viewing a different file.

/pattern/

Search forward for pattern; final / is optional.

?pattern?

Search backward for pattern; final ? is optional.

L

List all breakpoints and actions for the current file.

S [[!]pattern]

List subroutine names matching (or not matching with "!") pattern. If no pattern is given, all subroutines are listed.

t

Toggle trace mode.

t expr

Trace through execution of expr.

b [line] [condition]

Set a breakpoint at line. If line is omitted, set a breakpoint on the line that is about to be executed. condition, if given, is evaluated each time the statement is reached, and a breakpoint is taken only if condition is true. Breakpoints may only be set on lines that begin an executable statement. Conditions don't use if:

b 237 $x > 30
b 33 /pattern/i

b subname [condition]

Set a (possibly conditional) breakpoint at the first line of the named subroutine.

d [line]

Delete a breakpoint at the specified line. If line is omitted, deletes the breakpoint on the line that is about to be executed.

D

Delete all installed breakpoints.

a [line] command

Set an action to be done before the line is executed. The sequence of steps taken by the debugger is:

  • Check for a breakpoint at this line.

  • Print the line if necessary (tracing).

  • Do any actions associated with that line.

  • Prompt the user if at a breakpoint or in single-step.

  • Evaluate the line.

For example, this will print out $foo every time line 53 is passed:

a 53 print "DB FOUND $foo\n"

A

Delete all installed actions.

O [opt[=val]]

Set or query values of options. val defaults to 1. opt can be abbreviated to the shortest unique string, which is why some options are uppercase and others are lowercase. Options are:

OptionValue
recallCommand ShellBang

The characters used to recall command or spawn shell. By default, these are both set to "!" (see below).

pager

Program to use for output of pager-piped commands (those beginning with a | character). By default, $ENV{PAGER} will be used.

PrintRet

Enables printing of return value after r command.

frame

Enables printing messages on entry and exit from subroutines.

The following options affect what happens with V, X, and x commands:

OptionValue
arrayDepth hashDepth

Print only to depth n ("" for all).

compactDump veryCompact

Change style of array and hash dump.

globPrint

Whether to print contents of globs.

DumpDBFiles

Dump arrays holding debugged files.

DumpPackages

Dump symbol tables of packages.

quote HighBit undefPrint

Change style of string dump.

tkRunning

Run Tk while prompting (with ReadLine).[3]

signalLevel warnLevel dieLevel

Level of verbosity.

[3] A Perl application is usually frozen when sitting at the debugger prompt. Tk support keeps the event loop of Tk running while reading the prompt.

During startup, options are initialized from $ENV{PERLDB_OPTS}. You can put additional initialization options TTY, noTTY, ReadLine, and NonStop there. Here's an example using the $ENV{PERLDB_OPTS} variable:

$ PERLDB_OPTS="N f=2" perl -d myprogram

This will run the script myprogram without human intervention, printing out the call tree with entry and exit points. Note that "N f=2" is equivalent to "NonStop=1 frame=2".

< command

Set an action to happen before every debugger prompt. A multi-line command may be entered by backslashing the newlines. command should be Perl code.

> command

Set an action to happen after the prompt when you've just given a command to return to executing the script. A multi-line command may be entered by backslashing the newlines. command should be Perl code.

! number

Redo a previous command (defaults to previous command).

! -number

Redo number'th-to-last command.

! pattern

Redo last command that started with pattern. See "O recallCommand", too.

!! cmd

Run cmd in a subprocess (which will read from DB::IN, write to DB::OUT). See "O shellBang", too.

H -number

Display last number commands. Only commands longer than one character are listed. If number is omitted, lists them all.

q or ^D

Quit. ("quit" doesn't quite work for this.)

R

Restart the debugger by execing a new session. It tries to maintain your history across this, but internal settings and command line options may be lost.

|dbcmd

Run debugger command, piping DB::OUT to $ENV{PAGER}.

||dbcmd

Same as |dbcmd but DB::OUT is temporarily selected as well. Often used with commands that would otherwise produce long output, such as

|V main

= [alias value]

Define a command alias, or list current aliases.

command

Execute command as a Perl statement. A semicolon is not needed at the end.

8.1.2 Using the Debugger

If you have any compile-time executable statements (code within a BEGIN block or a use statement), they will not be stopped by the debugger, although requires will.

The debugger prompt is something like:

DB<8>

or even:

DB<<17>>

where that number is the command number. A csh-like history mechanism allows you to access previous commands by number. For example, !17 would repeat command number 17. The number of angle brackets indicates the depth of the debugger. You get more than one set of brackets, for example, if you're already at a breakpoint and then print out the result of a function call that itself also has a breakpoint.

If you want to enter a multi-line command, such as a subroutine definition with several statements, you may escape the newline that would normally end the debugger command with a backslash. Here's an example:

DB<1> for (1..4) {         \
  cont:     print "ok\n";    \
  cont: }
  ok
  ok
  ok
  ok

Note that this business of escaping a newline is specific to interactive commands typed into the debugger.

Let's say you want to fire up the debugger on a little program of yours (let's call it camel_flea), and stop it as soon as it gets down to a function named infested. Here's how you'd do that:

shell_prompt% perl -d camel_flea
Stack dump during die enabled outside of evals.

Loading DB routines from perl5db.pl patch level 0.94
Emacs support available.

Enter h or `h h' for help.

main::(camel_flea:3):   $a = 1;
  DB<1>

The debugger halts your program right before the first run-time executable statement (but see above regarding compile-time statements) and asks you to enter a command. Contrary to popular expectations, whenever the debugger stops to show you a line of code, it displays the line it's about to execute, not the one it just executed.

Now, you'd like to stop as soon as your program gets to the infested function, so you enter a breakpoint there like so:

DB<1> b infested
DB<2> c

The debugger now continues until it hits that function, at which point it does this:

main::infested(camel_flea:12):      my bugs;

It might be nice to look at a window of source code around the breakpoint, so you use the w command:

DB<2> w
9:      } 
10:
11:     sub infested {
12==>b      my $bugs;
13:         return 3.5;
14:     } 
DB<2>

As you see, your current line is line 12, and it has a breakpoint on it.

Now, you'd like to see who called whom, so you ask for a stack backtrace:

DB<2> T
$ = main::infested called from file `Ambulation.pm' line 10
@ = Ambulation::legs(1, 2, 3, 4) called from file `camel_flea' line 7
$ = main::pests('bactrian', 4) called from file `camel_flea' line 4

The left-hand character up there ($ or @) tells whether the function was called in a scalar or list context (we bet you can tell which is which). There are three lines because you were three functions deep when you ran the stack backtrace. Here's what each line means:

  • Line number one says you were in the function main::infested when you ran the stack dump. It tells you the function was called in a scalar context from line 10 of the file Ambulation.pm. It also shows that it was called without any arguments whatsoever, meaning it was called as &infested.

  • Line number two shows that the function Ambulation::legs was called in a list context from the camel_flea file with four arguments.

  • Line number three shows that main::pests was called in a scalar context, also from camel_flea, but from line 4.

Limited control over the Perl debugger can also be managed from within your Perl script itself. You might do this, for example, to set an automatic breakpoint at a certain subroutine whenever a particular program is run under the debugger. Setting $DB::single to 1 will stop at the next statement as though you'd used the debugger's s command. If you set $DB::single to 2, it's equivalent to having just typed the n command. The $DB::trace variable can be set to 1 to simulate having typed the t command.

8.1.3 Debugger Customization

To modify the debugger, copy perl5db.pl from the Perl library to another file and modify it as necessary. You'll also want to set your PERL5DB environment variable to say something like this:

BEGIN { require "myperl5db.pl" }

You can do some customization by setting up a .perldb file with initialization code. For instance, you could make aliases like these (the last one is one people expect to be there):

$DB::alias{'len'}  = 's/^len(.*)/p length($1)/';
$DB::alias{'stop'} = 's/^stop (at|in)/b/';
$DB::alias{'ps'}   = 's/^ps\b/p scalar /';
$DB::alias{'quit'} = 's/^quit\b.*/exit/';

8.1.4 Readline Support

As shipped, the only command-line history mechanism supplied is a simplistic one that checks for leading exclamation points. This is fine for casual use. However, if you install the Term::ReadKey and Term::ReadLine modules from CPAN, you will have full editing capabilities much like GNU readline(3) provides. Look for these in the modules/by-module/Term directory on CPAN.

8.1.5 Editor Support for Debugging

If you have GNU emacs installed on your system, it can interact with the Perl debugger to provide an integrated software development environment reminiscent of its interactions with C debuggers.

Perl is also delivered with a start file for making emacs act like a syntax-directed editor that understands (some of) Perl's syntax. Look in the emacs/ directory of the Perl source distribution.

(Historically, a similar setup for interacting with vi and the X11 window system had also been available, but at the time of this writing, no debugger support for vi currently exists.)

8.1.6 Debugger Internals

When you call the caller function from package DB, Perl sets the @DB::args array to the arguments that stack frame was called with. It also maintains other magical internal variables, such as @DB::dbline, an array of the source code lines for the currently selected (with the debugger's f command) file. Perl effectively inserts a call to the function DB::DB(linenum) in front of every place that can have a breakpoint. Instead of a subroutine call it calls DB::sub, setting $DB::sub to the name of the called subroutine. It also inserts a BEGIN {require 'perl5db.pl'} before the first line, since no subroutine call is possible until &DB::sub is defined (for subroutines defined outside this file). In fact, the same is true if $DB::deep (how many levels of recursion deep into the debugger you are) is not defined.

At the start, the debugger reads your config file (./.perldb or ~/.perldb under UNIX), which can set important options. This file may define a subroutine &afterinit to be executed after the debugger is initialized.

After the config file is processed, the debugger consults the environment variable PERLDB_OPTS and parses it as arguments to the O opt=val debugger command.

The following options can only be specified at startup. To set them in your config file, call &parse_options("opt=val").

TTY

The TTY to use for debugging I/O.

noTTY

If set, goes in NonStop mode. On an interrupt, if TTY is not set, it uses the value of noTTY or /tmp/perldbtty$$ to find TTY using Term::Rendezvous. The current variant is to have the name of TTY in this file.

ReadLine

If false, a dummy ReadLine is used so that you can debug ReadLine applications.

NonStop

If true, no interaction is performed until an interrupt.

LineInfo

File or pipe to print line number info to. If it's a pipe, then a short, emacs-like message is used. Example config file:

&parse_options("NonStop=1 LineInfo=db.out");
sub afterinit { $trace = 1; }

The script will run without human intervention, putting trace information into the file db.out. (If you interrupt it, you had better reset LineInfo to something "interactive"!)

8.1.7 Debugger Bugs

If your program exits or dies, so too does the debugger.

You cannot get the stack frame information or otherwise debug functions that were not compiled by Perl, such as C or C++ extensions.

If you alter your @_ arguments in a subroutine (such as with shift or pop), the stack backtrace will not show the original values.

8.1.8 Alternative Debuggers: The Perl Profiler

If you wish to supply an alternative debugger for Perl to run, just invoke your script with the -d:module switch. One of the most popular alternative debuggers for Perl is DProf, the Perl profiler. As of this writing, DProf was not included with the standard Perl distribution, but it is expected to be included "real soon now."

Meanwhile, you can fetch the Devel::DProf module from CPAN. Assuming it's properly installed on your system, you can use it to profile the Perl program in mycode.pl by typing:

perl -d:DProf mycode.pl

When the script terminates, the profiler will dump the profile information to a file called tmon.out. A tool like dprofpp (also supplied with the Devel::DProf package) interprets the profile.

8.1.9 Other Debugging Resources

You did try the -w switch, didn't you?