Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

TCL - Creating Commands - eval


One difference between Tcl and most other compilers is that Tcl will allow an executing program to create new commands and execute them while running.
A tcl command is defined as a list of strings in which the first String is a command or proc. Any string or list which meets this criteria can be evaluated and executed.
The eval command will evaluate a list of strings as though they were commands typed at the % prompt or sourced from a file. The eval command normally returns the final value of the commands being evaluated. If the commands being evaluated throw an error (for example, if there is a syntax error in one of the strings), then eval will will throw an error.
Note that either concat or list may be used to create the command string, but that these two commands will create slightly different command strings.
eval arg1 ??arg2?? ... ??argn??
Evaluates arg1 - argn as one or more Tcl commands. The args are concatenated into a string, and passed totcl_Eval to evaluate and execute.

Eval returns the result (or error code) of that evaluation.

Example


set cmd {puts "Evaluating a puts"}

puts "CMD IS: $cmd"
eval $cmd


ewProcA] ""] } {
puts "\nDefining newProcA

if {[string match [info procs
nfor this invocation"
set num 0;
set cmd "proc newProcA "

l num;\n"]
set cmd [concat $cm

set cmd [concat $cmd "{} {\n"]
set cmd [concat $cmd "glob
ad "incr num;\n"]
set cmd [concat $cmd " return \"/tmp/TMP.[pid].\$num\";\n"]

n[info body newProcA]\n"

put

set cmd [concat $cmd "}"]
eval $cmd
}

puts "\nThe body of newProcA is:
\s "newProcA returns: [newProcA]"
puts "newProcA returns: [newProcA]"

#
# Define a proc using lists
#


et cmd "proc newProcB "
lappend cmd {}

if {[string match [info procs newProcB] ""] } {
puts "\nDefining newProcB for this invocation"

slappend cmd {global num; incr num; return $num;}

eval $cmd
}

puts "\nThe body of newProcB is: \n[info body newProcB]\n"


puts "newProcB returns: [newProcB]"


This post first appeared on VLSI WORLD, please read the originial post: here

Share the post

TCL - Creating Commands - eval

×

Subscribe to Vlsi World

Get updates delivered right to your inbox!

Thank you for your subscription

×