Loginskip to content

Archive for October, 2006

7.3.2 ptkdb ptkdb, developed by Andrew E. Page,

Sunday, October 29th, 2006

More than a command line, less than a GUI, the free and insanely powerful GNU text editor includes quasi-graphical support for a simple but intuitive interface to the perl debugger via the cperlmode (see Chapter 3). To start a debug session in the active buffer, enter M-x perldbRET, and you’ll get something like Figure 7-3, where one subwindow contains the source code, and the other is for interactions with the debugger. Figure 7-3. The emacs interface to the Perl debugger via cperl-mode The syntax highlighting is evident in the source code when viewed in color. (Keywords in blue, subroutine prototypes in heavy blue, comments in red, and double-quoted strings in gray; if you don’t like any of those assignments, they are easy to change.) Emacs is available with any Linux system and can be downloaded for virtually any platform from http://www.gnu.org/software/emacs/emacs.html. Don’t overlook the use of debuggers as a teaching tool. I often use ptkdb to monitor the evolution of a variable (scalar, array, hash) as a program executes. Such a demonstration allows a new programer to observe a variable’s value and scope in various situations which is quite useful when you are forced to explain the difference between lexical variables and package variables.
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp hosting services

7.3.2 ptkdb ptkdb, developed by Andrew E. Page,

Sunday, October 29th, 2006

7.3.2 ptkdb ptkdb, developed by Andrew E. Page, is a free, Tk-based, graphical interface to the built-in perl debugger, with a clean and intuitive design. Obtain ptkdbfrom http://search.cpan.org/search?dist=Devel-ptkdb. It also requires PerlTk by Nick Ing- Simmons (http://search.cpan.org/search?dist=Bundle-Tk). ptkdbis invoked as a debugger plugin, so to debug myprog.pl, type perl -d:ptkdb myprog.pl There is little we need to add to the description of ptkdb. In Figure 7-2, we see that the current line is highlighted, nonbreakable line numbers are struck through, and the tabbed panes on the right are for inspecting expressions, subroutines, and breakpoints. The appearance of the interface can be extensively customized. Figure 7-2. The ptkdb interface One key advantage of ptkdbis that it can be used to debug a Perl program used in a CGI process. We give examples of this in Chapter 13. 7.3.3 Emacs
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp hosting services

current statement, you may see it jump around

Sunday, October 29th, 2006

type_spam() was called from within the enclosing block). Unfortunately, in this case, if the result of the function is not used by the caller, you’re out of luck. 7.3 Getting Graphical Programmers can choose from several graphical interfaces to the Perl debugger. We’ll describe two of the free alternatives.[4] We won’t provide much detail describing their use since they are simply graphical layers on top of Perl’s internal debugger; all you need to do is find the buttons corresponding to the commands just described. [4] There are a number of commercial (i.e., not free) products offering GUI Perl debugger interfaces and other IDE functionality. We omitted them merely because we didn’t want to appear biased toward or against any particular vendor by inadvertently leaving one out. 7.3.1 ddd ddd, developed by Andreas Zeller, is a GNU graphical interface to many debuggers, including Perl’s; obtain it from http://www.gnu.org/software/ddd/ddd.html. No Windows port currently exists, but it builds easily enough on Linux when you download the requisite RPMs or package files. In Figure 7-1, note the breakpoint, current statement pointer, and various displays. Figure 7-1. ddd used as an interface to the Perl debugger (The application is an object-oriented program for solving the “N Queens” problem: Print out all the possible ways of placing n queens on an n x n chessboard so that no one is attacked by another.)
Note: If you are looking for cheap and quality provider to host and run your java application check Astra java hosting services

current statement, you may see it jump around

Sunday, October 29th, 2006

