| 1 | '\" | 
|---|
| 2 | '\" Copyright (c) 1993 The Regents of the University of California. | 
|---|
| 3 | '\" Copyright (c) 1994-1996 Sun Microsystems, Inc. | 
|---|
| 4 | '\" | 
|---|
| 5 | '\" See the file "license.terms" for information on usage and redistribution | 
|---|
| 6 | '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. | 
|---|
| 7 | '\"  | 
|---|
| 8 | '\" RCS: @(#) $Id: eval.n,v 1.10 2007/12/13 15:22:32 dgp Exp $ | 
|---|
| 9 | '\"  | 
|---|
| 10 | .so man.macros | 
|---|
| 11 | .TH eval n "" Tcl "Tcl Built-In Commands" | 
|---|
| 12 | .BS | 
|---|
| 13 | '\" Note:  do not modify the .SH NAME line immediately below! | 
|---|
| 14 | .SH NAME | 
|---|
| 15 | eval \- Evaluate a Tcl script | 
|---|
| 16 | .SH SYNOPSIS | 
|---|
| 17 | \fBeval \fIarg \fR?\fIarg ...\fR? | 
|---|
| 18 | .BE | 
|---|
| 19 |  | 
|---|
| 20 | .SH DESCRIPTION | 
|---|
| 21 | .PP | 
|---|
| 22 | \fBEval\fR takes one or more arguments, which together comprise a Tcl | 
|---|
| 23 | script containing one or more commands. | 
|---|
| 24 | \fBEval\fR concatenates all its arguments in the same | 
|---|
| 25 | fashion as the \fBconcat\fR command, passes the concatenated string to the | 
|---|
| 26 | Tcl interpreter recursively, and returns the result of that | 
|---|
| 27 | evaluation (or any error generated by it). | 
|---|
| 28 | Note that the \fBlist\fR command quotes sequences of words in such a | 
|---|
| 29 | way that they are not further expanded by the \fBeval\fR command. | 
|---|
| 30 | .SH EXAMPLES | 
|---|
| 31 | Often, it is useful to store a fragment of a script in a variable and | 
|---|
| 32 | execute it later on with extra values appended. This technique is used | 
|---|
| 33 | in a number of places throughout the Tcl core (e.g. in \fBfcopy\fR, | 
|---|
| 34 | \fBlsort\fR and \fBtrace\fR command callbacks). This example shows how | 
|---|
| 35 | to do this using core Tcl commands: | 
|---|
| 36 | .CS | 
|---|
| 37 | set script { | 
|---|
| 38 |     puts "logging now" | 
|---|
| 39 |     lappend $myCurrentLogVar | 
|---|
| 40 | } | 
|---|
| 41 | set myCurrentLogVar log1 | 
|---|
| 42 | # Set up a switch of logging variable part way through! | 
|---|
| 43 | after 20000 set myCurrentLogVar log2 | 
|---|
| 44 |  | 
|---|
| 45 | for {set i 0} {$i<10} {incr i} { | 
|---|
| 46 |     # Introduce a random delay | 
|---|
| 47 |     after [expr {int(5000 * rand())}] | 
|---|
| 48 |     update    ;# Check for the asynch log switch | 
|---|
| 49 |     \fBeval\fR $script $i [clock clicks] | 
|---|
| 50 | } | 
|---|
| 51 | .CE | 
|---|
| 52 | .PP | 
|---|
| 53 | .VS 8.5 | 
|---|
| 54 | Note that in the most common case (where the script fragment is | 
|---|
| 55 | actually just a list of words forming a command prefix), it is better | 
|---|
| 56 | to use \fB{*}$script\fR when doing this sort of invocation | 
|---|
| 57 | pattern.  It is less general than the \fBeval\fR command, and hence | 
|---|
| 58 | easier to make robust in practice. | 
|---|
| 59 | .VE 8.5 | 
|---|
| 60 | The following procedure acts in a way that is analogous to the | 
|---|
| 61 | \fBlappend\fR command, except it inserts the argument values at the | 
|---|
| 62 | start of the list in the variable: | 
|---|
| 63 | .CS | 
|---|
| 64 | proc lprepend {varName args} { | 
|---|
| 65 |    upvar 1 $varName var | 
|---|
| 66 |    # Ensure that the variable exists and contains a list | 
|---|
| 67 |    lappend var | 
|---|
| 68 |    # Now we insert all the arguments in one go | 
|---|
| 69 |    set var [\fBeval\fR [list linsert $var 0] $args] | 
|---|
| 70 | } | 
|---|
| 71 | .CE | 
|---|
| 72 | .VS 8.5 | 
|---|
| 73 | However, the last line would now normally be written without | 
|---|
| 74 | \fBeval\fR, like this: | 
|---|
| 75 | .CS | 
|---|
| 76 | set var [linsert $var 0 {*}$args] | 
|---|
| 77 | .CE | 
|---|
| 78 | .VE 8.5 | 
|---|
| 79 |  | 
|---|
| 80 | .SH "SEE ALSO" | 
|---|
| 81 | catch(n), concat(n), error(n), interp(n), list(n), namespace(n), subst(n), tclvars(n), uplevel(n) | 
|---|
| 82 |  | 
|---|
| 83 | .SH KEYWORDS | 
|---|
| 84 | concatenate, evaluate, script | 
|---|