eval and catch
eval
As described earlier, Tcl uses a one-pass parsing mechanism when evaluating scripts. It is sometimes useful, however, to have the interpreter make more than one pass over a script before evaluating it. Being able to force the interpreter to parse a script more than once allows one to store Tcl scripts in variables, and have them be evaluated at a later time. This is shown in Example 10.1:
Example 10.1:
set foo "set a 22" eval $foo puts $a
Output:
22
The variable "foo" is set to the value "set a 22", which is itself a Tcl script. Next, the value of the variable "foo"
is substituted into the eval command. The eval command simply
passes its arguments through the Tcl interpreter for another round of parsing. When the interpreter encounters the statement "eval $foo",
the first round of parsing simply substitutes the value of the variable "foo" in the place of "$foo", resulting in the expression
"eval set a 22". The eval command then sends its arguments, "set a 22", through the interpreter
again, resulting in the variable "a" being created and assigned the value "22".
One might be tempted to think that the use of the eval command could be avoided and simply replaced with the statement,
$foo
This does not work because, on encountering the statement "$foo", the interpreter simply replaces it with the value stored in the variable "foo", and then considers its parsing work done. So, "$foo" gets replaced by "set a 22", but the interpreter never parses "set a 22", which it needs to do to make sense of the components of the statement (it needs to realize that "set" corresponds to a built in command, and that it is being passed two arguments, "a" and "22") and evaluate it correctly .
catch
When an error occurs in a Tcl command, the entire script of which it is a part is halted, and an error message is displayed. However, instead of halting the whole Tcl script, it may be useful to simply display a friendly error message and continue execution of the Tcl script.
The catch command prevents Tcl's error handling mechanisms from executing (and thus halting execution)
and simply returns a meaningful value when an error occurs. This allows the program to define its own behaviour in the case of an error.
Example 10.2:
set retval [catch {set f [open "nosuchfile" "r"]}]
if {$retval == 1} {
puts "An error occurred"
}
Output: (this output occurs if there is no file named "nosuchfile" in the current directory).
An error occurred
The catch command is given a Tcl script as an argument. It evaluates this script, and if an error
occurs, it returns 1, otherwise it returns 0. In Example 10.2, the script passed to catch tries to open
a file named "nosuchfile". Assuming that no file with this name exists in the current directory, the open
command should return an error. Since it occurs within a catch statement, the normal Tcl error handling routines do not get invoked,
and the catch command simply returns 1. This return value is assigned to the variable "retval", which is checked to determine whether or not to print the error message.
The catch command can be used in many different ways, only one of which is shown here. Refer to the
manual page for a more complete description.
This brings to end the tutorial on the basics of TCL. With the knowledge that you have gained from this tutorial you now know enough to write simple TCL scripts and to execute them using the TCL interpreter that is built in to Surpac, Xplorpac and other products from the same software family.
This tutorial now continues with more advanced examples of TCL.