Wrapping external commands with Tcl

Many times people want to do things like run a program feeding it information, or doing some setup, then running a command and doing something with its output, or writing a Tk wrapper for a text only command.

Here's an example of wrapping the bc arbitrary precision arithmetic language calculator in Tcl. This might provide one with longer precision math without implementing one of the extensions discussed in Arbitrary Precision Math Procedures.

 #! /usr/tcl84/bin/tclsh
 # Program: bcfe
 # Date: Jan 27, 2003
 # Version: 1.1

 set fe [open "|/bin/bc -l" "r+"]
 fconfigure $fe -buffering none

 puts "Enter expression:"
 set ex 1
 puts -nonewline "bc $ex> "
 flush stdout
 while { [gets stdin input] >= 0 } {
        if { $input == "quit" } {
        puts $fe "$input"
        flush $fe
        incr ex
        gets $fe newans
        puts "Answer is: $newans"
        puts -nonewline "bc $ex> "
        flush stdout

Derk Gwen succinctly answers the oh-so-frequently-asked question about how to keep Tk "live" while managing a sub-process in a clt posting [L1 ].

See Concepts of Architectural Design for Tcl Applications for lots of ways to communicate between applications besides opening a pipe.

This [L2 ] is the third of a three-part series on wrapping external commands.

[ CL intends to link in lots of references to related topics: Tcl and other processes, Tcl and other languages, the idea of wrapping, ...]