开发者

How do I use the eval statement in tcl

开发者 https://www.devze.com 2023-02-26 03:24 出处:网络
So basically in my tcl script I generate a line of t开发者_开发百科cl code that I have to execute( ie I come up with this instruction during runtime). Once I have the tcl instruction say for example p

So basically in my tcl script I generate a line of t开发者_开发百科cl code that I have to execute( ie I come up with this instruction during runtime). Once I have the tcl instruction say for example puts "hello world $test_var". How do I execute this using tcl?

Do I use the eval command or what?


The eval command is a reasonable way of doing this, but you might want to consider using catch instead, since that will trap any problems found during the evaluation of the generated code, for example like this:

# Generate the code somehow
set variable {puts "hello word $test_var"}

# Execute and trap problems
if {[catch $variable resultMsg]} {
    # An error (or other exception) happened
    puts stderr "BOOM, Error! $resultMsg"
}


Instead of using [eval] which works perfectly well, in newer versions of Tcl you can use the {*} syntax. For example:

set cmd "puts"
lappend cmd "abcd ef"
{*}$cmd

Note that it's a good habit to use list commands to build up your command so you wont run into quoting issues.


I'm afraid you don't have another reasonable choice than to use the eval command, e.g.:

set variable {puts "hello world $test_var"}
eval $variable
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号