从可能具有相同过程和变量名称的目录中获取所有文件

时间:2014-03-11 12:19:24

标签: tcl

我是TCL编程的新手。 我在目录中有大量文件,我想要全部采购。但问题是,有许多文件包含相同的变量名称。那么如何解决这个冲突以及如何使用文件名分别访问变量。 我正在考虑使用命名空间,这是完成此任务的正确方法。

在python中我可以在导入后使用fileName.variable。有没有办法在TCL中做到这一点。

任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:3)

使用命名空间是一种很好的方法

foreach file [glob *.tcl] {
    set ns [file rootname $file]   ;# remove ".tcl" extension
    namespace eval $ns {source $file}
}

测试

$ cat a.tcl
set hello world
proc foo {} {return bar}

$ tclsh
% set file a.tcl
a.tcl
% set ns [file rootname $file]
a
% namespace eval $ns {source $file}
% puts $hello
can't read "hello": no such variable
% puts $a::hello
world
% foo
invalid command name "foo"
% a::foo
bar
相关问题