2020ok  Directory of FREE Online Books and FREE eBooks

Free eBooks > Computers & Internet > Programming > Languages & Tools > TCL > Practical Programming In Tcl And Tk

Practical Programming In Tcl And Tk

by Brent Welch, Ken Jones, and Jeffrey Hobbs


Download Book
(Respecting the intellectual property of others is utmost important to us, we make every effort to make sure we only link to legitimate sites, such as those sites owned by authors and publishers. If you have any questions about these links, please contact us.)


link 1
link 2
link 3



About Book

Amazon.com
Despite its frequently obtuse syntax, Tcl/Tk enjoys a large and enthusiastic following. It's king of the world when it comes to building graphical user interfaces (GUIs) for C programs (particularly those running in X Windows environments), which is what the language originally was invented to do. Tcl/Tk (which is pronounced "tickle tee-kay," and which stands for "Tool Control Language/Toolkit" despite the abbreviation's unusual capitalization) is expanding its scope to encompass fields as diverse as voice scripting and molecular visualization. The latest edition of Practical Programming in Tcl and Tk, the fourth, offers an encyclopedic guide to Tcl/Tk that not only helps programmers solve problems, but enables them to conceive new applications for the language.

The new edition of Practical Programming in Tcl and Tk covers Tcl 8.4, particularly its versatile new Virtual File System (VFS) capabilities that allow you to include FTP sites and other remote directories in ordinary File Save and File Open interfaces. Further new coverage goes to Starkits, with which you can bundle Tcl/Tk programs for deployment, and some new Tk elements. Otherwise, the book is generally revised and improved with additional examples (the sample code has always been one of this book's main attractions) and intelligently laid-out keyword tables. It has a great lay-flat binding, too. This book (with John Ousterhout's Tcl and the Tk Toolkit) remains one of the two must-have Tcl/Tk books. --David Wall

Topics covered: The Tcl programming language and the Tk toolkit on which it most usually operates. Sections cover basic and advance programming techniques, specific aspects of Tk widgets, and extending the C library that defines Tcl. Copious examples make it easier to understand (and quickly employ) effective design strategies.

Joseph A. Konstan, Professor of Computer Science, University of Minnesota
"Brent Welch fills an important need for an introduction to Tcl/Tk with an applied focus and with coverage of many of the useful extensions available . . . I recommend this book to my new students . . . and I keep a copy handy for my own use."

Book Description

Practical Programming in Tcl/Tk is described as the "bible" for Tcl programmers. It is a guide to the Tcl/Tk programming language and GUI toolkit. This revision includes substantial updates to cover the new version 8.4-giving both an overview of the features, as well as details about every command in the language. The third edition, written on version 8.2, sold over 30,000 copies. Version 8.4 of Tcl - Tool Command Language-provides substantial updates to one of the most popular UNIX scripting languages. The latest release, includes the addition of a virtual filesystem (VFS), many additional programming widgets (spinbox, panedwindow, labelframe),and improved performance of about 20% over 8.3. The book provides a guide to the best ways to use the tooklit. It not only gives accurate details, but includes extensive examples that demonstrate the best way to use the toolkit. The authors are experts that have both developed the technology and used it to solve problems, so they have many valuable insights to relate to the readers.



Book Info
Covers user interface development, application integration techniques that leverage Tcl/Tk's cross-platform scripting capabilities, Tcl's compiler, and Tcl/Tk plug-ins for Netscape and Internet Explorer. Discusses Tcl's fundamental mechanisms and operating system interfaces. Softcover. CD-ROM included. DLC: Tcl (Computer prgramming language).

From the Inside Flap
Preface

Tcl stands for Tool Command Language — Tcl is really two things: a scripting language, and an interpreter for that language that is designed to be easy to embed into your application. Tcl and its associated graphical user-interface toolkit, Tk, were designed and crafted by Professor John Ousterhout of the University of California, Berkeley. You can find these packages on the Internet (as explained on page lii) and use them freely in your application, even if it is commercial. The Tcl interpreter has been ported from UNIX to DOS, Windows, OS/2, NT, and Macintosh environments. The Tk toolkit has been ported from the X window system to Windows and Macintosh.

I first heard about Tcl in 1988 while I was Ousterhout's Ph.D. student at Berkeley. We were designing a network operating system, Sprite. While the students hacked on a new kernel, John wrote a new editor and terminal emulator. He used Tcl as the command language for both tools so that users could define menus and otherwise customize those programs. This was in the days of X10, and he had plans for an X toolkit based on Tcl that would help programs cooperate with each other by communicating with Tcl commands. To me, this cooperation among tools was the essence of Tcl.

