:TITLE: More command construction - format, list ;# ;# RCSID: $Header: /cvsroot/tcl/tcltutorial/original/Tcl33.lsn,v 1.1 2004/11/04 16:01:14 davidw Exp $ ;# Copyright (c) 1995 Clif Flynt ;# 9300 Fleming Rd. ;# Dexter, MI 48130 ;# clif@cflynt.com ;# See file "NOTICE" for licensing terms. ;# :LESSON_TEXT_START_LEVEL 0: The main point to be aware of when constructing command strings for eval is how the arguments will be grouped when the command is evaluated.
For instance
eval puts OK
would print the string OK. However,
eval puts Not OK
will generate an error.
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 and 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 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.
For instance
eval puts OK
would print the string OK. However,
eval puts Not OK
will generate an error.
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.
For instance
eval puts OK
would print the string OK. However,
eval puts Not OK
will generate an error.
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
You can run into problems with this feature when you are using the contents of variables to construct the command string. For instance:
set x "OK"
eval puts $x
set x "NOT OK"
eval puts $x
Again the first time eval is called, it will print OK, and
the second time it will generate an error. In this case, it's because
the $x is substituted with NOT OK in the Tcl substitution phase.
Again, this code will work:
set x "NOT OK"
eval [list puts $x]
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 best 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 here 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.
As usual, run the example, and follow the code and output.
The first eval command shows a trivial use of eval that works.
The next eval command uses lappend to make a multiple word puts that will also work.
The next command demonstrates how a trivial use of eval can fail. Again, this fails because there is nothing in the command to group NOT OK into one argument. When eval invokes concat it will treat the two words as two arguments. Try a few methods of making this command correct.
The format command in the next example creates the string
puts "Even This Works". In this example, the second argument
"Even This Works" is grouped by the quotes into a single argument,
instead of being treated as three arguments.
The next example uses a variable to demonstrate the same process.
The next example uses lappend to create the tempFileName proc that was used in the previous lesson. Remember that a Tcl command is really a list, and this example becomes a bit more obvious.
The first line, set cmd {proc tempFileName } initializes the
command string to just contain proc tempFileName.
The next line, lappend cmd "" appends an empty list item to
cmd. The empty list item is {}, which is an empty argument
field. At this point cmd contains the string proc tempFileName {}.
The next line is the body of the proc. The things to note here are the escaped
quotes and dollar signs. If the body of the proc were enclosed in braces,
then the escapes wouldn't be needed. However, in that case
the [pid] would not be evaluated, and the [pid]
would be part of the proc, instead of having the pid number placed in the
string.
After this, the body of the proc is displayed for your viewing pleasure.
Next, the format command is used to create the proc definition command. The first line just initializes the command string, as before.
The subsequent lines append the body of the proc to the command. The
last line uses format to merge the results of the
[pid] into the command line.
:TEXT_END:
:CODE_START:
;# This works
set cmd "OK"
eval puts $cmd
;# This also works
set cmd "puts" ; lappend cmd {Also OK}; eval $cmd
;# This generates an error
;# Change it to eval [list puts $cmd] to continue
set cmd "NOT OK"
eval puts $cmd
eval [format {%s "%s"} puts "Even This Works"]
set cmd "And even this can be made to work"
eval [format {%s "%s"} puts $cmd ]
set tmpFileNum 0;
;# Creating the tempFileName proc with lappend
set cmd {proc tempFileName }
lappend cmd ""
lappend cmd "global num; incr num; return \"/tmp/TMP.[pid].\$num\""
eval $cmd
puts "\nThis is the body of the proc definition:"
puts "[info body tempFileName]\n"
;# Now, show an incomplete command being caught by info complete
set cmd {puts "This is Cool!}
if {[info complete $cmd]} {
eval $cmd
} else {
puts "INCOMPLETE COMMAND: $cmd"
}
:TEXT_END: