start page | rating of books | rating of authors | reviews | copyrights

Book HomeMastering Perl/TkSearch this book

1.6. Debugging and PrototypingPerl/Tk Programs

Debugging a Perl/Tk program need not be different from debugging a nongraphical program; you can always sprinkle warn statements throughout the code to track progress and display intermediate results. We suggest using warn rather than print for three reasons: it adds the newline to the message automatically; the output includes the line number of the warn statement; and the output goes to STDERR, which is not normally buffered, thus the output appears immediately. Furthermore, you type fewer characters.

You normally run programs by typing the program name at the command prompt:

% hello_world

or:

C:\>perl hello_world

When you invoke the program this way, any printed output goes to that terminal window. If you don't put a \n on the end of the string to be printed, you won't see the information actually printed until you quit the program. You may have to unbuffer a file handle by setting the special Perl variable $|. If you use warn rather than print, these drawbacks are eliminated.

If that old-fashioned way isn't to your liking, perhaps the slightly newer old-fashioned way of using the standard Perl debugger is. The debugger has built-in Tk support, though you must use the O command and enable it by setting the variable tkRunning:

[bug@Pandy atk]$ perl -de 0
Default die handler restored.

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

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

main::(-e:1):   0
  DB<1> O tkRunning
           tkRunning = '1'
  DB<2> use Tk    
  DB<3> $mw = MainWindow->new
  DB<4> $b = $mw->Button(-text => 'Beep', -command => sub{$mw->bell})
  DB<5> $b->pack
  DB<6> x $b
0  Tk::Button=HASH(0x82ed434)
   '_TkValue_' => '.button'
  DB<7> q

As you see, we can not only print debug information, but also do simple prototyping.

An even better environment for this sort of activity is the program ptksh. It's part of a standard Perl/Tk installation and, as its name suggests, it's a Perl/Tk shell that allows us to interactively enter and test Perl and Tk commands. Figure 1-3 shows a sample ptksh session.

Figure 1-3

Figure 1-3. ptksh session

If you're really into graphical debugging, treat yourself to the CPAN module Devel::ptkdb, an excellent, sophisticated Perl/Tk debugger. Simply invoke Perl with a -d argument such as this:

[bug@Pandy atk]$ perl -d:ptkdb group

Figure 1-4 shows a ptkdb session.

Figure 1-4

Figure 1-4. ptkdb session



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.