Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run tcl script inside other tcl script?

Tags:

tcl

I have two tcl scripts. I want to run the second script when the first finished. How can I do it?

like image 953
d_pilot Avatar asked Feb 14 '12 10:02

d_pilot


People also ask

How do I run a file in Tcl?

You can run this program by starting tclsh from the start menu, then typing the command source c:/hello. tcl. Note that the traditional Windows \ is replaced with /. to your script.

How do I iterate through a list in Tcl?

Tcl Built-In Commands The foreach command implements a loop where the loop variable(s) take on values from one or more lists. In the simplest case there is one loop variable, varname, and one list, list, that is a list of values to assign to varname. The body argument is a Tcl script.


2 Answers

While this is generally a correct answer, because the question was not precisely formulated there are tons of ways to achieve the goal of running Tcl code from within Tcl. I want to get into this in detail because understanding the execution of code is one major point in understanding Tcl itself.

There is source

The source command should not be confound with executing scripts in a classical way, what I think the thread starter has asked.

The source command is like the "include" command in c/perl/php. Languages like java or python on the other hand only have "import" mechanisms.

The difference is that those languages create a internal database of available packages, who are linked to the corresponding source/binary/bytecode files. By writing a import statement, linked source or bytecode or binary files are loaded. This allows more in-depth dependency management without writing additional code. In Tcl this can be achieved with namespaces and the package require command. Example:

Suppose you have this source.tcl:

proc foo {bar} {puts "baz"}
set BAM "BOO"

Now, you have your "master" script like you call it. I call it "main". It has the content:

set BAM {my important data}
source source.tcl
#also the function foo can now be used because the source reads the whole script
foo {wuz}
set BAM
#will output "BOO"

The exec command

If you can live with additional overhead of starting a whole new interpreter instance you could also do:

set BAM {my important data}
exec tclsh source.tcl
#The variable BAM will not be modified. You can not use the function foo.

The eval command

The command eval can evaluate a string or a list (in Tcl everything is a string) like it would be programmed code. You would have to load the complete source file to a string. And then use eval, to evaluate the code within a separate scope, to not overwrite stuff in your main source file.

set fp [open "somefile" r]
set code_string [read $fp]
close $fp
eval $code_string
like image 28
Lis Avatar answered Dec 30 '22 10:12

Lis


Depends on what do you really mean.

One way is to write a third ("master") script which would do

source /the/path/to/the/first.tcl
source /the/path/to/the/second.tcl

Another way is to just add the second call to source from the above example to the bottom of the first script.

Amendment to the first approach: if the scripts to be executed are located in the same directory as the master script, an idiomatic way to source them is

set where [file dirname [info script]]
source [file join $where first.tcl]
source [file join $where second.tcl]

This way sourcing will work no matter what the current process's directory is and where the project directory is located.

like image 176
kostix Avatar answered Dec 30 '22 12:12

kostix