Question about source in Tcl

I have a file called test7.tcl:

namespace eval ::dai { variable name "ratzip" variable birthday "1982" proc hello {} { variable name variable birthday puts "Hello, I am $name birthday is $birthday" } } 

and I want to transfer this file to another file called test8.tcl as follows:

 source test7.tcl ::dai::hello 

but this gives me an error: could not read the file "test7.tcl": there is no such file or directory

but both files are in the same folder, what happened?

+7
source share
3 answers

To create a file that is in the same directory as the current script, use this:

 source [file join [file dirname [info script]] "test7.tcl"] 

Note that this does not work inside the procedures defined inside the external script (test8.tcl in your case), because they are usually called after the source completes. If this is for you, the easiest fix is ​​to simply save the info script output in a variable in your external script (or just load all the files right away, not lazy for the most optimal approach).

+10
source

Use source [file join [file dirname [info script]] test7.tcl] - this way you will search for the target file by its full path built from the full path of the file executing source ; this will work no matter what your current directory is at runtime.

+4
source

You do not need to specify the path to the file that will be used in relation to the path test8.tcl , but relative to the current working directory. For example. use the absolute path:

 source /path/to/test7.tcl 
+2
source

All Articles