Version 4 of eval

Updated 2002-01-29 19:50:20

http://purl.org/tcl/home/man/tcl8.4/TclCmd/eval.htm

NAME

eval - Evaluate a Tcl script

Synopsis

SYNOPSIS

 eval arg ?arg ...?  

DESCRIPTION

Eval takes one or more arguments, which together comprise a Tcl script containing one or more commands. Eval concatenates all its arguments in the same fashion as the concat command, passes the concatenated string to the Tcl interpreter recursively, and returns the result of that evaluation (or any error generated by it). (From: TclHelp)


Use of the eval command is considered "evil", dangerous or bad style by some Tcl'ers (because it leads to double substitution), but there are situations where its feature of removing one layer of list structure (like with concat) comes in just right. If you compose widgets in Tk, first you create them, then you manage them (register at a geometry manager, e.g. pack). You either have to keep track of what widgets you created, and repeat that list in the pack command, or just say

 eval pack [winfo children .]

eval is often used with exec, to flatten input lists:

 eval exec grep foo $filelist

because otherwise grep would receive filelist as one long filename with embedded blanks. Or, if you want to append one list's elements to another:

 set thislist [concat $thislist $thatlist] ;# can also be done as
 eval lappend thislist $thatlist

Another application is in building up a command in pieces (by appending to a string) and finally calling

 eval $cmd

eval concatenates its arguments in the same fashion as


Tcl syntax help - Arts and crafts of Tcl-Tk programming - Category Command