quantum231
quantum231

Reputation: 2583

Does TCL Global scope covert all TCL scripts that are invoked using the TCL source command?

The TCL source command can be used to source TCL scripts from another TCl script. It is thus possible to nest the source command where we source a script that sources another script that sources another script and so on.

Now my question is, regardless of the nested level, is a variable declared as global in one script supposed to be accessible from all other and every other scripts and its name will clash with variable of same name in every other script? What if there are procedures in different scripts that are sourced and they happen to have the same name?

When I say ever other script I am referring to all the scripts that are running (or were sourced) inside a TCL interpreter at a time.

Upvotes: 1

Views: 49

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137587

The global scope (also equivalently called the namespace ::, or the topmost stack frame, #0) is common to all code running in the same interpreter context.

This is why packages are recommended to put their commands and variables in a namespace other than the global one (though some, including Tcl itself, put things in the global namespace for historical reasons). By convention, the global namespace is considered primarily the preserve of user code, i.e., your scripts.

You can make new interpreter contexts with the interp command, or when you spawn a thread (interpreters are very strictly single threaded, by design), or of course when you spawn a new process. Those do not share their global scopes.


The source command evaluates the script that it reads from the file in the current scope, whatever that is.

Upvotes: 3

Related Questions