1eval(n) Tcl Built-In Commands eval(n)
2
3
4
5______________________________________________________________________________
6
8 eval - Evaluate a Tcl script
9
11 eval arg ?arg ...?
12______________________________________________________________________________
13
15 Eval takes one or more arguments, which together comprise a Tcl script
16 containing one or more commands. Eval concatenates all its arguments
17 in the same fashion as the concat command, passes the concatenated
18 string to the Tcl interpreter recursively, and returns the result of
19 that evaluation (or any error generated by it). Note that the list
20 command quotes sequences of words in such a way that they are not fur‐
21 ther expanded by the eval command.
22
24 Often, it is useful to store a fragment of a script in a variable and
25 execute it later on with extra values appended. This technique is used
26 in a number of places throughout the Tcl core (e.g. in fcopy, lsort and
27 trace command callbacks). This example shows how to do this using core
28 Tcl commands:
29
30 set script {
31 puts "logging now"
32 lappend $myCurrentLogVar
33 }
34 set myCurrentLogVar log1
35 # Set up a switch of logging variable part way through!
36 after 20000 set myCurrentLogVar log2
37
38 for {set i 0} {$i<10} {incr i} {
39 # Introduce a random delay
40 after [expr {int(5000 * rand())}]
41 update ;# Check for the asynch log switch
42 eval $script $i [clock clicks]
43 }
44
45 Note that in the most common case (where the script fragment is actu‐
46 ally just a list of words forming a command prefix), it is better to
47 use {*}$script when doing this sort of invocation pattern. It is less
48 general than the eval command, and hence easier to make robust in prac‐
49 tice. The following procedure acts in a way that is analogous to the
50 lappend command, except it inserts the argument values at the start of
51 the list in the variable:
52
53 proc lprepend {varName args} {
54 upvar 1 $varName var
55 # Ensure that the variable exists and contains a list
56 lappend var
57 # Now we insert all the arguments in one go
58 set var [eval [list linsert $var 0] $args]
59 }
60
61 However, the last line would now normally be written without eval, like
62 this:
63
64 set var [linsert $var 0 {*}$args]
65
67 catch(n), concat(n), error(n), errorCode(n), errorInfo(n), interp(n),
68 list(n), namespace(n), subst(n), uplevel(n)
69
71 concatenate, evaluate, script
72
73
74
75Tcl eval(n)