Skip to content
Tcl Source Code Changes To Migrating scripts to Tcl 9
Login

Changes To Migrating scripts to Tcl 9

Changes to "Migrating scripts to Tcl 9" between 2024-06-06 01:27:14 and 2024-06-18 02:25:30

︙
109
110
111
112
113
114
115
116

117
118
119
120

121
122
123
124
125
126
127
109
110
111
112
113
114
115

116
117
118
119

120
121
122
123
124
125
126
127







-
+



-
+







## Change in variable name resolution

In Tcl 8 variable names that are not absolute, are resolved by looking first in the current namespace, and then in the global namespace. In Tcl 9 such variables are always interpreted as relative to the current namespace.

This avoids the problem that setting a variable inside a namespace scope will overwrite a global variable, if that global variable exists and the variable does not exist relative to the namespace. But it also has some consequences that may not immediately be obvious:

* Access to well-known variables such as env and tcl_platform inside a namespace eval block will need to either fully qualified or the variable must be brought into scope using `namespace upvar` command (`namespace upvar :: env env`).
* Inside procs in a namespace ns, it used to be possible to access namespace variables by referring to them as ns::var. That no longer works. Again, the solution is to either fully qualify the variable, or bring it into scope. In addition to the namespace upvar command, in this case that can also be done using the global command (`global ns::var`), as well as the variable command (`variable var`, or `variable ::ns::var`). The following egrep may be useful to detect such cases:
* Inside procs in a namespace ns, it used to be possible to access namespace variables within a global namespace `ns` by referring to them as ns::var. That no longer works. Again, the solution is to either fully qualify the variable, or bring it into scope. In addition to the namespace upvar command, in this case that can also be done using the global command (`global ns::var`), as well as the variable command (`variable var`, or `variable ::ns::var`). The following egrep may be useful to detect such cases:
```
egrep '(\S+\s+\$|(set|unset|append|lset|lappend|dict\s+(set|incr|append|lappend|with|update|unset)|upvar|namespace var|parray|info exists|gets\s+\S+|array\s+\S+|vwait)\s+)[a-zA-Z]+::' *.tcl
```
Not a complete regexp (oo, expr, variable and may be other commands)! Note each occurrence has to be checked since it may be legitimate reference relative to the current namespace (to be left alone) or a no-longer-valid reference relative to global namespace (which must be fully qualified). Strongly recommended this check be done. tclprodebug had over 500 such references (perfectly legitimate in Tcl8, not bugs) leading to error exceptions if lucky and to rather mysterious phase of the moon misbehavior otherwise.
Not a complete regexp (oo, expr, variable and may be other commands)! Note each occurrence has to be checked since it may be legitimate reference relative to the current namespace, i.e. a child namespace, (to be left alone) or a no-longer-valid reference relative to global namespace (which must be fully qualified). Strongly recommended this check be done. tclprodebug had over 500 such references (perfectly legitimate in Tcl8, not bugs) leading to error exceptions if lucky and to rather mysterious phase of the moon misbehavior otherwise.

Q - has the behavior of `global` changed within namespaces?

The behavior of the `global` command within namespaces has not changed. The command has no effect when used outside the context of a proc or apply body. Using `global` inside a namespace eval block does nothing. Variable names passed to the global command are still resolved relative to the global namespace, as they have always been. So, using `global ns::var` (inside a proc or apply body) will create a local variable var that is linked to the namespace variable ::ns::var.

See [TIP 278](https://core.tcl-lang.org/tips/doc/trunk/tip/278.md).

︙