More command construction - format, list

Table of Contents

More command construction - format, list
Example

More command construction - format, list

There may be some unexpected results when you try to compose command strings for eval.

For instance


eval puts OK


eval puts Not OK

The reason that the second command generates an error is that the eval uses concat to merge its arguments into a command string. This causes the two words Not OK to be treated as two arguments to puts. If there is more than one argument to puts, the first argument must be a file pointer.

Correct ways to write the second command include these:


eval [list puts {Not OK}]
eval [list puts "Not OK"]
set cmd "puts" ; lappend cmd {Not OK}; eval $cmd

As long as you keep track of how the arguments you present to eval will be grouped, you can use many methods of creating the strings for eval, including the string commands and format.

The recommended methods of constructing commands for eval is to use the list and lappend commands. These commands become difficult to use, however if you need to put braces in the command, as was done in the previous lesson.

The example from the previous lesson is re-implemented in the example code using lappend.

The completeness of a command can be checked with info complete. Info complete can also be used in an interactive program to determine if the line being typed in is a complete command, or the user just entered a newline to format the command better.

info complete string

If string