This early vision imagined that applications would be large bodies of compiled code and a small amount of Tcl used for configuration and high-level commands. John's editor, mx, and the terminal emulator, tx, followed this model. While this model remains valid, it has also turned out to be possible to write entire applications in Tcl. This is because the Tcl/Tk shell, wish, provides access to other programs, the file system, network sockets, plus the ability to create a graphical user interface. For better or worse, it is now common to find applications that contain thousands of lines of Tcl script.

This book was written because, while I found it enjoyable and productive to use Tcl and Tk, there were times when I was frustrated. In addition, working at Xerox PARC, with many experts in languages and systems, I was compelled to understand both the strengths and weaknesses of Tcl and Tk. Although many of my colleagues adopted Tcl and Tk for their projects, they were also just as quick to point out its flaws. In response, I have built up a set of programming techniques that exploit the power of Tcl and Tk while avoiding troublesome areas. This book is meant as a practical guide to help you get the most out of Tcl and Tk and avoid some of the frustrations I experienced.

It has been about 10 years since I was introduced to Tcl, and about five years since the first edition of this book. During the last several years I have been working under John Ousterhout, first at Sun Microsystems and now at Scriptics Corporation. I have managed to remain mostly a Tcl programmer while others in our group have delved into the C implementation of Tcl itself. I've been building applications like HTML editors, e-mail user interfaces, Web servers, and the customer database we run our business on. This experience is reflected in this book. The bulk of the book is about Tcl scripting, and the aspects of C programming to create Tcl extensions is given a lighter treatment. I have been lucky to remain involved in the core Tcl development, and I hope I can pass along the insights I have gained by working with Tcl.

Why Tcl?

As a scripting language, Tcl is similar to other UNIX shell languages such as the Bourne Shell (sh), the C Shell (csh), the Korn Shell (ksh), and Perl. Shell programs let you execute other programs. They provide enough programmability (variables, control flow, and procedures) to let you build complex scripts that assemble existing programs into a new tool tailored for your needs. Shells are wonderful for automating routine chores.

It is the ability to easily add a Tcl interpreter to your application that sets it apart from other shells. Tcl fills the role of an extension language that is used to configure and customize applications. There is no need to invent a command language for your new application, or struggle to provide some sort of user-programmability for your tool. Instead, by adding a Tcl interpreter, you structure your application as a set of primitive operations that can be composed by a script to best suit the needs of your users. It also allows other programs to have programmatic control over your application, leading to suites of applications that work well together.

The Tcl C library has clean interfaces and is simple to use. The library implements the basic interpreter and a set of core scripting commands that implement variables, flow control, and procedures (see page 22). There is also a set of commands that access operating system services to run other programs, access the file system, and use network sockets. Tk adds commands to create graphical user interfaces. Tcl and Tk provide a "virtual machine" that is portable across UNIX, Windows, and Macintosh environments.

The Tcl virtual machine is extensible because your application can define new Tcl commands. These commands are associated with a C or C++ procedure that your application provides. The result is applications that are split into a set of primitives written in a compiled language and exported as Tcl commands. A Tcl script is used to compose the primitives into the overall application. The script layer has access to shell-like capability to run other programs, has access to the file system, and can call directly into the compiled part of the application through the Tcl commands you define. In addition, from the C programming level, you can call Tcl scripts, set and query Tcl variables, and even trace the execution of the Tcl interpreter.

There are many Tcl extensions freely available on the Internet. Most extensions include a C library that provides some new functionality, and a Tcl interface to the library. Examples include database access, telephone control, MIDI controller access, and expect, which adds Tcl commands to control interactive programs.

The most notable extension is Tk, a toolkit for graphical user interfaces. Tk defines Tcl commands that let you create and manipulate user interface widgets. The script-based approach to user interface programming has three benefits:

Development is fast because of the rapid turnaround; there is no waiting for long compilations.

The Tcl commands provide a higher-level interface than most standard C library user-interface toolkits. Simple user interfaces require just a handful of commands to define them. At the same time, it is possible to refine the user interface in order to get every detail just so. The fast turnaround aids the refinement process.

