Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 40.  Send

Communicating Processes

Chapter 22 presented two examples: a browser for the examples in this book, and a simple shell in which to try out Tcl commands. In that chapter they are put into the same application. The two examples shown below hook these two applications together using the send command. Example 40-2 changes the Run and Reset procedures to send Tcl commands the shell defined in Example 22-4 on page 329. The StartEvalServer procedure starts up the shell, if necessary.

Example 40-2 Hooking the browser to an eval server.
# Replace the Run and Reset procedures from
# Example 22? on page 324 with these procedures

# Start up the evalsrv.tcl script.
proc StartEvalServer {} {
   global browse
   # Start the shell and pass it our name.
   exec evalsrv.tcl [tk appname] &
   # Wait for evalsrv.tcl to send us its name
   tkwait variable browse(evalInterp)
proc Run {} {
   global browse
   set apps [winfo interps]
   set ix [lsearch -glob $apps evalsrv.tcl*]
   if {$ix < 0} {
      # No evalsrv.tcl application running
   if {![info exists browse(evalInterp)]} {
      # Hook up to already running eval server
      set browse(evalInterp) [lindex $apps $ix]
   if [catch {send $browse(evalInterp) {info vars}}err] {
      # It probably died - restart it.
   # Send the command asynchronously. The two
   # list commands foil the concat done by send and
   # the uplevel in EvalServe
   send -async $browse(evalInterp) \
      [list EvalEcho [list source $browse(current)]]
# Reset the shell interpreter in the eval server
proc Reset {} {
   global browse
   send $browse(evalInterp) {EvalEcho reset}

The number of lists created before the send command may seem excessive, but they are all necessary. The send command concatenates its arguments, so instead of letting it do that, we pass it a single list. Similarly, EvalServe expects a single argument that is a valid command, so list is used to construct that.

The shell in Example 22-4 on page 329 has an EvalEcho procedure that we can use as the target of send. The only thing it needs is to complete the rendez-vous with the browser. When the tcl shell starts up, it sends the browser its application name. The browser passes its own name on the command line that starts the shell, so the shell knows how to talk to the browser.

Example 40-3 Making the shell into an eval server.
# Add this to Example 22? on page 329
if {$argc > 0} {
   # Check in with the browser
   send [lindex $argv 0] \
      [list set browse(evalInterp) [tk appname]]

      Previous section   Next section