开发者

How to source a script file by passing arguments?

Say I have a tcl script and I want to pass some arguments to开发者_JAVA百科 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.


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


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...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