3.1 Perl/TkThe Perl/Tk module, developed Nick Ing-Simmons, is one of the most popular and useful of the Perl extension modules. Perl/Tk is a toolkit that gives Perl the ability to create interactive and full-fledged GUI-driven applications. Writing GUIs can be complex, but Perl/Tk makes it easy by making available standardized libraries of reusable GUI code (widgets and controls) that you can select as appropriate. For those interested in writing Oracle DBA GUI applications of their own, we'll try to cover all the bases in this chapter, but we'll mostly focus on the tools currently available for those simply looking for ready-to-use database administration and tuning programs. For more information on generic Perl/Tk issues, these are the online and book resources we consider to be best:
For a quick example of what Perl/Tk can do for you, take a look at the widgets demonstration program in Figure 3-1, which comes automatically with the Perl/Tk installation. Figure 3-1. The Perl/Tk widget program's image interface3.1.1 Installing Perl/Tk under UnixAs with virtually every other Perl module we'll discuss in this book, you can install Perl/Tk from the online CPAN system in the following way: $ perl -MCPAN -e "shell" cpan shell -- CPAN exploration and modules installation (v1.59_51) ReadLine support enabled cpan> install Tk <RETURN> However, to gain far more control over the install and its tests, and to access all of the install information for the various Unix flavors, get hold of Perl/Tk's latest tarball from the following web site: Once you've downloaded the CPAN source into the temporary directory of your choice, you can install Perl/Tk manually as follows (we've demonstrated this with the Tk800.023.tar.gz file): $ gzip -d Tk800.023.tar.gz $ tar xvf Tk800.023.tar $ cd Tk800.023 Always comb religiously through the README and INSTALL files to make sure nothing special is required for your machine's setup: $ vi README INSTALL Once you're happy with your setup, you can proceed. Perl/Tk follows the usual pattern for installing most Perl modules. However, it is a large body of code, in comparison to most other Perl modules, so it often takes a few minutes to install, and you must ensure that the user running make test has access to the appropriate X Windows server, by running a command such as xhost +. Once the testing is complete, you'll also need to install Perl/Tk as root: $ perl Makefile.PL $ make $ make test $ make install We're now all set! All that rigid command-line discipline is about to change. Assuming that the installation ran typically smoothly, Perl/Tk is now installed and ready to GUI. 3.1.2 Installing Perl/Tk on Win32The corresponding ActiveState installation on Win32 is straightforward. Simply connect your PC to the Internet, as in Chapter 2, and then run through the following PPM commands, much as you did when installing Perl DBI and DBD-Oracle:
3.1.3 Combining Perl/Tk and Perl DBITo try Perl/Tk on for size, let's try searching for the widgets program demonstrated in Figure 3-1 as follows:
Those of you who are rolling your own might like to run through Example 3-1. This follows the general Perl/Tk program algorithm shown in Figure 3-2. Figure 3-2. The basic structure of Perl/Tk programsExample 3-1. HelloPtk.pl — trying Perl/Tk on for size#!/usr/bin/perl
use strict;
use warnings;
# Step 1: Get hold of the main Perl/Tk package.
use Tk;
# Step 2: Create the Main Window. Use the name of the program,
# held in the special Perl variable $0, to create the title.
my $mw = MainWindow->new(-title=>$0);
# Step 3: Pack a label onto the screen to hold our initial message.
$mw->Label(-text=> "Hello Perl/Tk", -anchor=>'center'
)->pack(-side=>'top');
# Step 4: Create a button to neatly exit the program.
$mw->Button( -text=>'Exit',
-command=>\&doExit )->pack(-side=>'bottom');
# Step 5: Launch the Perl/Tk looping process, to display window.MainLoop( );
# Step 6: Create an exit subroutine.
sub doExit { exit 0; }
We've run through the following program steps in a little more detail:
Figure 3-3. HelloPtk.pl under both Win32 and LinuxExciting as HelloPtk.pl may be, both in looks and execution, the real fun begins when we start to combine Perl/Tk with Perl DBI. In Example 3-2 we've expanded HelloPtk.pl to work out the time according to SYSDATE. You can see this program at work in Figure 3-4. Figure 3-4. WhatIsTheTime.pl in action, under UnixExample 3-2. WhatIsTheTime.pl — Combining Perl/Tk and Perl DBI#!/usr/bin/perl # Step 1: Get hold of the main Perl/Tk package, DBI, and set the # Oracle Environment, plus set the database connection and SQL. use Tk; use DBI; use strict; use warnings; my $dbh = DBI->connect( 'dbi:Oracle:orcl', 'scott', 'tiger', { RaiseError=>1, AutoCommit=>0 } ); my $sql = qq{ SELECT TO_CHAR(SYSDATE, 'HH:MI:SS') FROM DUAL }; my $mw = MainWindow->new(-title=>$0); # Set the main window up # Step 2: Get the latest time from the Oracle database. my oracleTime; getTheOracleTime( ); # Step 3: Pack a simple button onto the screen, to ask Oracle for the # current SYSDATE time. Assign the appropriate callback. $mw->Button(-text=>"What's the Time, according to Oracle?", -command=> \&getTheOracleTime )->pack(-side=>'top'); # Step 4: Pack a label onto the screen holding the SYSDATE time. $mw->Label(-textvariable=> \$oracleTime, -anchor=>'center' )->pack(-side=>'bottom'); # Create another button to neatly exit the program. $mw->Button( -text=>'Exit', -command=>\&doExit )->pack(-side=>'bottom'); # Launch the Perl/Tk looping process, to display the window! :-) MainLoop( ); # Step 5: Create the two required subroutines. sub getTheOracleTime { my $sth = $dbh->prepare( $sql ); $sth->execute( ); ($oracleTime) = $sth->fetchrow_array( ); } sub doExit { $dbh->disconnect( ); # A clean and graceful disconnection 8-) exit; } In the following list we've indicated the main differences between this program and the original HelloPtk.pl skeleton:
Although this is a rather stripped-down example, it does show how easy it is to quickly combine Perl/Tk and Perl DBI in order to develop your own applications. Before you know it, you'll have built an entire collection of Perl/Tk widgets that do all sorts of wonderful things — and you'll fail to understand how you ever lived without them. |