The user interface can be factored out from the rest of your application. The developer can concentrate on the implementation of the application core and then fairly painlessly work up a user interface. The core set of Tk widgets is often sufficient for all your user interface needs. However, it is also possible to write custom Tk widgets in C, and again there are many contributed Tk widgets available on the network.

There are other choices for extension languages that include Visual Basic, Scheme, Elisp, Perl, Python, and Javascript. Your choice between them is partly a matter of taste. Tcl has simple constructs and looks somewhat like C. It is easy to add new Tcl primitives by writing C procedures. Tcl is very easy to learn, and I have heard many great stories of users completing impressive projects in a short amount of time (e.g., a few weeks), even though they never used Tcl before.

Java has exploded onto the computer scene since this book was first published. Java is a great systems programming language that in the long run could displace C and C++. This is fine for Tcl, which is designed to glue together building blocks written in any system programming language. Tcl was designed to work with C, but has been adapted to work with the Java Virtual Machine. Where I say "C or C++", you can now say "C, C++, or Java," but the details are a bit different with Java. This book does not describe the Tcl/Java interface, but you can find TclBlend on the CD-ROM. TclBlend loads the Java Virtual Machine into your Tcl application and lets you invoke Java methods. It also lets you implement Tcl commands in Java instead of C or C++.

Javascript is a language from Netscape that is designed to script interactions with Web pages. Javascript is important because Netscape is widely deployed. However, Tcl provides a more general purpose scripting solution that can be used in a wide variety of applications. The Tcl/Tk Web browser plugin provides a way to run Tcl in your browser. It turns out to be more of a Java alternative than a JavaScript alternative. The plugin lets you run Tcl applications inside your browser, while JavaScript gives you fine grain control over the browser and HTML display. The plugin is described in Chapter 20.

Tcl and Tk Versions

Tcl and Tk continue to evolve. See beedub/book/ for updates and news about the latest Tcl releases. Tcl and Tk have had separate version numbers for historical reasons, but they are released in pairs that work together. The original edition of this book was based on Tcl 7.4 and Tk 4.0, and there were a few references to features in Tk 3.6. This third edition has been updated to reflect new features added through Tcl/Tk 8.2:

Tcl 7.5 and Tk 4.1 had their final release in May 1996. These releases feature the port of Tk to the Windows and Macintosh environments. The Safe-Tcl security mechanism was introduced to support safe execution of network applets. There is also network socket support and a new Input/Output (I/O) subsystem to support high-performance event-driven I/O.

Tcl 7.6 and Tk 4.2 had their final release in October 1996. These releases include improvements in Safe-Tcl, and improvements to the grid geometry manager introduced in Tk 4.1. Cross-platform support includes virtual events (e.g., <<Copy>> as opposed to <Control-c>), standard dialogs, and more file manipulation commands.

Tcl 7.7 and Tk 4.3 were internal releases used for the development of the Tcl/Tk plug-in for the Netscape Navigator and Microsoft Internet Explorer Web browsers. Their development actually proceeded in parallel to Tcl 7.6 and Tk 4.2. The plug-in has been released for a wide variety of platforms, including Solaris/SPARC, Solaris/INTEL, SunOS, Linux, Digital UNIX, IRIX, HP/UX, Windows 95, Windows NT, and the Macintosh. The browser plug-in supports Tcl applets in Web pages and uses the sophisticated security mechanism of Safe-Tcl to provide safety.

Tcl 8.0 features an on-the-fly compiler for Tcl that provides many-times faster Tcl scripts. Tcl 8.0 supports strings with embedded null characters. The compiler is transparent to Tcl scripts, but extension writers need to learn some new C APIs to take advantage of its potential. The release history of 8.0 spread out over a couple of years as John Ousterhout moved from Sun Microsystems to Scriptics Corporation. The widely used 8.0p2 release was made in the fall of 1997, but the final patch release, 8.0.5, was made in the spring of 1999.

Tk changed its version to match Tcl at 8.0. Tk 8.0 includes a new platform-independent font mechanism, native menus and menu bars, and more native widgets for better native look and feel on Windows and Macintosh.

Tcl/Tk 8.1 features full Unicode support, a new regular expression engine that provides all the features found in Perl 5, and thread safety so that you can embed Tcl into multithreaded applications. Tk does a heroic job of finding the correct font to display your Unicode characters, and it adds a message catalog facility so that you can write internationalized applications. The release history of Tcl/Tk 8.1 also straddled the Sun to Scriptics transition. The first alpha release was made in the fall of 1997, and the final patch release, 8.1.1, was made in May 1999.

