eval(3tcl)
π eval(3tcl) β the Tcl Built-In Commands era π
okay bestie, welcome to the glow-up of eval, straight out of the Tcl Built-In Commands collection π she's a classic, she's iconic, and she's about to eat β¨
β¨ NAME β¨
eval β Evaluate a Tcl script π¦
πΈ SYNOPSIS πΈ
eval arg ?arg ...?
π DESCRIPTION π
okay so eval takes one or more arguments, and together those arguments make up a Tcl script that has one or more commands in it. no cap. π
then β and this is the part β eval concatenates all its arguments in the exact same fashion as the concat command does, hands that concatenated string over to the Tcl interpreter recursively, and returns the result of that evaluation. or, if the universe said no, it returns any error generated by it π
ngl this is important: thelistcommand quotes sequences of words in such a way that they are not further expanded by theevalcommand. so for any values,$a,$b, and$c, these two lines are effectively equivalent:
eval [list $a $b $c]
$a $b $c
like. they're giving the same energy fr fr π«
π EXAMPLES π
so a lot of the time it's genuinely useful to store a fragment of a script in a variable and then execute it later on with extra values appended. this technique is used in a bunch of places all throughout the Tcl core (e.g. in fcopy, lsort and trace command callbacks π). here's how you do it with core Tcl commands:
set script {
puts "logging now"
lappend $myCurrentLogVar
}
set myCurrentLogVar log1
# Set up a switch of logging variable part way through!
after 20000 set myCurrentLogVar log2
for {set i 0} {$i<10} {incr i} {
# Introduce a random delay
after [expr {int(5000 * rand())}]
update ;# Check for the asynch log switch
eval $script $i [clock clicks]
}
okay but here's the tea β β in the most common case (where the script fragment is actually just a list of words forming a command prefix), it is better to use {*}$script when doing this sort of invocation pattern. why? because it is less general than the eval command, and hence easier to make robust in practice. lowkey the safer bestie π«Ά
the following procedure acts in a way that is analogous to the lappend command, except it inserts the argument values at the start of the list in the variable:
proc lprepend {varName args} {
upvar 1 $varName var
# Ensure that the variable exists and contains a list
lappend var
# Now we insert all the arguments in one go
set var [eval [list linsert $var 0] $args]
}
however π β that last line would now normally be written without eval, like this:
set var [linsert $var 0 {*}$args]
or indeed like this:
set var [list {*}$args {*}$var]
see how much cleaner? she ate and left no crumbs π β¨
π₯ SEE ALSO π₯
catch(3tcl), concat(3tcl), error(3tcl), errorCode(3tcl), errorInfo(3tcl), interp(3tcl), list(3tcl), namespace(3tcl), subst(3tcl), uplevel(3tcl)
π KEYWORDS π
concatenate, evaluate, script
and that's the whole page bestie π this little love letter lives under the Tcl banner and answers to the name eval(3tcl) β go forth and concatenate responsibly, mwah πβ¨π¦
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms