tags:

views:

63

answers:

2

I have a script that calls:

 eval source \{$scriptfile\}

where $scriptfile is another TCL script. Is there way to pass parameters to the script? I'd like to do something like:

set sampleData "ID=14678934"
eval source \{$scriptfile\} $sampleData 

I know that this isn't allowed but, is there a way to pass data to a script that is being executed using eval source?

+3  A: 

The solution is given in the Tclers Wiki, in the following articles: source with args and SrcFile.

The solution I like the most is srcfile:

proc srcfile { filename args } {
  global argv

  set argv $args
  source $filename
}

The only drawback of this approach is that it will modify argv, so you have to make sure you will not need it in the rest of the script, or backup and restore it.

tonio
Thanks, you rock :)
Corv1nus
+5  A: 

That's a horrible practice to start. It's much cleaner to call a proc that is within the script you're sourcing.

source script.tcl  ;# defines proc run_script_with_data
run_script_with_data $data
Trey Jackson
This is an interesting approach, thanks.
Corv1nus
Another question. This is being run with interp eval source. Do you know how to access the data in a script run in another interpreter? I apologize for the noob questions but, I'm new to TCL unfortunately.
Corv1nus
@Corv1nus You mean a slave interpreter? (as in, on created by the Tcl command `interp`) Or one in a different process?
Trey Jackson
A slave interpreter.
Corv1nus
@Corv1nus The master always has access to the data in the slave. e.g. `set result [interp eval $slave set varname]` will retrieve the value stored in `varname` in the slave interpreter.
Trey Jackson
Thank you very much, worked perfectly.
Corv1nus