The following example is a general-purpose script that reads input and then sends it to another application. You can put this at the end of a pipeline to get a loopback effect to the main application, although you can also use fileevent for similar effects. One advantage of send over fileevent is that the sender and receiver can be more independent. A logging application, for example, can come and go independently of the applications that log error messages:
The sender application supports communication with two processes. It sends all its input to a primary “logging” application. When the input finishes, it can send a notification message to another “controller” application. The logger and the controller could be the same application.
Use list to quote arguments to send. |
Consider the send command used in the example:
send $app [concat $cmd [list $input ]]
The combination of concat and list is tricky. The list command quotes the value of the input line. This quoted value is then appended to the command, so it appears as a single extra argument. Without the quoting by list, the value of the input line will affect the way the remote interpreter parses the command. Consider these alternatives:
send $app [list $cmd $input]
This form is safe, except that it limits $cmd to a single word. If cmd contains a value like the ones given below, the remote interpreter will not parse it correctly. It will treat the whole multiword value as the name of a command:
.log insert end .log see end ; .log insert end
This is the most common wrong answer:
send $app $cmd $input
The send command concatenates $cmd and $input together, and the result will be parsed again by the remote interpreter. The success or failure of the remote command depends on the value of the input data. If the input included Tcl syntax like $ or [ ], errors or other unexpected behavior would result.
3.145.101.192