Review: Exploring Expect

Expect and Tcl

Expect is an extension of the Tcl scripting language, but you could be easily forgiven for thinking it's a complete stand-alone language given it's often invoked from its own "expect" binary.

Expect is the go-to language for automation. Spawn a process (such as Ssh, Telnet, or any text-mode app from the shell), then send commands, expect responses, branch according to content, and make use of the strengths of the Tcl programming language, and its other many extensions. In the last year of last century, I discovered Expect for myself and was using it to automate connections and config updates to Cisco routers. In recent times, I've used Expect as part of an "auto-discovery tool" to build configs for Puppet, a DevOps management system. Expect is a joy to work with, and you'll do amazing things.

You might be able to replicate what Expect does using another language... but I wouldn't like to see your source code!

Exploring Expect

"Exploring Expect" by Don Libes is an O'Reilly Media tome. My own copy is a bit old, print date 1996, and punches in at 568 pages. Localhost says the one you buy today is 606 pages. They're the same edition (1st), which leads me to think the difference may be minor corrections and formatting changes.

As an O'Reilly Media "animal book" it's formatted with standard layout and font set. It's easy to underestimate how much easier this makes reading and scanning. O'Reilly hit on a good formula: just the right amount of white-space at the margins, and the content is not padded out with screenshots or other graphics.

The first 200 or so pages are used to introduce the reader to general features of the language: strings, lists, flow control, arrays, file IO, regular expressions, etc. There's enough Tcl coverage here for you to write excellent scripts, without needing to buy a separate Tcl textbook.

Expect itself is well covered, from getting started, right through to handling problem "corner cases" and some interesting use cases you might not have thought of. It's all meat, no filler. Examples for each concept and command are concise and stripped of anything that might distract from what they're intended to demonstrate. This is what you're buying the book for, and you'll get excellent value for your time and money.

Later on, you get 33 pages on Tk, the GUI toolkit for Expect and Tcl. The info there is densely packed, which is good, but there's an awful lot of Tk missing. The example script used is perhaps a little complicated for its purpose — there's a risk that beginners and skimmers may confuse proc functions with Expect functions. There's only one screenshot (zoomed in a little bit too much), and it could probably do with a few more. Essentially, there's enough about Tk to whet your appetite, but not satiate it. (If you're really interested in providing a GUI for your automation tool, I'd recommend picking up this book but also: Practical Programming in Tcl and Tk by Brent Welch and Ken Jones; or Tcl/Tk For Real Programmers by Cliff Flynt.)

Also included is some excellent content for C and C++ developers, covering the Expect library. It's assumptions are a little Unix-centric, but most of it is still relevant if you're developing for other platforms like Microsoft Windows.

In Sum

Publisher: O'Reilly Media; 1st edition (December 11, 1994)
Series: Nutshell Handbooks
Paperback: 606 pages
Language: English
ISBN-10: 1565920902
ISBN-13: 978-1565920903
Product Dimensions: 7 x 1.3 x 9.2 inches

Rating: 9/10. Audience: Intermediate to expert programmers, and systems administrators.

For me, this one is a classic. Its readability helped me get scripts written in 1999, and contributed to my continuing interest in Tcl lasting to the present day. If you need to automate text sessions, this is your book. I strongly recommend it.

Peter Caffin is a systems administrator and software developer from Perth, Western Australia. His contributions to Tcl/Tk include TclMacBag (various Mac UI controls to all platforms in a platform appropriate way) and TclTalkback (automated error reporting to developers). Product links point to Localhost, where Peter Caffin is an affiliate.

(Reviewed: August 2015.)