-1

I code a bit in MATLAB and this is my first time with TcL. So, needed help to know if in TcL is it possible to get variables of one script from another script? Like in MATLAB we pass variables to and from Workspace.

2 Answers2

0

Variables are bound to an interpreter by default, except for the global env array, which is mapped to a process global maintained by the C runtime (and which is really slow).

But other options are possible. For example, you can use the thread-shared-variable API in the thread package to have a variable that is shared between all threads in a process. The mapping isn't to a true variable by default, though (for simple use cases only) you can make the mapping via some variable traces:

package require Thread

# The lambdas do some adaptation of arguments in traces
trace add variable foo write {apply {args {
    global foo
    tsv::set share Foo $foo
}}}
trace add variable foo read {apply {args {
    global foo
    set foo [tsv::get share Foo]
}}}
trace add variable foo unset {apply {args {
    tsv::unset share Foo
}}}

If you want to share between processes (or between systems), things get a bit more complicated. I'd suggest looking at the Tequila system, if that is still alive. I do not know what the security consequences are, though a server that is only exposed to localhost isn't too much of a problem most of the time.

Donal Fellows
  • 120,022
  • 18
  • 134
  • 199
0
  • Method# 1

Original script:

$ cat bar.tcl 
set script_name $argv0
set var1 [lindex $argv 0]
set var2 [lindex $argv 1]
puts "$script_name: var1=$var1, var2=$var2"
$ 

Execution output:

$ tclsh bar.tcl 10 20
bar.tcl: var1=10, var2=20
$ 

Wrapper script:

$ cat foo.tcl 
set file_name bar.tcl
set argv0 $file_name
set argv [list 100 200]
puts "Invoking $file_name"
source $file_name
$ 

Execution output:

$ tclsh foo.tcl 
Invoking bar.tcl
bar.tcl: var1=100, var2=200
$ 
  • Method# 2

Wrapper script:

$ cat bat.tcl 
set file_name bar.tcl
puts "Invoking $file_name"
puts [exec tclsh $file_name 500 600]
$ 

Execution output:

$ tclsh bat.tcl 
Invoking bar.tcl
bar.tcl: var1=500, var2=600
$ 
Sharad
  • 5,301
  • 2
  • 16
  • 30