Tcl/Tk 8.2 is primarily a bug fix and stabilization release. There are a few minor additions to the Tcl C library APIs to support more extensions without requiring core patches. Tcl/Tk 8.2 went rapidly into final release in the summer of 1999.

Who Should Read This Book

This book is meant to be useful to the beginner in Tcl as well as the expert. For the beginner and expert alike, I recommend careful study of Chapter 1, Tcl Fundamentals. The programming model of Tcl is designed to be simple, but it is different from many programming languages. The model is based on string substitutions, and it is important that you understand it properly to avoid trouble in complex cases. The remainder of the book consists of examples that demonstrate how to use Tcl and Tk productively. For your reference, each chapter has tables that summarize the Tcl commands and Tk widgets they describe.

This book assumes that you have some programming experience, although you should be able to get by even if you are a complete novice. Knowledge of UNIX shell programming will help, but it is not required. Where aspects of window systems are relevant, I provide some background information. Chapter 2 describes the details of using Tcl and Tk on UNIX, Windows, and Macintosh.

How to Read This Book

This book is best used in a hands-on manner, trying the examples at the computer. The book tries to fill the gap between the terse Tcl and Tk manual pages, which are complete but lack context and examples, and existing Tcl programs that may or may not be documented or well written.

I recommend the on-line manual pages for the Tcl and Tk commands. They provide a detailed reference guide to each command. This book summarizes much of the information from the manual pages, but it does not provide the complete details, which can vary from release to release. HTML versions of the on-line manual pages can be found on the CD-ROM that comes with this book.

Other Tcl Books

This book was the second Tcl book after the original book by John Ousterhout, the creator of Tcl. Since then, the number of Tcl books has increased remarkably. The following are just some of the books currently available.

Tcl and the Tk Toolkit (Addison-Wesley, 1994) by John Ousterhout provides a broad overview of all aspects of Tcl and Tk, even though it covers only Tcl 7.3 and Tk 3.6. The book provides a more detailed treatment of C programming for Tcl extensions.

