在 tcl 中如何从一个文件位置获取数据并使用 TCL 代码运行该数据。 例如 在文件夹 1 中有配置文件,我想获取配置文件的信息,我想执行存在或不存在的信息,
最佳答案
如果配置文件包含Tcl代码,它只是:
# Put the filename in quotes if you want, or in a variable, or ...
source /the/path/to/the/file.tcl
如果文件包含 Tcl 代码但您不信任它,您可以使用“安全解释器”上下文。这会禁用许多命令,提供一组更受限制的功能,然后您可以向这些功能添加特定的异常(exception)(使用 interp 别名
):
# Make the context
set i [interp create -safe]
# Set up a way for the context to let the master find out about what to
# really set
interp alias $i configure {} recordConfiguration
proc recordConfiguration args {
puts "configured with $args"
}
# Evaluate the script (note that [source] is hidden by default) in the context
$i invokehidden source /the/path/to/the/file.tcl
# Dispose the context
interp delete $i
如果文件不是 Tcl 代码,您必须解析它。这是一个复杂得多的问题,复杂到我们需要知道文件的格式才能回答。
关于testing - 如何从一个文件位置获取数据并使用 tcl 代码运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21844843/