0

I have a tcl file which has few config procs defined. My one app is coded in itcl which needs to call these config proc for each object. I am not able to source TCL file at the global level in itcl. it allows sourcing only within public methods but this is not what i am looking for. I want to source the tcl file at once only for all objects and then use it till the program runs. Any suggestions please?

thanks

Ruchi
  • 693
  • 3
  • 14
  • 27

1 Answers1

0

The source command always evaluates the file's contents at the current level; it really is effectively just read the file's contents into a string and then eval that (except for a little trickery with info script). That means that if you want to source a file in the global context, you should do one of these:

# Quotes around #0 just because of Stack Overflow's highlighting
uplevel "#0" {
    source thefile.tcl
}
uplevel "#0" [list source $thefile]
namespace eval :: {
    source thefile.tcl
}
namespace eval :: [list source $thefile]

The versions with list are doing code generation, and will work much better when the filename is in a variable (or generated by a command like file join); if everything is literal, you're better off using the braced versions.

The versions with uplevel #0 are different from the versions with namespace eval :: in that the former goes up the stack to the global level, whereas the latter just pushes a new stack frame that you can uplevel out of. Most of the time, with sane code, the differences are very slight; use the one you prefer as both are good code.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • Maybe `apply [list {} [list source $thefile] ::]` is also worth a note? – mrcalvin Apr 29 '18 at 12:49
  • But that does something else; unqualified variables in it are local and not global. – Donal Fellows Apr 29 '18 at 12:54
  • Sure, but this might be just fine or even required in certain scenarios. And given that the question is about "config procs" only (so command resolution in the global NS), this is a viable option so not to pollute anything. – mrcalvin Apr 29 '18 at 13:27