by bch · 9y ago · 0 comments
I think this may have improved since you last looked:
$ cat j
#!/usr/bin/env tclsh
puts [ set a [ expr { 9 + [set x]}]]
$./j
can't read "x": no such variable
while executing
"set x"
invoked from within
"expr { 9 + [set x]}"
invoked from within
"set a [ expr { 9 + [set x]}]"
invoked from within
"puts [ set a [ expr { 9 + [set x]}]]"
(file "./j" line 3)
> there are tons of places where a variable is referenced both like "$this" and "this"$this is essentially [ set this ], returning its value, or deferencing the value, if you will. The entire set of rules that Tcl abides by are contained entirely in a small document (here [0], with commentary). It's really not difficult to figure out.
Also, I have no problem with the logical "$x" syntax; rather, the fundamental flaw is that the string "x" could be used to change the variable "x" without any clue in the syntax that this may happen (e.g. no "&x" like in C, no "\$x" like in Perl; just "x", or worse "$y" that resolves to "x"). And don’t even get me started on multi-level "upvar". This whole thing is a minefield, especially if people use simple variable names that can’t be searched in a code base. If you’re really unlucky, you accidentally swap the order of parameters and it just so happens that your "$x" value is almost plausible so you never notice the disaster you just created. It’s simply a “write once, modify never” coding style: it saves the programmer 10 seconds and creates the most brittle of code. I would never recommend it to anyone.