如何通过传递参数来获取脚本文件?

发布于 2024-11-06 07:58:20 字数 187 浏览 2 评论 0原文

假设我有一个 tcl 脚本,我想将一些参数传递给第一个 tcl 中来源的第二个脚本文件:

#first tcl file

source second.tcl

我想控制来自 first.tcl 的 secondary.tcl 的流程,并且我读到 tcl 源不接受论据。我想知道那时我该怎么办。

Say I have a tcl script and I want to pass some arguments to the second script file which is being sourced in the first tcl:

#first tcl file

source second.tcl

I want to control the flow of second.tcl from first.tcl and I read that tcl source does not accept arguments. I wonder how I can do then.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

り繁华旳梦境 2024-11-13 07:58:20

source 不接受任何其他参数。但您可以使用(全局)变量来传递参数,例如:

# first tcl file
set ::some_variable some_value
source second.tcl

第二个 TCL 文件可以引用该变量,例如:

# second tcl file
puts $::some_variable

备注:
获取文件意味着源脚本的内容在当前上下文中执行。这意味着源脚本可以访问该上下文中存在的所有变量。上面的代码等同于:

# one joint tcl file
set ::some_variable some_value
puts $::some_variable

source does not accept any additional arguments. But you can use (global) variables to pass arguments, e.g.:

# first tcl file
set ::some_variable some_value
source second.tcl

The second TCL file can reference the variable, e.g.:

# second tcl file
puts $::some_variable

Remark:
Sourcing a file means that the content of the sourced script is executed in the current context. That means that the sourced script has access to all variables existing in that context. The above code is the same as:

# one joint tcl file
set ::some_variable some_value
puts $::some_variable
千柳 2024-11-13 07:58:20

关于“::”的事情 - 请参阅此处的解释(抱歉,我还没有足够的代表来发表评论)。

我还应该补充一点,最初的问题讨论了一个看起来很奇怪的问题:似乎最好在第二个源文件中提供一个特定的过程,该过程将设置与该脚本定义的状态相关的状态。
像这样的事情:

source file2.tcl
setup_state $foo $bar $baz

根据某些全局变量使 [source] 表现不同对我来说看起来太晦涩了。当然,你可能有正当理由这样做,但无论如何......

Regarding the "::" thing -- see the explanation here (sorry, I don't have enough rep. to leave comments yet).

I should also add that the original question discusses a problem which appears to be quite odd: it seems that it could be better to provide a specific procedure in your second source file that would set up a state pertaining to what is defined by that script.
Something like:

source file2.tcl
setup_state $foo $bar $baz

Making [source] behave differently based on some global variables looks too obscure to me. Of course you might have legitimate reasons to do this, but anyway...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文