Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

global a b != global a; global b in this circumstance

3 views
Skip to first unread message

ewils...@gmail.com

unread,
Nov 17, 2007, 8:14:38 AM11/17/07
to
I had to quickly hack a script and came across a "gotcha" that wasn't
obvious because of a well-placed catch.

Here is a script that shows the problem:

#----------------------------
set x 1
set y1 "y1"

proc ok {} {
global x
global y$x
puts "$x [set y$x]"
}

proc fail {} {
global x y$x
puts "$x [set y$x]"
}

ok
fail
#----------------------------

Regards
E Wilson

Uwe Klein

unread,
Nov 17, 2007, 8:47:04 AM11/17/07
to
Ha, that answers my question, it is not Evil Son then ;-)

all tokens of a line are evaluated ($ substitution [] substitution
{*} expansion _and then_ this list is run as a command (item1) with
arguments Item-2 .. Item-last.
Thus $x must resolve _before_ the command [global] sees any of this.

uwe

ewils...@gmail.com

unread,
Nov 17, 2007, 9:38:15 AM11/17/07
to
On Nov 17, 11:47 pm, Uwe Klein <uwe_klein_habertw...@t-online.de>
wrote:

Ah, thanks for that clear explanation.

Evil son :-)

Bryan Oakley

unread,
Nov 17, 2007, 9:43:44 AM11/17/07
to

Do you understand why this is so? It's an important lesson to learn
about Tcl.

The explanation is pretty simple: tcl expands all variables before
calling the command. So, y$x is expanded, and _then_ passed to the
global command. At the point in time that y$x is expanded, x is undefined.

0 new messages