Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to source a script file by passing arguments?

Tags:

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.

like image 628
Narek Avatar asked May 10 '11 11:05

Narek


2 Answers

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
like image 51
bmk Avatar answered Oct 24 '22 08:10

bmk


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

like image 22
kostix Avatar answered Oct 24 '22 08:10

kostix