current statement, you may see it jump around oddly. As recently as version 5.004_04 of perl, this could be observed in a program like the following: 1 my @a = qw(one two three); 2 while ($_ = pop @a) 3 { 4 print “$_n”; 5 } 6 1; See what happens when we step through this, again using perl 5.004_04 or earlier: main::(while.pl:1): my @a = qw(one two three); DB<1> n main::(while.pl:6): 1; DB<1> main::(while.pl:4): print “$_n”; DB<1> three main::(while.pl:2): while ($_ = pop @a) DB<1> main::(while.pl:4): print “$_n”; DB<1> two In fact, if we set a breakpoint for line 6 and ran to it, we’d get there before the loop executed at all. So it’s important to realize that under some circumstances, what the debugger tells you about where you are can be confusing. If this inconveniences you, upgrade. 7.2.11 Another “Gotcha” If you set a lexical variable as the last statement of a block, there is no way to see what it was set to if the block exits to a scope that doesn’t include the lexical. Why would code do that? In a word, closures. For example, { # Start a closure-enclosing block my $spam_type; # This lexical will outlive its block sub type_spam { # … $spam_type = $spam_types[complex_func()]; } } In this case, either type_spam or some other subroutine in the closure block would have a good reason for seeing the last value of $spam_type. But if you’re stepping through in the debugger, you won’t see the value it gets set to on the last line because, after the statement executes, the debugger pops out to a scope where $spam_type is not in scope (unless
Note: If you are looking for cheap and quality provider to host and run your java application check Astra java hosting services

DB l 1-5 1==> my %phone; 2: while

Sunday, October 29th, 2006

Notice that trace mode causes the debugger to output the call tree when execution enters the print_line subroutine. 7.2.9 Programmatic Interaction with the Debugger You can put code in your program to force a call to the debugger at a particular point. For instance, suppose you’re processing a long input file line by line and you want to start tracing when it reaches a particular line. You could set a conditional breakpoint, but you could also extend the semantics of your input by creating “enable debugger” lines. Consider the following code: while () { $DB::trace = 1, next if /debug/; $DB::trace = 0, next if /nodebug/; # more code } When run under the debugger, this enables tracing when the loop encounters an input line containing “debug” and ceases tracing upon reading one containing “nodebug”. You can even force the debugger to breakpoint by setting the variable $DB::singleto 1, which also happens to provide a way you can debug code in BEGIN blocks (which otherwise are executed before control is given to the debugger). 7.2.10 Optimization Although the Perl debugger displays lines of code as it runs, it’s important to note that these are not what actually executes. Perl internally executes its compiled opcode tree, which doesn’t always have a contiguous mapping to the lines of code you typed, due to the processes of compilation and optimization. If you have used interactive debuggers on C code in the past, you may be familiar with this process. When debugging C programs on VAX/VMS, it was common for me to want to examine an important variable only to get the message that the variable was not in memory and had been “optimized away.” Perl has an optimizer to do as good a job as it can in the short amount of time people will wait for compilation of taking shortcuts in the code you’ve given it. For instance, in a process called constant folding, it does things like build a single string in places where you concatenate various constant strings together so that the concatenation operator need not be called at run-time. The optimization process also means that perl may execute opcodes in an order different from the order of statements in your program, and therefore when the debugger displays the
Note: If you are looking for good and high quality web space to host and run your java application check Vision java hosting services

DB l 1-5 1==> my %phone; 2: while

Sunday, October 29th, 2006

DB<1> l 1-5 1==> my %phone; 2: while (<>) { 3: my ($k, $v) = split; 4: $phone{$k} = $v; 5 } DB<2> W $phone{’555-1212′} DB<3> c Watchpoint 0: $phone{’555-1212′} changed: old value: undef new value: ‘Information’ main::(foo:2): while (<>) { DB<3> n main::(foo:3): my ($k, $v) = split; DB<3> p 555-1234 Weather Delete all watchpoints with a blank W command. 7.2.8 Trace: t The debugger’s t command provides a trace mode for those instances that require a complete trace of program execution. Running the program with an active trace mode: % perl -wd debug.pl main::(debug.pl:4): my @parole = qw(Salutations Hello Hey); DB<1> t Trace = on DB<1> n main::(debug.pl:6): print_line(@parole); DB<1> n main::print_line(debug.pl:13): main::print_line(debug.pl:14): main::print_line(debug.pl:15): main::print_line(debug.pl:16): Salutations Perl World main::print_line(debug.pl:14): main::print_line(debug.pl:15): main::print_line(debug.pl:16): Hello Perl World main::print_line(debug.pl:14): main::print_line(debug.pl:15): main::print_line(debug.pl:16): Hey Perl World main::print_line(debug.pl:14): main::print_line(debug.pl:15): my @parole = @_; foreach (@parole) { print “$_ Perl Worldn”; foreach (@parole) { print “$_ Perl Worldn”; foreach (@parole) { print “$_ Perl Worldn”; foreach (@parole) { main::(debug.pl:7): print “Donen”;
Note: If you are looking for good and high quality web space to host and run your java application check Vision java hosting services

main::(debug.pl:4): my @parole = qw(Salutations Hello Hey); DB

Saturday, October 28th, 2006

Salutations Perl World Hello Perl World Greetings Perl World Done Debugged program terminated. L also lists any actions you have created. Delete all the installed actions with the A command. This process is commonly used to insert tracing code on the fly. For example, suppose you have a program executing a loop containing way too much code to step through, but you want to monitor the state of certain variables each time it goes around the loop. You might want to confirm what’s actually being ordered in a shopping cart application test (looking at just a fragment of an imaginary such application here): DB<1> l 79-91 79: while (my $item = shift @shopping_basket) 80 { 81: if ($item->in_stock) 82 { 83: $inventory->remove($item); 84: $order->add($item); 85 } 86 else 87 { 88: $order->back_order($item); 89: $inventory->order($item); 90 } 91 } DB<2> a 81 printf “Item: %25s, Cost: %5.2fn”, $item->name, $item->cost DB<3> c 92 Item: Forbidden Planet, Cost: 24.50 Item: Kentucky Fried Movie, Cost: 29.95 Item: Eraserhead, Cost: 14.75 main::(cart.pl:92): $customer->charge($order->total); DB<3> 7.2.7 Watch It: W Suppose you want to break on a condition that is dictated not by a particular line of code but by a change in a particular variable. This is called a watchpoint, and in Perl you set it with the W command followed by the name of a variable.[3] [3] In fact, Perl can monitor anything that evaluates to an lvalue, so you can watch just specific array or hash entries, for example. Let’s say you’re reading a file of telephone numbers and to whom they belong into a hash, and you want to stop once you’ve read in the number 555-1212 to inspect the next input line before going on to check other things: main::(foo:1): my %phone;

Hint: This post is supported by Gama besplatan domen provider

main::(debug.pl:4): my @parole = qw(Salutations Hello Hey); DB

Saturday, October 28th, 2006

main::(debug.pl:4): my @parole = qw(Salutations Hello Hey); DB<1> l 4==> my @parole = qw(Salutations Hello Hey); 5 6: print_line(@parole); 7: print “Donen”; 8 9 # Our subroutine which accepts an array, then prints 10 # the value of each element appended to “Perl World.” 11 sub print_line 12 { 13: my @parole = @_; DB<1> l 14: foreach (@parole) 15 { 16: print “$_ Perl Worldn”; 17 } 18 } DB<1> b 16 /Hey/ DB<2> c Salutations Perl World Hello Perl World main::print_line(debug.pl:16): print “$_ Perl Worldn”; DB<2> p Hey Notice that we’ve demonstrated several things here: the source listing command l, the conditional breakpoint with the criterion that $_must match /Hey/, and that $_is the default variable for the pcommand (because pjust calls print). The capability of the debugger to insert code that gets executed in the context of the program being debugged does not exist in compiled languages and is a significant example of the kind of thing that is possible in a language as well designed as Perl. The command L lists all breakpoints. 7.2.6 Taking Action: a, A An even more advanced use of the facility to execute arbitrary code in the debugger is the action capability. With the a command (syntax: aline code), you can specify code to be executed just before a line would be executed. (If a breakpoint is set for that line, the action executes first; then you get the debugger prompt.) The action can be arbitrarily complicated and, unlike this facility in debuggers for compiled languages, lets you reach into the program itself: main::(debug.pl:4): my @parole = qw(Salutations Hello Hey); DB<1> a 16 s/Hey/Greetings/ DB<2> c

Hint: This post is supported by Gama besplatan domen provider

Examine references to hashes instead of the hashes

Saturday, October 28th, 2006

Examine references to hashes instead of the hashes themselves in the debugger to get well-formatted output. 7.2.3 Examining Source: l, -, w, . Sometimes you want more of the context of your program than just the current line. The following commands show you parts of your source code: l List successive windows of source code starting from the current line about to be executed. lx+y List y + 1 lines of source starting from line x. lx-y List source lines x through y. -List successive windows of source code before the current line. w List a window of lines around the current line. w line List a window of lines around line. . Reset pointer for window listings to current line. Source lines that are breakable (i.e., can have a breakpoint inserted before them see the following section) have a colon after the line number. 7.2.4 Playing in the Sandbox Since the debugger is a full-fledged Perl environment, you can type in Perl code on the fly to examine its effects under the debugger;[1] some people do this as a way of testing code quickly without having to enter it in a script or type in everything perfectly before hitting end- of-file. (You just saw us do this at the end of section 7.2.2.) [1] So, you might wonder, how would you enter Perl code which happened to look like a debugger command (because you’d defined a subroutine l, perhaps)? In versions of Perl prior to 5.6.0, if you enter leading white space before text, the debugger assumes it must be Perl code and not a debugger command. So be careful not to hit the space bar by accident before typing a debugger command. This was no longer true as of version 5.6.0. Type perl-de0 to enter this environment.[2] Let’s use this as a sandbox for testing Perl constructs: [2] There are many expressions other than 0 that would work equally well, of course. Perl just needs something innocuous to run. % perl -de 0 DB<1> $_ = ‘What_do*parens-in=split+do%again?’; DB<2> @a = split /(W+)/; 0 DB<3> x @a ‘What_do’ 1 ‘*’ 2 3 ‘parens’ ‘-’

Note: If you are looking for good and affordable webspace to host and run your servlet application check Virtualwebstudio servlet hosting services

Examine references to hashes instead of the hashes

Saturday, October 28th, 2006

4 ‘in’ 5 ‘=’ 6 7 ’split’ ‘+’ 8 ‘do’ 9 ‘%’ 10 11 ‘again’ ‘?’ You can even use this feature to change the values of variables in a program you are debugging, which can be a legitimate strategy for seeing how your program behaves under different circumstances. If the way your program constructs the value of some internal variable is complex and it would require numerous changes in the input to have it form the variable differently, then a good way of playing “What if?” is to stop the program at the right place in the debugger and change the value by hand. How would we stop it? Let’s see 7.2.5 Breakpointing: c, b, L An important feature of a debugger is the ability to allow your program to continue executing until some condition is met. The most common such condition is the arrival of the debugger at a particular line in your source. You can tell the Perl debugger to run until a particular line number with the c (for continue) command: main::(debug.pl:4): my @parole = qw(Salutations Hello Hey); DB<1> c 16 main::print_line(debug.pl:16): print “$_ Perl Worldn”; DB<2> What the debugger actually did was set a one-time breakpoint at line 16 and then executed your code until it got there. If it had hit another breakpoint earlier, it would have stopped there first. So what’s a breakpoint? It’s a marker set by you immediately before a line of code, invisible to anyone but the perl debugger, which causes it to halt when it gets there and return control to you with a debugger prompt. If you have a breakpoint set at a line of code that gets printed out with one of the source examination commands listed earlier, you’ll see a bnext to it. It’s analogous to putting a horse pill in the trail of bread crumbs the mouse follows so the mouse gets indigestion and stops to take a breather (we really have to give up this metaphor soon). You set a breakpoint with the b command; the most useful forms are blineor b subroutine to set a breakpoint either at a given line number or immediately upon entering a subroutine. To run until the next breakpoint, type c. To delete a breakpoint, use d line to delete the breakpoint at line number lineor D to delete all breakpoints. In certain situations you won’t want to break the next time you hit a particular breakpoint, but only when some condition is true, like every hundredth time through a loop. You can add a third argument to b specifying a condition that must be true before the debugger will stop at the breakpoint. For example,

Note: If you are looking for good and affordable webspace to host and run your servlet application check Virtualwebstudio servlet hosting services