Exploring Expect (O'Reilly & Associates, Inc., 1995) by Don Libes is a great book about an extremely useful Tcl extension. Expect lets you automate the use of interactive programs like ftp and telnet that expect to interact with a user. By combining expect and Tk, you can create graphical user interfaces for old applications that you cannot modify directly.

Graphical Applications with Tcl & Tk (M&T Press, 1996) by Eric Johnson is oriented toward Windows users. The second edition is up-to-date with Tcl/Tk 8.0.

Tcl/Tk Tools (O'Reilly & Associates, Inc., 1997) by Mark Harrison describes many useful Tcl extensions. These include Oracle and Sybase interfaces, object-oriented language enhancements, additional Tk widgets, and much more. The chapters were contributed by the authors of the extensions, so they provide authoritative information on some excellent additions to the Tcl toolbox.

CGI Developers Resource, Web Programming with Tcl and Perl (Prentice Hall, 1997) by John Ivler presents Tcl-based solutions to programming Web sites.

Effective Tcl/Tk Programming (Addison Wesley, 1997) by Michael McLennan and Mark Harrison illustrate Tcl and Tk with examples and application design guidelines.

Interactive Web Applications with Tcl/Tk (AP Professional, 1998) by Michael Doyle and Hattie Schroeder describes Tcl programming in the context of the Web browser plugin.

Tcl/Tk for Programmers (IEEE Computer Society, 1998) by Adrian Zimmer describes Unix and Windows programming with Tcl/Tk. This book also includes solved exercises at the end of each chapter.

Tcl/Tk for Real Programmers (Academic Press, 1999) by Clif Flynt is another example-oriented book.

Tcl/Tk in a Nutshell (O'Reilly, 1999) by Paul Raines and Jeff Tranter is a handy reference guide. It covers several popular extensions including Expect, incr Tcl, Tix, TclX, BLT, SybTcl, OraTcl, and TclODBC. There is a tiny pocket-reference guide for Tcl/Tk that may eliminate the need to thumb through my large book to find the syntax of a particular Tcl or Tk command.

Web Tcl Complete (McGraw Hill, 1999) by Steve Ball describes programming with the Tcl Web Server. It also covers Tcl/Java integration using TclBlend. incr Tcl From The Ground Up (Osborn-McGraw Hill, 1999) by Chad Smith describes the incr Tcl object-oriented extension to Tcl.

On-line Examples

The book comes with a CD-ROM that has source code for all of the examples, plus a selection of Tcl freeware found on the Internet. The CD-ROM is created with the Linux mkhybrid program, so it is readable on UNIX, Windows, and Macintosh. There, you will find the versions of Tcl and Tk that were available as the book went to press. You can also retrieve the sources shown in the book from my personal Web site:

beedub/book/

Ftp Archives

The primary site for the Tcl and Tk distributions is given below as a Universal Resource Location (URL):

ftp://ftp.scriptics/pub/tcl

You can use FTP and log in to the host (e.g., ftp.scriptics) under the anonymous user name. Give your e-mail address as the password. The directory is in the URL after the host name (e.g., /pub/tcl). There are many sites that mirror this distribution. The mirror sites provide an archive site for contributed Tcl commands, Tk widgets, and applications. There is also a set of Frequently Asked Questions files. These are some of the sites that maintain Tcl archives: ftp://ftp.neosoft/pub/tcl ftp://ftp.syd.dit.csiro.au/pub/tk ftp://ftp.ibp.fr/pub/tcl ftp://src.doc.ic.ac.uk/packages/tcl/ ftp://ftp.luth.se/pub/unix/tcl/ ftp://sunsite.cnlab-switch.ch/mirror/tcl ftp://ftp.sterling/programming/languages/tcl ftp://ftp.sunet.se/pub/lang/tcl ftp://ftp.cs.columbia/archives/tcl ftp://ftp.uni-paderborn.de/pub/unix/tcl ftp://sunsite.unc/pub/languages/tcl ftp://ftp.funet.fi/pub/languages/tcl

You can use a World Wide Web browser like Mosaic, Netscape, Internet Explorer, or Lynx to access these sites. Enter the URL as specified above, and you are presented with a directory listing of that location. From there you can change directories and fetch files.

If you do not have direct FTP access, you can use an e-mail server for FTP. Send e-mail to ftpmail@decwrl.dec with the message Help to get directions. If you are on BITNET, send e-mail to bitftp@pucc.princeton.

You can search for FTP sites that have Tcl by using the Archie service that indexes the contents of anonymous FTP servers. Information about using Archie can be obtained by sending mail to archie@archie.sura that contains the message Help.

World Wide Web

Start with these World Wide Web pages about Tcl: scriptics/ sco/Technology/tcl/Tcl.html purl/NET/Tcl-FAQ/

The home page for this book contains errata for all editions. This is the only URL I control personally, and I plan to keep it up-to-date indefinitely:

beedub/book/

The Prentice Hall Web site has information about the book, but you must use its search facility to find the exact location. Start at:

phptr/

Newsgroups

The comp.lang.tcl newsgroup is very active. It provides a forum for questions and answers about Tcl. Announcements about Tcl extensions and applications are posted to the comp.lang.tcl.announce newsgroup.

Book Organization

The chapters of the book are divided into seven parts. The first part describes basic Tcl features. The first chapter describes the fundamental mechanisms that characterize the Tcl language. This is an important chapter that provides the basic grounding you will need to use Tcl effectively. Even if you have programmed in Tcl already, you should review Chapter 1. Chapter 2 goes over the details of using Tcl and Tk on UNIX, Windows, and Macintosh. Chapter 3 presents a sample application, a CGI script, that illustrates typical Tcl programming. The rest of Part I covers the basic Tcl commands in more detail, including string handling, data types, control flow, procedures, and scoping issues. Part I finishes with a description of the facilities for file I/O and running other programs.

Part II describes advanced Tcl programming. It starts with eval, which lets you generate Tcl programs on the fly. Regular expressions provide powerful string processing. If your data-processing application runs slowly, you can probably boost its performance significantly with the regular expression facilities. Namespaces partition the global scope of procedures and variables. Unicode and message catalogs support internationalized applications. Libraries and packages provide a way to organize your code for sharing among projects. The introspection facilities of Tcl tell you about the internal state of Tcl. Event driven I/O helps server applications manage several clients simultaneously. Network sockets are used to implement the HTTP protocol used to fetch pages on the World Wide Web. Safe-Tcl is used to provide a secure environment to execute applets downloaded over the network. TclHttpd is an extensible web server built in Tcl. You can build applications on top of this server, or embed it into your existing applications to give them a web interface.

Part III introduces Tk. It gives an overview of the toolkit facilities. A few complete examples are examined in detail to illustrate the features of Tk. Event bindings associate Tcl commands with events like keystrokes and button clicks. Part III ends with three chapters on the Tk geometry managers that provide powerful facilities for organizing your user interface.

Part IV describes the Tk widgets. These include buttons, menus, scrollbars, labels, text entries, multiline and multifont text areas, drawing canvases, list-boxes, and scales. The Tk widgets are highly configurable and very programmable, but their default behaviors make them easy to use as well. The resource database that can configure widgets provides an easy way to control the overall look of your application.

Part V describes the rest of the Tk facilities. These include selections, keyboard focus, and standard dialogs. Fonts, colors, images, and other attributes that are common to the Tk widgets are described in detail. This part ends with a few larger Tk examples.

Part VI is an introduction to C programming and Tcl. The goal of this part is to get you started in the right direction when you need to extend Tcl with new commands written in C or integrate Tcl into custom applications.

Part VII provides a chapter for each of the Tcl/Tk releases covered by the book. These chapters provide details about what features were changed and added. They also provide a quick reference if you need to update a program or start to use a new version.

What's New in the Third Edition

The third edition is up-to-date with Tcl/Tk 8.2. The main new Tcl/Tk features are Internationalization, which is covered in Chapter 15, a new regular expression engine, which is described in Chapter 11, and thread-safety. There is a new chapter about compiling C extensions, and there is a more complete C extension example. The chapters on Eval and the Web browser plugin received a thorough update. I made a light sweep through the remainder of the book correcting errors and improving examples. Perhaps the best addition for the reader is an all-new index.

My favorite addition to the book is Chapter 18 that describes TclHttpd, a Web server built in Tcl. TclHttpd provides a number of nice ways to integrate a Web server with a Tcl application, replacing the standard CGI interface with something that is much more flexible and efficient. I have been using this server for the last year to build scriptics. This freely available server has been used to build several other products, plus it provides an easy way for you to bring up your own Web server.

Contact the Author

I am always open to comments about this book. My e-mail address is welch@acm. It helps me sort through my mail if you put the word "book" or the title of the book into the e-mail subject line. Visit my Web site at:

beedub/

for current news about the book and my other interests.

From the Back Cover

Practical Programming in Tcl/Tk, 4th edition

  • The Tcl/Tk bestseller fully updated for Tcl 8.4!
  • Authoritative coverage of every Tcl and Tk command in the core toolkits
  • State-of-the-art Tk GUI coverage for Tcl, Perl, Python, and Ruby developers
  • Covers all key Tcl 8.4 enhancements: VFS, internationalization and performance improvements, new widgets, and much more
  • Covers multi-threaded Tcl applications and Starkits, a revolutionary way to package and deploy Tcl applications
  • CD-ROM: Source and binary Tcl/Tk distributions and extensions for Windows®, Linuxâ„¢, Solarisâ„¢, and Macintosh®, plus all code from the book

The Tcl/Tk bestseller, now completely updated for Tcl 8.4!

The world's #1 guide to Tcl/Tk has been thoroughly updated to reflect Tcl/Tk8.4's powerful improvements in functionality, flexibility, and performance!Brent Welch, Ken Jones, and Jeffrey Hobbs, three of the world1s leading Tcl/Tk experts, cover every facet of Tcl/Tk programming, including cross-platform scripting and GUI development, networking, enterprise application integration, and much more.Coverage includes:
  • Systematic explanations and sample code for all Tcl/Tk 8.4 core commands
  • Complete Tk GUI development guidance--perfect for developers working with Perl, Python, or Ruby
  • Insider's insights into Tcl 8.4's key enhancements: VFS layer, internationalized font/character set support, new widgets, and more
  • Definitive coverage of TclHttpd web server--written by its creator
  • New ways to leverage Tcl/Tk 8.4's major performance improvements
  • Advanced coverage: threading, Safe Tcl, Tcl script library, regular expressions, and namespaces

Whether you1re upgrading to Tcl/Tk 8.4, or building GUIs for applicationscreated with other languages, or just searching for a better cross-platformscripting solution, Practical Programming in Tcl and Tk, Fourth Editiondelivers all you need to get results!

The accompanying CD-ROM includes source and binary distributionsof Tcl/Tk, Tcl extensions, and tools for Windows 9x/2000/XP, Linux, Solaris,and Macintosh, plus all the code from the book.



About the Author

BRENT B. WELCH is a software architect at Panasas, and former researchengineer at Sun Microsystems and Xerox® PARC. He served as senior webengineer at Scriptics, and has been involved in Tcl and Tk from theirearliest days, developing major Tcl applications such as the exmh email userinterface and TclHttpd web server.

Ken Jones has provided training and documentation for software developersfor almost 20 years. He has specialized in the Tcl/Tk language since 1998.

Jeffrey Hobbs is a member of the Tcl Core Team and the release manager forTcl/Tk core. As Senior Developer at ActiveState, he is technical lead forTcl technologies. He served as Tcl Ambassador for Scriptics, and maintainsthe Tk Usage FAQ.



Excerpt. © Reprinted by permission. All rights reserved.

Preface

Tcl stands for Tool Command Language. Tcl is really two things: a scripting language, and an interpreter for thatlanguage that is designed to be easy to embed into your application. Tcl and itsassociated graphical user-interface toolkit, Tk, were designed and crafted by ProfessorJohn Ousterhout of the University of California, Berkeley. You can findthese packages on the Internet and use them freely in your application, even if itis commercial. The Tcl interpreter has been ported from UNIX to DOS, PalmOS,VMS, Windows, OS/2, NT, and Macintosh environments. The Tk toolkit has beenported from the X window system to Windows and Macintosh.

I first heard about Tcl in 1988 while I was Ousterhout's Ph.D. student atBerkeley. We were designing a network operating system, Sprite. While the studentshacked on a new kernel, John wrote a new editor and terminal emulator.He used Tcl as the command language for both tools so that users could definemenus and otherwise customize those programs. This was in the days of X10,and he had plans for an X toolkit based on Tcl that would help programs cooperatewith each other by communicating with Tcl commands. To me, this cooperationamong tools was the essence of Tcl.

This early vision imagined that applications would be large bodies of compiledcode and a small amount of Tcl used for configuration and high-level commands.John's editor, mx, and the terminal emulator, tx, followed this model.While this model remains valid, it has also turned out to be possible to writeentire applications in Tcl. This is because the Tcl/Tk shell, wish, provides accessto other programs, the file system, network sockets, plus the ability to create agraphical user interface. For better or worse, it is now common to find applicationsthat contain thousands of lines of Tcl script.

This book was written because, while I found it enjoyable and productive touse Tcl and Tk, there were times when I was frustrated. In addition, working atXerox PARC, with many experts in languages and systems, I was compelled tounderstand both the strengths and weaknesses of Tcl and Tk. Although many ofmy colleagues adopted Tcl and Tk for their projects, they were also just as quickto point out its flaws. In response, I have built up a set of programming techniquesthat exploit the power of Tcl and Tk while avoiding troublesome areas.This book is meant as a practical guide to help you get the most out of Tcl and Tkand avoid some of the frustrations I experienced.

It has been about 14 years since I was introduced to Tcl, and about eightyears since the first edition of this book. During several of those years I workedunder John Ousterhout, first at Sun Microsystems and then at Scriptics Corporation.There I remained mostly a Tcl programmer while others in our grouphave delved into the C implementation of Tcl itself. I've built applications likeHTML editors, email user interfaces, Web servers, and the customer database weran our business on. This experience is reflected in this book. The bulk of thebook is about Tcl scripting, and the aspects of C programming to create Tclextensions is given a lighter treatment. I have been lucky to remain involved inthe core Tcl development, and I hope I can pass along the insights I have gainedby working with Tcl.

Why Tcl?

As a scripting language, Tcl is similar to other UNIX shell languages suchas the Bourne Shell (sh), the C Shell (csh), the Korn Shell (ksh), and Perl. Shellprograms let you execute other programs. They provide enough programmability(variables, control flow, and procedures) to let you build complex scripts thatassemble existing programs into a new tool tailored for your needs. Shells arewonderful for automating routine chores.

It is the ability to easily add a Tcl interpreter to your application that sets itapart from other shells. Tcl fills the role of an extension language that is used toconfigure and customize applications. There is no need to invent a configurationfile format or a command language for your new application, or struggle to providesome sort of user-programmability for your tool. Instead, by adding a Tclinterpreter, you structure your application as a set of primitive operations thatcan be composed by a script to best suit the needs of your users. It also allowsother programs to have programmatic control over your application, leading tosuites of applications that work well together.

The Tcl C library has clean interfaces and is simple to use. The libraryimplements the basic interpreter and a set of core scripting commands thatimplement variables, flow control, and procedures (see page 22). There is also abroad set of APIs that access operating system services to run other programs,access the file system, and use network sockets. Tk adds commands to creategraphical user interfaces. The Tcl and Tk C APIs provide a "virtual machine"that is portable across UNIX, Windows, and Macintosh environments.

The Tcl virtual machine is extensible because your application can define new Tcl commands. These commands are associated with a C or C++ procedurethat your application provides. The result is applications that are split into a setof primitives written in a compiled language and exported as Tcl commands. ATcl script is used to compose the primitives into the overall application. Thescript layer has access to shell-like capability to run other programs, has accessto the file system, and can call directly into the compiled part of the applicationthrough the Tcl commands you define. In addition, from the C programminglevel, you can call Tcl scripts, set and query Tcl variables, and even trace the executionof the Tcl interpreter.

There are many Tcl extensions freely available on the Internet. Most extensionsinclude a C library that provides some new functionality, and a Tcl interfaceto the library. Examples include database access, telephone control, MIDIcontroller access, and expect, which adds Tcl commands to control interactiveprograms.

The most notable extension is Tk, a toolkit for graphical user interfaces. Tkdefines Tcl commands that let you create and manipulate user interface widgets.The script-based approach to user interface programming has three benefits:

  • Development is fast because of the rapid turnaround; there is no waiting for long compilations.
  • The Tcl commands provide a higher-level interface than most standard C library user-interface toolkits. Simple user interfaces require just a handful of commands to define them. At the same time, it is possible to refine the user interface in order to get every detail just so. The fast turnaround aids the refinement process.
  • The user interface can be factored out from the rest of your application. The developer can concentrate on the implementation of the application core and then fairly painlessly work up a user interface. The core set of Tk widgets is often sufficient for all your user interface needs. However, it is also possible to write custom Tk widgets in C, and again there are many contributed Tk widgets available on the network.

There are other choices for extension languages that include Visual Basic,Scheme, Elisp, Perl, Python, Ruby and Javascript. Your choice between them ispartly a matter of taste. Tcl has simple constructs and looks somewhat like C. Itis easy to add new Tcl primitives by writing C procedures. Tcl is very easy tolearn, and I have heard many great stories of users completing impressiveprojects in a short amount of time (e.g., a few weeks), even though they neverused Tcl before.

Java has exploded onto the computer scene since this book was first published.Java is a great systems programming language that in the long run coulddisplace C and C++. This is fine for Tcl, which is designed to glue together buildingblocks written in any system programming language. Tcl was designed towork with C, but has been adapted to work with the Java Virtual Machine.Where I say "C or C++", you can now say "C, C++, or Java," but the details are abit different with Java. This book does not describe the Tcl/Java interface, but you can find TclBlend on the CD-ROM. TclBlend loads the Java Virtual Machineinto your Tcl application and lets you invoke Java methods. It also lets youimplement Tcl commands in Java instead of C or C++. Jacl is a Tcl interpreterwritten in Java. It has some limitations compared with the native C-based Tclinterpreter, but Jacl is great if you cannot use the native interpreter.

Javascript is a language from Netscape that is designed to script interactionswith Web pages. Javascript is important because of its use in HTML userinterfaces. However, Tcl provides a more general purpose scripting solution thatcan be used in a wide variety of applications. The Tcl/Tk Web browser plugin providesa way to run Tcl in your browser. It turns out to be more of a Java alternativethan a JavaScript alternative. The plugin lets you run Tcl applicationsinside your browser, while JavaScript gives you fine grain control over thebrowser and HTML display. The plugin is described in Chapter 20.

Tcl and Tk Versions

Tcl and Tk continue to evolve. See http://www.beedub.com/book/ forupdates and news about the latest Tcl releases. Tcl and Tk have had separateversion numbers for historical reasons, but they are released in pairs that worktogether. The original edition of this book was based on Tcl 7.4 and Tk 4.0, andthere were a few references to features in Tk 3.6. This fourth edition has beenupdated to reflect new features added through Tcl/Tk 8.4:

Related Tags

DIGG This story   Save To Google   Save To Windows Live   Save To Del.icio.us   diigo it   Save To blinklist
Save To Furl   Save To Yahoo! My Web 2.0   Save To Blogmarks   Save To Shadows   Save To stumbleupon   Save To Reddit