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
14
16 Eval takes one or more arguments, which together comprise a Tcl script
17 containing one or more commands. Eval concatenates all its arguments
18 in the same fashion as the concat command, passes the concatenated
19 string to the Tcl interpreter recursively, and returns the result of
20 that evaluation (or any error generated by it). Note that the list
21 command quotes sequences of words in such a way that they are not fur‐
22 ther expanded by the eval command.
23
25 Often, it is useful to store a fragment of a script in a variable and
26 execute it later on with extra values appended. This technique is used
27 in a number of places throughout the Tcl core (e.g. in fcopy, lsort and
28 trace command callbacks). This example shows how to do this using core
29 Tcl commands:
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 proc lprepend {varName args} {
53 upvar 1 $varName var
54 # Ensure that the variable exists and contains a list
55 lappend var
56 # Now we insert all the arguments in one go
57 set var [eval [list linsert $var 0] $args]
58 }
59 However, the last line would now normally be written without eval, like │
60 this: │
61 set var [linsert $var 0 {*}$args] │
62
63
65 catch(n), concat(n), error(n), interp(n), list(n), namespace(n),
66 subst(n), tclvars(n), uplevel(n)
67
68
70 concatenate, evaluate, script
71
72
73
74Tcl eval(n)