8. Other Oddments
Contents:
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 DebuggerFirst 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:
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. Debugger CommandsThe debugger understands the following commands:
Using the DebuggerIf 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:
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. Debugger CustomizationTo 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/'; Readline SupportAs 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. Editor Support for DebuggingIf 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.) Debugger InternalsWhen 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`).
Debugger BugsIf 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. Alternative Debuggers: The Perl ProfilerIf 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. Other Debugging ResourcesYou did try the -w switch, didn't you? | ||||||||||||||||||||||||||||||
|